Eyris
How to best manage DTOs in Clean arch DDD project
Im struggling to understand how to manage DTOs and returns types for a scenario in my project.
I have a Domain project with DomainObjects and Repository Interfaces, Service interfaces and so on.
I have a infrastructure layer which implements the repos and services.
An application layer with cqrs, so each use case has a query or command with "dtos" for that. We use mediatr also.
Then I have a API layer which uses fastendpoints to define the endpoints, this calls the Application layer via mediatr.
I understand that I could have treated the API layer as pretty much the Application layer since it will basically just call down to the app layer but there is another legacy project that still needs to work which uses the application layer.
So first question is the fastendpoints uses request/response dtos and the application layer with mediatr uses virtually identical CommandHandler/QueryHandler/Responses. For now I have them as separate Dtos and map but it's a lot of boilerplate. Im considering sharing at least the return type. So lets say I search for Article the application layer would return ArticleDTO which is defined in the application layer or some other "contracts" layer and then the fastendpoint would return that as well. Is that bad or a acceptable compromise to avoid a bunch of mapping?
Second question is, I have a external service to search for Articles, it's implemented in the infrastructure layer with a service Interface implementation in the domain layer. This means I need to know in the domain layer about a DTO from the external service, since it is defined in the interface. It feels wrong to have dtos in the domain layer? Should I not implement the interface in the domain layer at all and talk directly with the infrastructure layer? Or should I just define it as IArticleSearchService<T> ?
15 replies