存储库模式和多个相关的核心实体或业务对象 - 一个或多个存储库?

发布于 2024-08-23 12:42:46 字数 338 浏览 1 评论 0原文

我正在考虑实现存储库模式(因为无论如何,我想出的 90% 都是它的实现),并且遇到了一个设计问题 - 我有两个或多个核心业务对象(例如,业务和联系人) CRM 应用程序),BO 可以是强相关的,也可以是根本不相关的。

在这种情况下,我应该实现一个存储库(例如 CrmRepository,带有 .addBusiness()、.addContact() 等)还是多个存储库(BusinessRepository、ContactRepository,每个存储库都有自己的 .add()、.delete() 等) )。

在这种情况下,最佳做法是什么?

底层 DAL 是 EF4。

问候

I am looking at implementing the repository pattern (since what I came up with was 90% an implementation of it anyway), and have come across a design question - where I have two or more core business objects (for example, Business and Contact in a CRM app), the BO's can be strongly related or not related at all.

In this situation, should I implement one repository (CrmRepository for example, with .addBusiness(), .addContact() et al), or multiple repositories (BusinessRepository, ContactRepository each with their own .add(), .delete() et al).

What is the best practice in this situation?

The underlying DAL is EF4.

Regards

Moo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

摘星┃星的人 2024-08-30 12:42:46

最近,我们在工作中进行了很多思考,并发现了一些文章,帮助我们以一致的方式可视化和设计我们的存储库。

根据我们的发现,更好的做法之一是为每个聚合根创建一个存储库。聚合根将是一个实体类型,您需要引用该实体类型才能访问子值类型。只能从数据库查询实体类型,并且需要从实体遍历任何子值类型。

根据您问题中的信息,业务似乎是一个聚合根,因此是一个实体类型,并且需要自己的存储库。由于联系人可以独立存在,因此也可能是聚合根。两个对象可以相互引用,并使用存储库从联系人加载业务或通过其各自的存储库从业务加载联系人。

我最近读了很多书,所以我希望我的思考过程有意义。

一些链接

聚合根

实体、值对象、聚合和根

We have been doing a lot of thinking recently at my work and came across a few articles that helped us visualize and design our repositories in a consistent manner.

From what we found out one of the better practices is to create one repository per aggregate root. An aggregate root would be an Entity type where you need to reference that entity type to reach child value types. Only an Entity type could be queried from the database and any child Value types would need to be traversed from the Entity.

With your information in your question it seems like a Business would be an aggregate root and thus an Entity Type and would require its own repository. Since Contact can live independently that might be an aggregate root as well. Both objects could have references to each other and use a repository to load up the Businesses from a Contact or Load up the Contacts from a Business via its respective repository.

I have been doing a lot of reading recently so i hope I made some sense in my thought process.

Some links

Aggregate Root

Entities, Value Objects, Aggregates and Roots

感情洁癖 2024-08-30 12:42:46

我完全同意马克的观点,但还要补充一点。当您了解创建通用存储库的好处时,常见的模式是 IRepository 和 Repository。我发现更有用的一件事是 Jeremy D. Miller(找不到参考资料)指出的,那就是在方法级别使用泛型。

所以我的 IReposity 将具有这样的方法:

T FindByKey<T>(int key);
IEnumerable<T> FindAll();
T FindBy<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression);
void Update<T>(entity);

然后,根据您的理念,您可以传递 Repository 类并直接查询它,或者使您的 Repository 实现抽象并强制它被显式存储库封装,如下所示:

CrmRepository : Repository
{
   FindByCustomerId(int customerId)
   { return FindByKey<Customer>(customerId);}
}

I totally agree with Mark on this, but to add a little more. As you look at the benefits of creating a Generic Repository, the common patter is IRepository and Repository. One thing I've found to be much more useful, brought to light by Jeremy D. Miller (can't find the reference) is having generics at the method level.

So my IReposity will have methods like this:

T FindByKey<T>(int key);
IEnumerable<T> FindAll();
T FindBy<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression);
void Update<T>(entity);

Then, depending on your philosophy, you can pass around the Repository class and query it directly, or make your Repository implementation abstract and force it's use to be encapsulated by an explicit repository, like this:

CrmRepository : Repository
{
   FindByCustomerId(int customerId)
   { return FindByKey<Customer>(customerId);}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文