简体   繁体   English

扩展POCO类以使用其他上下文进行计算

[英]Extending POCO class to make calculations using other contexts

Consider these two POCO classes for use with EF Code First. 考虑将这两个POCO类与EF Code First一起使用。

public class Sale 
{
    public int SaleId { get; set; }
    public DateTime Date { get; set; }
    public double Amount { get; set; }
}

public class History
{
    public int HistoryId { get; set; }
    public DateTime Date { get; set; }
    public double Total { get; set; }
}

That is all well and good, but I actually want my class to represent more when I'm using it in my application: 一切都很好,但是当我在应用程序中使用它时,我实际上希望我的班子代表更多:

 public class SaleWithAllIWant
    {
        public int SaleId { get; set; }
        public DateTime Date { get; set; }
        public double Amount { get; set; }
        public double Calculation   // Read only and NOT stored in DB
        {
            get
            {
                // Obtain Total from _Context.History on Date
                // Perform Calculation
            }
        }
    }

I want to be able to use EF's beauty and be able to do something like: 我希望能够使用EF的美丽,并能够执行以下操作:

gridSales.DataSource = _Context.Sales.ToList();

However Sale doesn't have all of my info. 但是Sale没有我的全部信息。 SaleWithAllIWant is not a context so I'd have to do some sort of post butchering to get everything from Sale into SaleWithAllIWant. SaleWithAllIWant并非上下文,因此我必须进行某种形式的屠宰才能将所有内容从Sale转移到SaleWithAllIWant中。 I could use unbound columns or similar but it gets messy and starts to affect performance. 我可以使用未绑定的列或类似的列,但它会变得凌乱并开始影响性能。

I can't just put SaleWithAllIWant in my POCO class because the Calculation requires knowledge of the History context. 我不能只将SaleWithAllIWant放在我的POCO类中,因为“计算”需要了解“历史”上下文。 My DAL project (which creates the DbSets) references my Model project (which holds the POCO classes) – so the Model project can't reference the DAL to get access to the contexts as it would be a circular reference. 我的DAL项目(创建DbSet)引用了我的Model项目(包含POCO类)–因此,Model项目无法引用DAL来访问上下文,因为它是循环引用。

Is the solution to just bung it all in together and not have a separate DAL and Model project? 是否只是将所有内容放在一起而不用单独的DAL和Model项目的解决方案?

I suspect there is a better way I am missing. 我怀疑还有一种更好的方式我会丢失。 All comments much appreciated. 所有评论非常感谢。

Merging DAL and Model together is definitely an option I'd consider if you're not deploying them separately. 如果您不单独部署DAL和Model,则绝对可以考虑将其合并。

That said, Calculation looks like a great candidate for an extension method: 也就是说, Calculation似乎是扩展方法的理想之选:

public static class SaleExtensions
{
    public static double Calculation(this Sale sale)        
    {
            // Obtain Total from _Context.History on Date
            // Perform Calculation
    }
}

which could live in your DAL project. 它可以存在于您的DAL项目中。 If you need any additional properties to be kept in Sale for the purpose of doing the calculation just mark them with [NotMapped] so EF would ignore them. 如果出于计算目的需要将任何其他属性保留在Sale中,则只需将其标记为[NotMapped]以便EF忽略它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM