I have started using an onion architecture because I wasn't satisfied with my previous project structure. My main question is, can I return DTOs from the data access layer?
This is my current structure:
/Core
- Application
- Domain
/Infrastructure
- Persistence
- Identity
/WebApi
- WebApi
Quick explanation:
- Application layer is where I have all my business logic
- Domain layer is where all the entities/models are defined
- Persistence layer is where I query the database
- Identity layer is for user management
- WebApi layer is where I have my controllers
The data flow is the following:
Client <- WebApi Layer <- (DTO) Application Layer <- (Entity) Persistence Layer
As of right now, the persistence layer returns the actual database entities which are defined in the domain layer which the application layer transforms to DTOs.
My problem here is that often, the persistence layer actually needs to perform different queries which will make the type different from the entity type. For example, I could join different tables, apply groupings, etc. Thus, I cannot return an entity type.
Am I allowed to return DTOs from the persistence layer (data access layer)? If yes, where do I defined these DTOs? Defining them along with the other DTOs in the application layer and using these DTOs in the persistence layer would make it dependend on the application layer (not so great in my opinion since we want to minimize couplings). Another option would be to create them in the domain layer along with the entities (probably the better approach)? For consitency's sake, should I only return DTOs from the application layer or is it fine to return entity types as well as DTOs?
Lots of questions that I couldn't find. Just trying to become a better programmer:)