asp.net-coredomain-driven-designlayerdtoonion-architecture

DTO definition in presentation layer and application layer


I am learning about DDD and I would like to say what is the correct way to implement DTO using DDD principles ?

In my onion architecture I implement a first DTO definition in presentation layer to map datas between RabbitMQViewModel(presentation layer) and RabbitMQModelsResultDTO (application layer).

And I implement a second DTO definition between application layer (RabbitMQModelsResultDTO) and domain layer (entity LogRabbitMQ).

However, I am not sure if it's a good way to implement two DTO definition ?

Light description of my DTO definition in presentation layer :

CreateMap<RabbitMQViewModel, RabbitMQModelsResultDTO>().ReverseMap();

and application layer :

 CreateMap<Domain.Entities.LogRabbitMQ, RabbitMQModelsDTO>().ReverseMap();

It's a screen of my project architecture :

enter image description here


Solution

  • Translating presentation object (RabbitMQViewModel) into application object (RabbitMQModelsResultDTO) then application object into domain object (LogRabbitMQ) is considered to be a good practice in the DDD world.

    However, it is pointless to make anemic, flat translations: it would be much easier to use the same object through all three layers (in which case DDD becomes irrelevant).

    Here is how it can be done in the DDD way:

    1. Application object reflects domain context properties, namely value objects, into which presentation object's primitives are translated.
    2. Domain object is a "smart" object, implemented with all OOP goodies in order to keep it consistent all the time, and this object is being constructed using application object's properties.

    As for the term "DTO", it should be used primarily for presentation object. It is less appropriate to use it in the context of application/domain objects, which are considered to be business objects.