I know there are thousands articles about it and I read many of them but I am still not sure what to do it my specific case.
I am writing a .NET Core 5 application and have 3 project layers:
- API - REST, interacts with the client-side code, gets & returns DTOs.
- BLL - Business logic - most code is here.
- DAL - Repository pattern => Calls the SQL Server database. Works with entities.
I am using entities in my DAL layer and map them later to DTO models which are returned in the API to the API caller.
If I write in Google "In which layer to map from entity to DTO?" I get the answer:
In that case, the service layer maps domain entities to data contracts (DTO's).
The problem is that I don't have a service layer and I don't want to create it.
The question is, in which layer in my layers structure should I do the mapping from entity to DTO?
In my structure I guess I can do it in 2 places (I am not sure which one is better and why):
In the BLL - instead of returning an entity, I can map it to a DTO (using an
AutoMapper
) and return the DTO to the API layer.In the API - Receiving an Entity from the BLL and then mapping it to a DTO (using an
AutoMapper
orResultFilter
) and returning it to the API caller.