c#Repository 模式:每个子类一个存储库?

发布于 2024-08-27 07:34:50 字数 484 浏览 3 评论 0原文

我想知道您是否会为域模型的每个子类创建一个存储库。 例如,有两个类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荭秂 2024-09-03 07:34:50

这实际上取决于您的域的工作方式。通常,每个 聚合根(一种 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文