Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In CQRS pattern, should work go in domain services or command handlers

Tags:

c#

cqrs

Should domain services inject other domain services and do work between each other and have the commandhandler be dumb. OR, should the domain services be dumb (only be used to interface the repository barrier) and the majority of work be done in commandhandler's? What is best practices here...

like image 474
user3689167 Avatar asked Dec 10 '15 15:12

user3689167


2 Answers

I would say add ALL business logic inside domain objects (and also domain services if the functionality doesn't fit into an object) and use commandhandlers for things like:

  • instantiate domain objects and run methods on them,
  • run methods on domain services,
  • provide dependencies to domain objects,
  • manage database transactions,
  • ...

You can check out the onion architecture, I guess your domainservices are inside Domain Model and commandhandlers inside Application Services.

like image 86
sventevit Avatar answered Sep 20 '22 06:09

sventevit


CommandHandlers could be viewed as the use case's of your application, therefore it is their function to orchestrate the access to repositories, domain services and domain models.

If you start to inject domain services inside each other you start to couple them and lose the single responsibility of each one.

My answer is to let the commandHandler deal with the execution of the domains services and models needed in your use case, this way you can freely compose any new command without having to deal with domain services that are full of logic that belongs to a use case, and not entirely to the domain itself.

like image 40
gutors Avatar answered Sep 24 '22 06:09

gutors