Help with clean architecture
I wondered how I could return the DTO without breaking anything.
Here is the code from the service where I access the entity.
And here in the controller
I could create a mapper right here in the controller, but is that a good approach?
If I'm doing anything, it's purely because I don't like the way it glows in the swager.
Here:
and the DTO that should replace the Todo entity itself looks virtually identical to it, but the point is that I don't think it's a good idea to expose the entity.

2 Replies
If you need an API model / dto to map to, I'd totally do that in the controller
That's definitely the right layer for that
Where else should it fit
In clean architecture each layer has it's own abstractions and models. And it is recommended to map each model from layer to layer
For example if you have a ToDo model in application layer, the abstraction to retrive it (some repository interface) and some use case flow
here as you see I am getting a
ToDo
model from abstraction IToDoRepository
which defined in same application layer. How you implement this abstraction is the problem of infrastructure layer. You can store it in your database not the same ToDo
as previously, but with some additional props if you wish to. These two layers separated and only infrastructure responsible for mapping to application
Also my code lacks of Domain and Presentation layers abstraction, but the idea that mapping should be only one way. Deeper layers don't know anything about previous layers
and also clean architecture is not very good with EFCore, you gonna end up recreating entire EFCore on your infrastructure layer with repositories, since DbSet<T>
is already the one
I personally like to create a Map method (or extension method) on model itself, so it can produce deeper layer model. And this should be done on your controller