When there is “derived data” in a view/projection/query, there should the derivation logic reside?
Martin’s discussion of “Eager Read Derivation” suggests that the “domain model” runs the derivation logic. But the example does not use separate Command/Query models.
http://martinfowler.com/bliki/EagerReadDerivation.html
In the context of CQRS, where should the derivation domain
logic go? I’m assuming in most cases it
should be located on the Query side, but I would like to confirm that? Would it also be standard practice for the
Query model to publish Events for derived results (in Martin's example - BecamePaternalGrandfatherEvent)?
Thank you.
Thanks Greg.
>> Projections generally live outside the domain model.
That statement also brings up a side-bar question –
…for a CQRS/EventSourced system, what is the Domain Model?
My understanding is that Command Model + Query Model(s) == the Domain Model?
Returning to the original question, so for a CQRS/EventSourced system, are you saying that Martin’s suggestion (and yours?) is that derivation logic should reside in the Command Model? In that case the calculation/derivation of PaternalGrandfather would be a result of Command processing and published as an Event to Projections?
What about when PaternalGrandfather is not a concern of the Command Model and is never used by it for maintaining invariants or for any other reason (except to publish it)? And for my system’s particular case we would have many projections with values that are not required by the Command Model. I would be concerned that the Command Model would be polluted with a lot of Query logic noise that is not essential to it (which seemed to be central to purpose of factoring out the Query Model)?
If the Query Model is considered the portion of the Domain Model concerned with Queries, then it would seem that query derivation logic would find a natural home there?
>> (though a bit odd as do you replay them? They potentially have side effects)
Wouldn’t you treat this case as you would with the Command Model – replaying events for the Command Model does not republish any Events?
Thank you.
Thanks Greg.
>> Projections generally live outside the domain model.
That statement also brings up a side-bar question –
…for a CQRS/EventSourced system, what is the Domain Model?
My understanding is that Command Model + Query Model(s) == the Domain Model?
Returning to the original question, so for a CQRS/EventSourced system, are you saying that Martin’s suggestion (and yours?) is that derivation logic should reside in the Command Model? In that case the calculation/derivation of PaternalGrandfather would be a result of Command processing and published as an Event to Projections?
What about when PaternalGrandfather is not a concern of the Command Model and is never used by it for maintaining invariants or for any other reason (except to publish it)? And for my system’s particular case we would have many projections with values that are not required by the Command Model. I would be concerned that the Command Model would be polluted with a lot of Query logic noise that is not essential to it (which seemed to be central to purpose of factoring out the Query Model)?
But i'm not sure the Command Model should be required to generate Events because a particular View requires it. It feels like the Command Model should generate the Event if there is a good reason - in the context of the Command Model. But if it turns out that there is a never ending list of ReadDerivation Events being added to the Command Model, only because Views require them, then it seems like there is forced coupling going on and confused responsibilities? And if the Query Model is a first-class part of the Domain, then why not put the Query logic there, rather than pollute the Command Model with Events that are not essential to it? Seems like the Query Model should be responsible for Query logic?
If you say that it is fine to put the PaternalGrandfather logic in the Query Model, then i'm good (and i have misinterpreted guidance on the topic).