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; }  
}
有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
scroll top