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?
-
Domain and Application shouldn't know about presentation details. – FCin Feb 18 '19 at 13:16
-
@FCin . So my DTOs and mapping should reside in MVC ?? – Emad Ali Feb 18 '19 at 13:18
-
1Mapping specific to a website should be done inside that website. Imagine you have a website and a WPF application, both using the same Domain and Application layers. If you map them in Application then you automatically restrict yourself to have the same DTO for both applications. Instead if you map your model inside each presentation, you can have different DTO's for the same part of application. – FCin Feb 18 '19 at 13:20
-
@FCin .Thanks. I got it. Another question about having the 'PostManager' - which take care off CRUD operation on the Post entity - inside the Application project instead of Domain project. Is that a good or bad design?? – Emad Ali Feb 18 '19 at 13:25
-
What exactly are your DTOs used for? In the context of clean architecture (https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html) there are many "DTOs" called "Requests, RequestModel, ReponseModel, Response" ... – plainionist Feb 23 '19 at 07:12
3 Answers
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 :)

- 106
- 1
- 5
-
The more I read about CA, the more confused I become. I saw people recommending putting DTOs inside Infrastructure, others consider it is a part of the presentation layer. Here - https://www.learmoreseekmore.com/2021/05/clean-architecture-in-dotnet5-application.html DTOs are inside Domain Layer... – Dorin Baba Sep 26 '21 at 17:52
-
I think it really depends on where you intend to use those DTOs. For example, if the DTOs will mainly used by the frontend that relies on some private APIs then I would put the DTOs into the UI layer as other parts of the system do not need to know about those DTOs. However, if those DTOs are used by say another micro services such as a public API then I would move them into the SharedKernel so they are shared between the web frontend and the API services. – GETah Jun 01 '22 at 20:06
This looks like a single application. So in that case, I place my DTOs as close to where they are used as possible. If it's MVC, then my DTOs are right next to my views:
- Views
- Account
- Index.cshtml
- IndexModel.cs
Or if it's Razor Pages, then the DTOs are simply inner classes. See my ContosoUniversity examples for a working example:
It's not "clean architecture" but "vertical slice architecture" but that shouldn't matter. Put the classes close to where they're actually used.

- 26,045
- 5
- 74
- 69
In your case,the DTO is accessed by both Presentation & Domain. so better to have it Infrastructure and refer it from there.

- 374
- 1
- 4
- 11