问题描述
在阅读了几篇文章之后,我开始理解DAO和存储库之间的区别,但是在尝试理解存储库和服务之间的区别时遇到了麻烦。
After reading several articles, I am starting to understand the difference between DAO and Repositories, but I find myself in trouble trying to understand the difference between Repositories and Services.
简而言之,在OO范式中:
For putting in short terms, in the OO paradigm:
-
DAO :包含以下内容的类一个实体类的基本
CRUD操作
。它具有获取或检索基础持久性存储系统内容的必要代码。一般来说,方法使用对象实体作为参数,但retrieve
方法除外,在该方法中使用标识符类型是有效的。
DAO : Class that contains the basic
CRUD operations
for one entity class. It has the necessary code to get or retrieve things of the underlying persistent storage system. Generally speaking, the methods receive object entities as parameters, except in theretrieve
method where using a type of the Identifier is valid.
存储库:在更高的抽象级别上。通常,我读过的地方是放置对聚合对象(具有子对象的对象)进行处理的代码。它使用 DAO
s从数据库中检索对象,最后以域业务语言公开一个接口。 (但同样,我认为使用id的数据类型非常有效)。示例:一个非常简单的 addSomething
,其中 something
是其实例btw作为实例管理的父级的子对象。
Repositories : In a higher level of abstraction.. as generally I have read is a kind of place where put code that handle operations over aggregate objects (objects that have child objects). It uses the DAO
s to retrieve objects from the database, and in the end it exposes an interface in the domain "business" language. (But again, I think it is very valid to use data types of ids). Example : A very simple addSomething
where something
is a child object of the parent whose instances, btw, are managed as a whole by the Repository.
服务:同样,它处于更高的抽象级别。以我的拙劣观点,它们是连接两个不共享父子关系但与存储库(在抽象术语上)一样远的类的好地方。示例:两个银行帐户
之间的方法 transferCash
。
Services : Again, it is in a higher level of abstraction. To my humble point of view they are a good place to connect two classes that do not share parent-child relation, but is as far (in abstraction terms) as Repository. Example : The method transferCash
between two bank accounts
.
所以,这是我的读物,但我想在这里问以上想法是否正确。或者我应该怎么想。还是让我真正了解所有这些概念的区别的东西。
So, that's are my readings about, but I am asking here the above thoughts are right or not. Or how I should think. Or something that points me to really understand the difference of all this concepts.
某些资料来源:
- 。存储库和DTO都可以通过将持久数据映射到等效的实体对象集合来简化数据库持久性。但是,通过提供对整个或其他对象/关系映射(ORM)框架,而不是编写自己的框架实施。
Repositories are - like you say - an abstraction. They originate from Martin Fowler's Object Query Pattern. Both Repositories and DTOs can simplify database persistence by mapping persisted data to equivalent collection of entity objects. However, Repositories are more coarse-grained than DAOs by providing control of an entire Aggregate Root (AG) often hiding a lot of internal state from the client. DAO's on the other hand can be as fine-grained as being dedicated to a single entity object. For both Repositories and DAOs it is common to use Hibernate or other Object/Relational Mapping (ORM) Frameworks instead of writing your own implementation.
通常,服务可以驻留在服务层中,并且既可以充当功能外观,反腐败层又可以充当缓存和共享的协调器。交易。它们通常是进行日志记录的好地方。服务粗粒度且面向用例,例如
Service.updateCustomerAdress()
或Service.sendOrder()
。储存库的粒度可能太小,客户无法使用,例如Customer.add(...)
,Order.modify(...)
。Typically, services can reside in a Service Layer and can act both as a functionality facade, anti-corruption layer and coordinator for caching & transaction. They are often a good place to conduct logging. Services coarse-grained and usecase-oriented, e.g.
Service.updateCustomerAdress()
orService.sendOrder()
. Repositories can be too fine-grained for clients to consume, e.g.Customer.add(…)
,Order.modify(…)
.存储库和DAO具有相同的目的-永久存储数据。另一方面,服务应该忽略持久性,并且不了解数据库。它们通常与域服务,存储库,域核心紧密协作。
Repositories and DAOs have the same purpose - to persist data permanently. Services on the other hand should be ignorant of persistence and have no knowledge about your database. They usually work tightly together with domain services, repositories, domain core.
这篇关于DDD中的DAO,存储库和服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!