有关存储库的域驱动设计问题 [英] Domain Driven Design issue regarding repository

查看:62
本文介绍了有关存储库的域驱动设计问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现DDD,因此我创建了以下类:

-用户[域模型]

-UserRepository [管理对象的中央工厂)]

-UserMapper + UserDbTable [用于映射应用程序功能并提供CRUD实现的Mapper]

I am trying to implement the DDD so I have created the following classes
- User [the domain model]
- UserRepository [a central factory to manage the object(s)]
- UserMapper + UserDbTable [A Mapper to map application functionality and provide the CRUD implementation]

我的第一个问题是何时需要模型与持久层通信,它应该联系存储库还是映射器?我个人认为它应该询问存储库,该存储库将与映射器联系并提供所需的功能。

My first question is that when a model needs to communicate with the persistent layer should it contact the Repository or the mapper? Personally I am thinking that it should ask the repository which will contact the mapper and provide the required functionality.

现在,我的第二个担心是对于同一个类的所有对象应该只有一个存储库,所以这意味着我将创建一个单例。但是,如果我的应用程序有很多域模型(比如说20个),那么将有20个单例。而且感觉不对。另一个选择是使用DI(依赖注入),但是我使用的框架(Zend Framework 1.11)不支持DIC。

Now my second concern is that there should be only one repository for all the objects of same class, so that means I will be creating a singleton. But if my application has lots of domain models (lets say 20), then there will be 20 singletons. And it don't feel right. The other option is to use DI (dependency injection) but the framework I am using (Zend Framework 1.11) has no support for DIC.

我的第三个

推荐答案



  • UserRepository [管理对象的中央工厂]

在DDD存储库中不是工厂。存储库负责域对象的生命周期的中期和结束。工厂负责起步。从概念上讲,域对象的生存和恢复发生在中间对象中。

In DDD Repository is not a Factory. Repository is responsible for middle and end of life of the domain object. Factory is responsible for beginning. Conceptually, persisting and restoring happens to the domain object in its middle life.



  • UserMapper + UserDbTable [用于映射应用程序功能的Mapper并提供CRUD实现]

这些类不属于域层,这是数据访问。它们都将由存储库实现封装(或者如果使用ORM则根本不存在)。

These classes do not belong to a domain layer, this is data access. They would all be encapsulated by repository implementation (or would not exist at all if you use ORM).


我的第一个问题是模型何时需要进行通信与
持久层一起,它应该联系存储库还是映射器?
就我个人而言,我认为它应该询问存储库,它将使
与映射器联系并提供所需的功能。

My first question is that when a model needs to communicate with the persistent layer should it contact the Repository or the mapper? Personally I am thinking that it should ask the repository which will contact the mapper and provide the required functionality.

Model确实可以不需要与持久层进行通信。实际上,您应该尝试使模型尽可能与持久性无关。从域模型的角度来看,存储库只是一个接口。该接口的实现属于另一个层-数据访问。稍后在您的应用程序层中的某个位置注入实现。应用程序层知道持久性和事务。在这里,您可以实现工作单元模式(该模式也不属于域层)

Model does not need to communicate with persistence layer. In fact you should try to make your model as persistence-agnostic as possible. From the perspective of your domain model, Repository is just an interface. The implementation of this interface belongs to a different layer - data access. The implementation is injected later, somewhere in your Application layer. Application layer is aware of persistence and transactions. This is where you can implement Unit Of Work pattern (that also does not belong to domain layer).


现在,我的第二个担心是,对于同一类的所有对象,应该只有一个存储库,所以这意味着我将创建一个单例。但是,如果我的应用程序有很多域模型(比如说20个),那么将有20个单例。

Now my second concern is that there should be only one repository for all the objects of same class, so that means I will be creating a singleton. But if my application has lots of domain models (lets say 20), then there will be 20 singletons.

首先,您可以拥有给定域对象的多个存储库。无论如何,大多数情况下都会发生这种情况,因为您要避免在存储库界面上发生方法爆炸。其次,单例存储库不是一个好主意,因为它将所有使用者耦合到一个实现上,这将使单元测试变得困难。第三,拥有20个或更多的存储库没有什么错,实际上,重点突出的类越好,请参阅 SRP

更新:

我认为您在混淆常规Factory模式和DDD Factory。用DDD术语来说,从数据库中还原对象时,它在概念上已经存在(即使它是内存中的新对象)。因此,存储库有责任保留并还原它。当复杂的域对象开始使用时,DDD Factory就开始发挥作用-不管它是一个长期存在的对象(保存在db中)。

I think that you are confusing regular Factory pattern and DDD Factory. In DDD terms, when the object is restored from the database it already exists conceptually (even though it is a new object in memory). So it is a responsibility of the Repository to persist and restore it. DDD Factory comes into play when the complex domain object begins its life - whether it would be a long lived object (saved in db) or not.

这篇关于有关存储库的域驱动设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆