Search code examples
c#automapperclean-architecture

Where i should put my DTOs in clean architecture?


enter image description here

Need to implement the clean architecture and struggling with DTO concept. As I understand, i can't use my domain objects in presentation layer (asp mvc) instead i should use DTO or viewmodel. I'm not sure where these DTOs should go. I have two projects as my core layer (Domain, Application).Domain holds my entities ex:'Post' + Repository interfaces ex:'IPostRepository' . Application holds logic ex:'IPostManager' + 'PostManager'. Where DTOs and DTOs mapping to Entities should happen ? Asp MVC, Application or Domain?


Solution

  • As we already know, Dtos can be of different types that does not have any behaviour and are only used for transporting data eg a Model in the MVC pattern or a class that probably is named with a suffix 'classNameDto'

    In your case, it really depends on what context you are using the Application layer. Some Devs understand that 'Application Services' are more specific to the application, meaning they are tied closely to the UI.

    If this is the case then, this is a good place to have the Dtos where the data is mapped to and from the domain model.

    Else if the mapping is done at the Web layer then Dtos need to go there.

    In simple terms as @Jimmy Bogard said "Put the classes close to where they're actually used."

    I would also suggest to readup more on the clean architecture and see if you are headed in the right direction.

    Hope this helps :)