0

We have Customer transaction table with many foreign key relationships.

We only want to see certain columns of the 3 tables joined in a web interface.

Eg just Date, PurchaseAmount, ProductName, and StatusDescription.

I hear repository should not select certain columns, it should select All columns. So we should not use repository below. If reading all columns or too many related tables, sql query can slow down with unoptimized data.

What should I name this new data access method?

Additionally, should I create own folder for this, I already have a Repository folder., should I create another folder for WebRepository?

CustomerTransactionWebRepository instead of CustomerTransactionRepository?

Repository:

public class CustomerTransactionRepository: ICustomerTransactionRepository{
    public CustomerTransaction GetCustomerTransaction(int CustomerTransactionId ) 
    {
        var result = context.CustomerTransaction.Where(c=>c.CustomerTransactionId == id)
         .Include(p=>p.ProductType)
         .Include(s=>s.StatusType);
    }


public class CustomerTransactionWebServiceRepository: IWebServiceRepository 
{
    public CustomerTransaction GetCustomerTransaction(int CustomerTransactionId ) 
        {
            var result = context.CustomerTransaction.Where(c=>c.CustomerTransactionId == id)
             .Include(p=>p.ProductType)
             .Include(s=>s.StatusType);
              .Select(p => new {
                      DateOfPurchase= c.DateOfPurchase, 
                      PurchaseAmount= c.PurchaseAmount, 
                      ProductName= p.ProductName
                      StatusDescription = s.StatusDescription
        }

Model sample:

public class CustomerTransaction
{
    public int CustomerTransactionId { get; set; }
    public string DateOfPurchase{ get; set; }
    public int PurchaseAmount { get; set; }

    // relationships
    public ProductType ProductType { get; set; }
    public int ProductTypeId { get; set; } 

    public StatusType StatusType { get; set; }
    public int StatusID { get; set; }  
}
1

What you should do is make an object that matches your table columns. Then write a repository that returns a list of those objects. ie:

select * from CustomerTransaction where x

Remove the child objects from CustomerTransaction and don't try to build a whole object graph in one go.

You can probably go further. I imagine some of your xxxType objects look like:

ProductType
int id              = 1
varchar type = 'widget'

These can be simplified by getting rid of the id, putting the 'type' field on the main table and mapping it to an enum

  • yes, I already have a viewmodel, I have questions down below, What should I name this new data access method? Additionally, should I create own folder for this, I already have a Repository folder., should I create another folder for WebRepository? CustomerTransactionWebRepository instead of CustomerTransactionRepository? – SkyPool392 May 28 '18 at 10:29
  • read my answer again slowly. Im saying dont write your new type of repository at all. – Ewan May 28 '18 at 10:35
  • ok, I see , Then write a (function in an existing) repository that returns a list of those objects. ie: – SkyPool392 May 28 '18 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.