DDD repository and factory

人走茶凉 提交于 2019-11-30 02:14:24

First of all, the layers approach is kinda obsolete. When talking layers think 'context', who's on top of who isn't important.

The repository is in charge of restoring an object. A factory just creates a new object. Note the different semantics. The repository knows how saving/restoring to/from persistence is done and that depends on the storage and the method of access.

So, everything is done inside the repository i.e in the Infrastructure. If you serialize things, then you need just to deserialize back (this is how a document db does things anyway). If you're using an ORM or store things in tables then you'll do all the query required to get the data and repopulate the object. An ORM is the easiest way since it can use reflection to populate private properties. In this case the ORM itself is the factory.

One more thing, restoring, while technically can be done by a domain factory, it isn't the factory's purpose to do that because it breaks the layer boundaries. We want to keep everything persistence related in the Infrastructure.

Factory & Repository Concepts

To answer your question, I think it's important to focus on responsibilities of the concepts defined by DDD.

In the blue book, there is a section that deals with the problem that you describe:

A FACTORY handles the beginning of an object’s life; a REPOSITORY helps manage the middle and the end.

and specifically for your question:

Because the REPOSITORY is, in this case, creating objects based on data, many people consider the REPOSITORY to be a FACTORY—indeed it is, from a technical point of view.

(both quotes from Evans, chapter 6, section "The relationship with factories")

To keep the concepts pure, it is important that the interface of your factories and repositories are clean. So don't allow creating new business objects through the repository interface, and don't allow querying existing ones through the factory interface.

Keeping the interfaces clean does however not mean that you should not use a factory from the repository implementation, because, after all, the repository creates an instance at some point, and if that instance creation is complex, a factory is the appropriate solution.

To quote Evans again:

Whenever there is exposed complexity in reconstituting an object from another medium, the FACTORY is a good option.

Note, however, that the repository will most likely call a different method on the factory than the clients that really want to create a new domain object (as opposed to reconstitution).

There is even an example in Evans' book that illustrates approach:

Answer to your question

Now that it is clear that this is allowed, lets focus on your question of where to put the factory:

The DDD factory interface belongs in the domain, because your domain logic uses this to create domain objects.

The DDD reconstitution factory interface does not belong in the domain, since this is only relevant for your repository. It does not exist in the real world of your domain.

Now if you're using an architecture that prohibits dependencies from the domain to the infrastructure (which you probably should when applying DDD), it's clear that the factory implementation belongs in the infrastructure. Note that it does not matter whether you call your layers layers, rings, realms or whatever, the dependencies are the important part.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!