What is the difference between Command + CommandHandler and Service?

Having Commands gives you the benefits of the good old Command pattern:

  • you can parameterize an object, e.g. a UI element, with a Command to perform
  • you can store a Command and execute it later, e.g. in a queue or a transaction log
  • you can track which Commands you executed, giving you a foundation for implementing undo

If your services were large, each with many complex methods (and if the methods weren’t complex you probably shouldn’t be using DDD or CQRS), then moving each method into a Command Handler might improve your application by making it more composable, easier to test, etc. No doubt it is common for people who refactor straight from big services to Commands/Command Handlers to regard this as a benefit of the latter pattern. But you could get the same benefit by decomposing large services into smaller ones (as suggested by the very specific service in your example), so strictly speaking that isn’t a difference between services and Commands/Command Handlers.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)