سؤال

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.

هل كانت مفيدة؟

المحلول 2

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.

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top