相关疑难解决方法(0)

DDD:实体的集合和存储库

假设我有

public class Product: Entity
{
   public IList<Item> Items { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

假设我想找到一个带有最大值的项...我可以添加方法Product.GetMaxItemSmth()并使用Linq(from i in Items select i.smth).Max())或手动循环或其他任何方法.现在,问题是这会将完整的集合加载到内存中.

正确的解决方案是执行特定的数据库查询,但域实体无法访问存储库,对吧?所以我要么

productRepository.GetMaxItemSmth(product)
Run Code Online (Sandbox Code Playgroud)

(这很丑陋,没有?),或者即使实体有权访问存储库,我也使用IProductRepository实体

product.GetMaxItemSmth() { return Service.GetRepository<IProductRepository>().GetMaxItemSmth(); }
Run Code Online (Sandbox Code Playgroud)

这也是丑陋的,是代码的重复.我甚至可以去看看并进行扩展

public static IList<Item> GetMaxItemSmth(this Product product)
{
   return Service.GetRepository<IProductRepository>().GetMaxItemSmth();
}
Run Code Online (Sandbox Code Playgroud)

哪个更好只是因为它并没有真正使存储库中的实体混乱......但仍然是方法重复.

现在,这是是否使用product.GetMaxItemSmth()productRepository.GetMaxItemSmth(product)再次使用的问题.我错过了DDD的东西吗?这里的正确方法是什么?刚用productRepository.GetMaxItemSmth(product)?这是每个人都使用和满意的吗?

我只是感觉不对...如果我无法Items从产品本身访问产品,为什么我需要这个系列呢Product?然后,Product如果它不能使用特定的查询并访问其集合而没有性能命中,可以做任何有用的事情吗?

当然,我可以使用效率较低的方式,而且从不介意,当它很慢时,我会将存储库调用作为优化注入实体......但即使这听起来也不对,是吗?

有一点要提一下,也许它不是DDD ......但我需要在Product中使用IList才能获得使用Fluent NHibernate生成的数据库模式.不过,请在纯DDD环境中自由回答.

更新:这里描述了一个非常有趣的选项:http://devlicio.us/blogs/billy_mccafferty/archive/2007/12/03/custom-collections-with-nhibernate-part-i-the-basics.aspx,不仅处理与DB相关的集合查询,也可以帮助进行集合访问控制.

c# domain-driven-design

13
推荐指数
2
解决办法
4359
查看次数

标签 统计

c# ×1

domain-driven-design ×1