C#域模型存储库:放置加载实体的代码的位置
发布时间:2020-12-15 08:06:01 所属栏目:百科 来源:网络整理
导读:我有一个模型类,它从我的存储库类中的“GetById”方法加载.我现在需要向此实体添加其他属性,这些属性不保存在数据库中,而是由服务类计算.就像是: public class MyEntity{ public int ThingId { get; set; }; public string ThingName { get; set; } // Set
我有一个模型类,它从我的存储库类中的“GetById”方法加载.我现在需要向此实体添加其他属性,这些属性不保存在数据库中,而是由服务类计算.就像是:
public class MyEntity { public int ThingId { get; set; }; public string ThingName { get; set; } // Set from the service public int WooFactor { get; set; } } public class WooFactorGenerator { public int CalculateWooFactor(MyEntity thing); // Hits other services and repo's to eventually determine the woo factor. } // Code to get a "MyEntity": var myEntity = repo.GetById(1); var gen = new WooFactorGenerator(); myEntity.WooFactor = gen.CalculateWooFactor(myEntity); 因此,为了加载/饱和MyEntity对象,我需要从db加载,然后调用生成器来确定“woo factor”(哼).这段代码应该从架构的角度来看?目前的想法: 1)在存储库中:如果我在这里添加它,我觉得我对repo负有太多责任. 2)在“MyEntity”课程中.在这里添加可能在访问WooFactor时加载WooFactor的代码.这会给MyEntity添加很多依赖项. 3)一个单独的服务类 – 似乎过度和不必要. 解决方法
>如果WooFactor纯粹依赖于MyEntity属性,那么它必须在MyEntity中完成
>如果它需要外部信息(例如配置,规则等),那么它需要是存储库外部的单独服务.我会在这里使用这个附加属性创建一个WooEntity. 无论如何,它永远不应该在存储库中. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |