且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

DDD中的DAO,存储库和服务

更新时间:2023-02-13 11:06:07

存储库是一个抽象-如您所说。它们源自Martin Fowler的对象查询模式。存储库和DTO都可以通过将持久数据映射到等效的实体对象集合来简化数据库持久性。但是,通过提供对整个 Aggregate Root(AG)的控制,存储库比DAO粒度更粗>通常会向客户端隐藏很多内部状态。另一方面,DAO可以像专用于单个实体对象一样精细。对于存储库和DAO,通常使用 Hibernate 或其他对象/关系映射(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() or Service.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.