Command Validation in DDD with CQRS

后端 未结 3 2068
情歌与酒
情歌与酒 2021-02-01 06:39

I am learning DDD and making use of the CQRS pattern. I don\'t understand how to validate business rules in a command handler without reading from the data store.

For

3条回答
  •  被撕碎了的回忆
    2021-02-01 07:11

    I just asked the exact same question from a knowledgeable friend of mine and his answer was that it is OK to do this validation inside the command handler (in my case inside an akka persistent actor that interprets commands and writes events to the journal).

    However if that is not possible for performance reasons (because processing validation inside a persistent actor would block the actor and that would be a scalability bottleneck for the whole app) then optimistic locking (OCC) can be used.

    In other words the validation can be performed in an other actor (lets call it the validator actor - which is not in the persistent actor) , this will not block the persistent actor but it might happen that the data that is used for the validation has changed in the persistent actor while the validation was running in the validator actor) .

    If the validator actor returns with an OK and all the data that has been used for the validation is still the same in the persistent actor (has the same version - version as in OCC) as it has been at the point when the command arrived to the command handler (persistent actor) then the command is accepted by the persistent actor , otherwise the validation needs to be resubmitted for re-evaluation to the validator actor.

提交回复
热议问题