Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD repository and factory

In my application a few layers. In this topic will focus on Domain and Infrastructure layers.

I have repository interface ClientRepositoryInterface in Domain layer. And I have implementation of this interface ClientRepositoryImpl in Infrastructure layer.

But to reconstitute the object in the middle of the cycle of its existence I need factory(ReconstitutionClientFactory). Call the factory will be in the repository. The book by Eric Evans is described as a normal practice.

But where this factory(ReconstitutionClientFactory) should be located? In Domain or in Infrastructure layer?

I think in Domain... BUT! But then the lower layer will directly call a higher layer! This is wrong, but how to do right?

like image 557
stalxed Avatar asked Jul 21 '15 00:07

stalxed


People also ask

What is Factory in DDD?

A factory is a tactical pattern used in the DDD world. It helps us create complex objects. It is important to keep in mind that we should only implement this pattern when the instantiation of an object is complex.

What is a DDD repository?

In DDD, a repository is an objcect that participates in the domain but really abstracts away storage and infrastructure details. Most systems have a persistent storage like a database for its fully functioning. Applying repositories happens by integrating and synchronizing with existing aggregate objects in the system.

What is Domain Service in DDD?

Introduction. Domain Services (or just Services in DDD) is used to perform domain operations and business rules. In his DDD book, Eric Evans describes a good Service in three characteristics: The operation relates to a domain concept that is not a natural part of an Entity or Value Object.

What is domain-driven design example?

An aggregate is a domain-driven design pattern. It's a cluster of domain objects (e.g. entity, value object), treated as one single unit. A car is a good example. It consists of wheels, lights and an engine.


1 Answers

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:

Domain object reconstitution with help of a factory

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.

like image 119
theDmi Avatar answered Sep 25 '22 22:09

theDmi