2

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.

Community
  • 1
  • 1
RedgoodBreaker
  • 310
  • 1
  • 10

1 Answers1

2

@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.

Pepito Fernandez
  • 2,352
  • 6
  • 32
  • 47
  • You mean if i have to check that user have privileges to do "Update" on that _Document_ i should check it before issuing command. Next all the data which is required to run that command should be fetched before and passed as parameters ? So potentially all duplicated code should be outside command ? – RedgoodBreaker Jan 06 '17 at 11:51
  • Well, you could do a good some security checks at the Controller/Action level. This is easy to do in web api. You need to create your own "validation" logic, etc. But this is not a lot of work. Also, if you pass that first check and need some extra "business security check" it can be done in the CommandHandler. Also, I prefer to do it in Decorators classes before calling the CommandHandler. Think of an onion, being your command handler closer to the center. Your 'security check' will be almost at the outter layers. Decorators may be the solution for you. – Pepito Fernandez Jan 06 '17 at 17:23
  • have you seen good examples of what you write ? not counting links i have provided ? – RedgoodBreaker Jan 30 '17 at 11:46