My question is related to CQRS (Command and Query Responsibility Segregation) and mechanism that builds read model (views). As far as I understand read model is built by event handlers. These handlers (also called denormalizers) receive domain events and use these events to build different views of data.
Specific event carries information about change done in domain model. I think that this information is not sufficient in some cases to build view - i.e. not changed fields, not changed entities are missing in such event etc.
So my question is:
Is it allowed that denormalizer responsible for building read model accesses not only events but also:
What is your opinion about allowed dependencies for event handlers (denormalizers)?
edit: Just added simple example to the question above:
Suppose the following model:
AR: ProductOffering * name * description * category * price
AR: Customer * name * type * method: purchaseProduct(productOffering) that emits ProductPurchasedByCustomer event
entity: ProductInstance * customer * productOffering
event: ProductPurchasedByCustomer * customerId * productOfferingId
view: ProductInventoryView * customerId * productOfferingId * customerType * productOfferingName * productOfferingCategory * price
How to build ProductInventoryView using only ProductPurchasedByCustomer event? How can I write denormalizer to put into view information about customerType, productOfferingName etc? Should I lookup additional information about customerType and productOfferingName from different views?
CQRS is implemented by a separation of responsibilities between commands and queries, and event sourcing is implemented by using the sequence of events to track changes in data.
CQRS is a popular architecture pattern because it addresses a common problem to most enterprise applications. Separating write behavior from read behavior, which the essence of the CQRS architectural pattern, provides stability and scalability to enterprise applications while also improving overall performance.
The CQRS application pattern is frequently associated with event sourcing: when doing event sourcing and domain driven design, we event source the aggregates or root entities.
The CQRS design pattern raises the idea of separating writing and reading data from object to system level. This means, for example, that an application has not only one but two APIs to address it: While one API is used for writing data, the other is used for reading.
If an event consumer requires more information, it's okay for an event producer to provide that required information, eventhough it hasn't changed. Eventsourcing should not be treated like an ORM. Mind you things can become tricky with regard to how you get that information, but lets not complicate things (yet). The eventhandlers can use the state (data) of the read model if it's not provided by the event. But doing so requires you to know if and which data you can use. It requires you to couple time to the data. Messages/events are likely to be processed out of order. So, providing information in the event, pertaining to the event, is much easier. Try do that before doing anything else. Don't try to access your domain from the eventhandlers specific to your read model, that's just nasty coupling. One last thing, it's okay for aggregates to copy data from other aggregates to provide the data you need in your events. Mind you that from then on you'll have to start thinking on how to keep that data fresh in the aggregates that copied it in the first place. Probably confused you some more ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With