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:
- In the Domain create an interface called Iclienteservicorepositorio containing signature methods to Save, Delete, Update and Recover data in the database
- 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:
- Create a class in the Customer domain
- 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.
Felipe, put the complete reference of the book or a link somewhere with the reference.
– RSinohara
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.
– Caffé