课堂设计问题 [英] Class Deisgin Question

查看:116
本文介绍了课堂设计问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有客户,发票标题,发票明细和产品.都是一对多的关系.

我的解决方案有一个使用实体框架构建的数据层项目.我还有一个模型项目,其中包含每个实体的模型.数据层项目引用模型项目.

到目前为止,非常典型.

问题是这个...

这是CustomerModel:

I have Customers, Invoice Headers, Invoice Details, and Products. All are one-to-many relationships.

My solution has a data layer project built using entity framework. I also have a models project with models for each entity. The data layer project references the models project.

So far, pretty typical.

The question is this...

Here is the CustomerModel:

public class CustomerModel : _ModelBase
{
    public int CustomerId { get; set; }
    public string CustomerName { get; set; }
    public List<invoiceheadermodel> InvoiceHeaders { get; set; }
}
</invoiceheadermodel>




我在DataLayer类上具有此GetCustomer方法:




I have this GetCustomer method on the DataLayer class:

public static CustomerModel GeCustomer(int CustomerId)
{
    CustomerModel retVal = (from c in context.tblCustomers
                            where c.CustomerId == CustomerId
                            select new CustomerModel
                            {
                                CustomerId = c.CustomerId,
                                CustomerName = c.CustomerName
                            }).FirstOrDefault();
    return retVal;
}



问题是在此方法中,我没有加载InvoiceHeaders属性.如果我在这里这样做,还可以加载发票详细信息,然后加载产品吗?

如果我想要的只是一个简单的客户"列表,以便我可以填充一个简单的列表,该怎么办?

我可以使用延迟加载,但这需要将模型耦合到数据层.

正确的方法是什么?



The problem is in this method I did not load the InvoiceHeaders property. If I did that here, would you also then load the invoice details, and then the products?

What if all I wanted was a simple list of Customers so that I could populate a simple list?

I could use lazy loading, but that requires the Models to be coupled to the data layer.

What''s the right way to do this?

推荐答案

您可以添加一个params数组,该数组引用要链接到的每个渴望加载到方法签名的实体.数据层:

You can add a params array that references each linked entity you want to eager load to the method signature in your data layer:

public static CustomerModel GetCustomer(int CustomerId, params string[] eagerLoadCollections)
{
    var retVal = (from c in context.tblCustomers
                            where c.CustomerId == CustomerId
                            select new CustomerModel
                            {
                                CustomerId = c.CustomerId,
                                CustomerName = c.CustomerName
                            });

    foreach(string eagerLoadCollection in eagerLoadCollections)
    {
       retVal = retVal.Include(eagerLoadCollection);
    }

    return retVal.FirstOrDefault();
}



这使您可以根据需要加载尽可能多的相关实体集合:



This enables you to load as many related entity collections as you want:

var customer = DataLayer.GetCustomer(123);



或:



or:

var customer = DataLayer.GetCustomer(123, "InvoiceHeaders", "OtherRelatedEntities", "etc.");



这是我不能说的正确方法.它对我有用:)



Whether it''s the right way I can''t say. It works for me :)


这篇关于课堂设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆