Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 765
Emad Ali Avatar asked Feb 18 '19 13:02

Emad Ali


People also ask

Where do you put DTOs in project?

DTOs are inside Domain Layer... 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.

Where should DTOs be defined?

Define the DTO to the layer where the source of the values comes from. Relative to OP's question: place the DTO in the Application Service Layer. DTO is an output of that layer, it makes sense if you define it there. Don't put your DTO in the Domain Layer.

Should repositories return DTOs?

Short answer: No. Long answer: repository is responsible for turning persisted data back to entities (models) and vice versa.

How many layers are there in clean architecture?

The layers are the main core of a clean architecture. In our app, we will use three layers: presentation, domain, and model. Each layer should be separated and shouldn't need to know about other layers.


2 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 :)

like image 134
francisfai Avatar answered Oct 10 '22 01:10

francisfai


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:

MVC example

Razor Pages 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.

like image 37
Jimmy Bogard Avatar answered Oct 10 '22 03:10

Jimmy Bogard