简体   繁体   English

如果存储库是针对聚合根的,那么其他实体的数据访问逻辑应该去哪里?

[英]If repositories are for aggregate roots, where should data access logic for other entities go?

I have a few objects that represent a web application. 我有一些代表Web应用程序的对象。 Currently I have a cluster object to represent a specific deployment of the app. 目前,我有一个集群对象来表示应用程序的特定部署。 Within a cluster object I have the following objects: Server, Customer, User. 在集群对象中,我有以下对象:服务器,客户,用户。 None of these objects can exist without being part of a cluster, so I created a ClusterRepository to retrieve the clusters from the database. 如果不成为集群的一部分,这些对象都不能存在,因此我创建了一个ClusterRepository来从数据库中检索集群。 Now, from the cluster I need to get a list of Customers, presumably by using a method in the Cluster object like GetCustomers(). 现在,从集群中我需要获得一个Customers列表,可能是通过使用Cluster对象中的方法,如GetCustomers()。 Now, my initial thought was to then offload the work of this operation to a CustomerRepository, but since repositories are only for aggregate roots, where should the data access logic go? 现在,我最初的想法是将此操作的工作卸载到CustomerRepository,但由于存储库仅用于聚合根,因此数据访问逻辑应该在哪里? Does this belong in a service class? 这属于服务类吗?

Essentially, an Aggregate root is any object that you might need to fetch as the root of an object graph. 实质上,聚合根是您可能需要作为对象图的根获取的任何对象。 Just because a specific entity is an aggregate root, and has a repository, does not mean that another entity which is one of it's properties can't also be an aggregate root - with it's own repository. 仅仅因为特定实体是聚合根并且具有存储库,并不意味着作为其中一个属性的另一个实体也不能是聚合根 - 使用它自己的存储库。

A good example is a customer billing system. 一个很好的例子是客户计费系统。 Customer would certainly be an aggregate root, and would contain a collection of invoices... But for another application function, the invoice itself might be an aggregate root with constituent LineItem objects in it's object graph. 客户肯定是一个聚合根,并且将包含一组发票......但对于另一个应用程序函数,发票本身可能是一个聚合根,其对象图中包含组成LineItem对象。

So in your example, there is nothing wrong with creating another repository for customers, if you need to fetch them independantly of clusters in some situations. 因此,在您的示例中,如果您需要在某些情况下单独获取群集,则为客户创建另一个存储库没有任何问题。

NOTE: See thread in comments as well. 注意:请参阅注释中的线程。 Although root entities can, (and often will) have references to other root entities, it is frowned upon (and that may be too mild a turn of phrase) to allow the repository for any root entity to contain functionality that manages (creates, updates, or deletes) any root entity within its object graph that it has a reference to. 虽然根实体可以(并且经常会)引用其他根实体,但是不赞成(并且这可能是一个过于温和的短语)允许任何根实体的存储库包含管理(创建,更新)的功能,或删除)其引用的对象图中的任何根实体。 Any such referenced root entities should have their own individual repositories, and the functionality to manage them (Create, Update and/or delete operations) should be in their repository, so that it is only in one place. 任何此类引用的根实体都应该有自己的各个存储库,管理它们的功能(创建,更新和/或删除操作)应该在它们的存储库中,以便它只在一个地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM