Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Trasfer Objects Between Layers in Domain Driven Design

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?

like image 397
barteloma Avatar asked Nov 02 '13 20:11

barteloma


2 Answers

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

like image 81
Ilya Palkin Avatar answered Oct 02 '22 19:10

Ilya Palkin


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.

like image 28
Szymon Avatar answered Oct 02 '22 17:10

Szymon