9
votes

enter image description here

Domain layers are communicating the other layers via Data Transfer Objects(DTOs). I confused about DTOs.

DTO 1 is between Domain and Presentation Layer.

DTO 2 is between Domain and Data Layer.

Should I create two different DTO objects between layers or only one DTO. Which is the proffesional way?

4
The Domain Layer should know nothing about your Data Layer also, if it is Data Access Layer.Ilya Palkin

4 Answers

6
votes

Lets go through all your layers:

  • Data Access Layer (DAL). It is used in order to get data from database (DB).

Usually it knows about Domain Entities and Domain Layer.

The DAL can return either Domain Entities or DTOs (DB oriented data structures). These DTOs or Domain Entities can be used in order to build DTOs of Presentation Layer (view models) if it is needed.

Domain Entities usually are heavy and require data mappers or any ORM. I prefer working with Domain Entities, map them and avoid other DTOs. Otherwise DTOs should be mapped also.

  • Domain Layer (Domain model). It is used in order to represent Business entities and their behaviour, business rules, pure business logic.

Domain Layer should know nothing about the way the entities are stored somewhere (e.g. in DB). It can have its own DTOs which can be results of refactoring Introduce Parameter Object.

  • Presentation Layer (UI). It is used in order to present UI to users.

It should know about Data Access Layer to load data from DB and about Domain Layer to have access to its business logic.

It can have its own DTOs - view models, which are user interface friendly representation of Domain Entities or DB friendly DTOs. It is responsibility of Presentation Layer to know about view models.

If you are going to have only one presentation your Application Infrastructure can be implemented as part of presentation layer also, but usually it is a separate Application Layer.

enter image description here

2
votes

It really depends on your your specific needs.

In a general sense, you should create 2 sets of DTO's. This allows for better de-coupling of different layers and makes the architecture of your system more flexible. The specific reasons or cases where it is needed are for example:

  • Sharing DTO's may not be possible, e.g. because there are differences in technologies used, e.g. a web service and data layer are written in C# and the presentation layer is written in Java.
  • DTO's are not necessarily the same, i.e. your DTO for the interaction with the database layer may be modelled on the database structure but you may expose it differently to the presentation layer.

Having said that, if you can live with the limitations of having one set of DTO's, you can share them if it suits your needs as it produces less code to write and maintain.

1
votes

Your image shows two DTO object named DTO1 and DTO2.

DTO1 is sharing data between presentation layer and domian layer. You can call them as ViewModel classes.

DTO2 is sharing data between domain and data layer. You can call them as Data Transfer Objects (DTOs).

So You can use two different transfer objects.

0
votes

If we assume that you use two separated DTOs (DTO1 and DTO2),
answer is simple:

You must create two separate DTO in this case: DTO1 and DTO2.
Even if they are identical, they should be implemented as separated classes.

It is because DTO1 is created in Domain Layer but DTO2 is created in Data Layer (according to yor picture).

Note that in some solutions, two DTOs are not used - sometimes, there is only one DTO.