Should the Domain layer depend on Infrastructure?

Asked

Viewed 516 times

4

I’m reading Evans' book on D.O.D., and I came across the following quote::

The Infra layer does not perform any action on the domain layer, because it is below it, because it is below it it should not have specific knowledge about the domain it is serving.

Application and Domain Layers call services provided by the Infrastructure layer.

Remembering that I am a beginner with DDD, and from what I had read earlier the domain layer should not know any other layer, could someone please show some implementation closer to what Evans meant at this point or correct me if I got it wrong.

  • Felipe, put the complete reference of the book or a link somewhere with the reference.

  • 1

    Not knowing a layer doesn’t mean not relying on it. I can depend on someone without personally knowing my benefactor, I just know what he does for me and I know how to enjoy his services. For example: the government is my benefactor and provides me health when I need it; to enjoy it, I just need to know how to get to the hospital. The care workflow, how doctors are paid, how the examination machines got there and how they work... are hospital implementation details that I don’t know and that can change without affecting me and without I even taking notice that they have changed.

2 answers

5


The Domain layer must depend on Infrastructure?

Yes, the Domain layer depends on the Infrastructure layer to perform data persistence in the database.

However, what the DDD guides is that the Domain classes not implemented with direct references the Infrastructure layer.

That is, in your project responsible for the Domain (related to the business rules) do not put database references, Nhibernate, . Net, threads, HTML5, javascript, etc.

Instead use Services external that can be reused at various points in the system, such as:

  1. In the Domain create an interface called Iclienteservicorepositorio containing signature methods to Save, Delete, Update and Recover data in the database
  2. In the Infrastructure project/layer, implement Iclienteservicorepositorio in a Clientservicorepositorio class, making the Infrastructure have the implementation of how to Save, Delete, Update and Recover in the Client object database using the repositories

With the Clienteservicorepositorio class in your Infrastructure layer implementing the Cyclic Interface defined in the Domain, now let’s imagine a scenario where your Domain will depend on Infrastructure services...

Assuming I need to restrict debtor client exclusion, I can include this rule in the Domain and request Infrastructure layer methods, example:

  1. Create a class in the Customer domain
  2. Use Dependency Injection/Control Inversion to maintain minimum coupling between the Dominino and Infrastructure layers
public class ClienteServico
{
    private readonly IClienteServicoRepositorio _clienteServicoRepositorio;

    public ClienteServico(IClienteServicoRepositorio clienteServicoRepositorio)
    {
       _clienteServicoRepositorio = clienteServicoRepositorio;
    }

    public ActionResult ExcluirCliente(Cliente cliente)
    {
       if(_clienteServicoRepositorio.ClienteDevedor(Cliente.Id))
       {
           RetornarMensagemExclusaoClienteDevedor();
       }
       //else ...
    }
}

So we have an example where the Domain layer calls services provided by the Infrastructure layer with minimal coupling between layers.

  • Good answer. I just wouldn’t limit my concerns to "direct references" or use of interfaces because the concept is a little deeper. For example, I feel comfortable having in my entities the Hibernate annotations that map the data to tables in the database. So I have in the domain code two different concerns (business and DB), but, for various reasons that would give a long text, I’m still separating the concepts and I’m still enjoying the best of the DDD.

0

The domain layer should not be 'married' to any other layer, but it needs to call functions to CRUD in the persistence layer.

The idea of the domain layer not knowing the persistence layer (or any other layer) is to avoid 'marrying' it with a specific implementation of the persistence layer. Understand: make calls, yes, but through interfaces.

The above quote is from another concept. The infra layer cannot make calls in the domain layer. Neither by interface, nor without. Who makes the calls, who orchestrates everything is the domain layer.

So the two ideas that you have exposed are correct, but they deal with different things (although they follow the same philosophy of separation of concepts).


Examples: Say you have the class UsersBL for business rules and class UsersRepository for persistence.

The class UsersBL will need to call the class UsersRepository data creation, search, data modification.

The first concept that you quoted says the class UsersRepository cannot contain calls to the class UsersBL. For example, it might seem useful to use a validation function that is already implemented and tested in the class UsersBL, but this is wrong.

The second concept says the class UsersBL must make calls in class UsersRepository, of course, but should not do this through instances of the class UsersRepository. The class in the domain must use interfaces.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.