Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity vs DTO in CQRS

In a normal DDD project I expect the Entities retrieved from a repository to be sent from the Domain Layer to the Application Layer as a DTO.

It seems that one reason of CQRS to exist is that the queries needed by the application layer (mostly read operations) are different from the ones needed by the domain layer. So it seems that even a query result of the same object might be different among layers.

Within my domain layer I am already mapping query results into domain entities. I am confused about some CQRS examples mapping query results straight into DTOs skipping their matching entities.

Suppose that a database returns:

{"person": {
  "id:": 5323423,
  "name": "John",
  "family_name": "Smith"
  ...
}}

However the entity layout maps family name as surname:

class Person
{
   Identity id;
   String name;
   String surname;
}

If this happens in some CQRS examples I have seen, the extracted DTO will look different, causing a conflict when matching an entity with its DTO. How are these conflicts resolved? It looks to me that any DTO (related with an Entity) should always be generated from its Entity. However in that case, the freedom to perform different type of queries in the Application Layer is lost.

like image 904
SystematicFrank Avatar asked Oct 27 '13 16:10

SystematicFrank


People also ask

What is the difference between DTO and entity?

Short answer: Entities may be part of a business domain. Thus, they can implement behavior and be applied to different use cases within the domain. DTOs are used only to transfer data from one processor context to another.

What is the difference between DTO and domain object?

DTOs are mostly used out of the hexagon, in the delivery mechanism. On the other hand domain models should promote a good object oriented design with proper encapsulation. They belong in the hexagon. However it's not always clear where to use a DTO and a domain model, where and how to convert one into the other.

What is difference between model and DTO?

Do you know the difference between data transfer objects and view models? Data Transfer Objects (DTOs) and View Models (VMs) are not the same concept! The main difference is that while VMs can encapsulate behaviour, DTOs do not. The purpose of a DTO is the transfer of data from one part of an application to another.

When should DTOs be used?

4. When to Use It? DTOs come in handy in systems with remote calls, as they help to reduce the number of them. DTOs also help when the domain model is composed of many different objects and the presentation model needs all their data at once, or they can even reduce roundtrip between client and server.


2 Answers

Totally aggreed with @MikeSW, just add an graph(borrowed from axon-framework) to explain the architecture.

enter image description here

The readmodel is driven by query use-case, you could consider it as the output of the command operations.

like image 119
Yugang Zhou Avatar answered Sep 21 '22 12:09

Yugang Zhou


The point is you don't map entities to DTOs. The DTOs are defined by what that specific context needs and that becomes the query/read model. When an entity is updated an event handler will use it to update the read model too.

So basically the read model is generated and updated from all needed entities (1 ore more), usually in an incremental manner.

like image 32
MikeSW Avatar answered Sep 22 '22 12:09

MikeSW