c#Repository 模式:每个子类一个存储库?
我想知道您是否会为域模型的每个子类创建一个存储库。 例如,有两个类:
public class Person { public virtual String GivenName { set; get; } public virtual String FamilyName { set; get; } public virtual String EMailAdress { set; get; } } public class Customer : Person { public virtual DateTime RegistrationDate { get; set; } public virtual String Password { get; set; } }
您会创建 PersonRepository 和 CustomerRepository 还是仅创建也能够执行与 Customer 相关的查询的 PersonRepository?
I am wondering if you would create a repository for each subclass of a domain model.
There are two classes for example:
public class Person { public virtual String GivenName { set; get; } public virtual String FamilyName { set; get; } public virtual String EMailAdress { set; get; } } public class Customer : Person { public virtual DateTime RegistrationDate { get; set; } public virtual String Password { get; set; } }
Would you create both a PersonRepository and a CustomerRepository or just the PersonRepository which would also be able to execute Customer related queries?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这实际上取决于您的域的工作方式。通常,每个 聚合根(一种 DDD 术语,意思是域的父实体(但不是传统的继承方式)。
如果您确实将个人和客户作为域的一部分进行工作,那么每个人的存储库都是合适的。存储库不需要繁重的实现,并且存储库通常可以共享许多实现,尤其是在使用 ORM 时。
此页面是很好地介绍了存储库模式的目标。 领域驱动设计方法是一个很好的起点。
It really depends on how you domain works. You would usually have one Repository for every Aggregate Root (a DDD term meaning a parent entity, but not in the traditional inheritance way) of your domain.
If you really work with both Person and Customer as part of you domain, then a Repository for each is suitable. A Repository does not need to have a heavy implementation, and Repositories can often share a lot of their implementation, especially when working with an ORM.
This page is a good introduction to the aims of the repository pattern. The Domain Driven Design approach is a good place to start.