Search code examples
c#onion-architectureef-core-2.1

Dtos in Onion architecture


Something is not right with my structure. I have a webapi(project with all controllers) And there is a reference to my IService layer in core project (IServices classes in core) here is also the core entities(domain entities).

From the wepapi project I call my Iservices and I get my response with objects from domain entities.

And now my question Should the services return and accept Domain entities(plain customer) or DTO:s(customerForUpdate,customerForCreate).

If I return Domain entities I get many lookups against several services to create my DTO. example customer with clothdetails(Customer is one service and Cloth is another). This makes Ef core to flip beacuse each service method calls a save in unitOfWork. Example I call clothservice to get on value to use for customer and the update clothservice again.

If I return Dto:s from my IServices I need to add a reference from the service project to the DTO(in my infra project). But in this case I can call other services with in this project and create my dto. and just call save once when I am finished.

Or is there another way to just call save once per webapi call or I have missunderstand the dtos this kind of dtos should be places in the core instead because they are not real dto:s?


Solution

  • according to the onion architecture, all dependencies should be on the domain layer, and each layer with its own higher layer can communicate. Now, the point is that the domain layer should not have access to the domain layer and other layers like infrastructure at all, and only in the web service layer or UI should only see the application layer, now your problem is to fix it. Make a layer separate from the application layer in which the application layer interfaces are named (Application.Contracts) In this layer, in addition to the interfaces you define for the application layer, you define the DTOs on the same layer. Then, with this, you define the interface you want to define for the application layer, it only works with those (DTOs) so that the web service layer or the UI layer does not have a domain Layer.

    -Application
        StudentService
        TeacherService
    
    -Application.Contracts
              --Interfaces
                  IStudentService
                  ITeacherService
              --DTO
                  StudentListDto
                  TeacherDto
    -UI Or Web service layer
     //Only can work with Application.Contracts