CQRS Commands with common operations - duplicated code

937 Views Asked by At

Lets assume that we have several commands sharing common logic. For example i have Document that have several states. We have mutating operation that is possible on some states, but some of the logic is different depending on its state. Making one Command using If statements for more than 3 states is confusing. Its better for each operation make separate command, but what to do with common logic ?

We have to fetch data from DB, validate, generate some side documents, write to audit table and other stuff. So it looks like it should be common place and making meaningless Helper class is the worst option. I assume that this operations can / don't require transaction.

I have read http://scrapbook.qujck.com/holistic-abstractions-take-2/ and CQRS code duplication in commands . I am looking for other options.

1

There are 1 best solutions below

3
On BEST ANSWER

@Redgood, If I am not mistaken, some of the things you describe belongs to the Domain.

Make sure that your business/domain "logic" is not spilling outside the domain. I do use ICommand interfaces to mark my commands and I do have some logic in there but only for data type validations or other types of integrity checks.

Keep it at that. From a Command perspective all you care about is that the data contained in the command is Good. That's it. So, make sure that all your methods in your Command are just to enforce that integrity.