In context of CQRS and DDD how would you handle slightly varying use cases?

342 Views Asked by At

I could have a command for each variation or I could have one command and handle variations within it.

To illustrate, I have three use cases (or more), "CommentOnPhoto", "CommentOnWall", "CommentOnArticle". All of them internally use the same commenting system. The difference between three is:

  • Different policy needs to be checked (CommentOnPhotoPolicy, CommentOnWallPolicy, CommentOnArticlePolicy)
  • Different aggregate has to be pulled from repository so that we could call, photo.attachComment(comment), article.attachComment(comment), etc.

If going the route of one command for each variation then should each command inherit from some base CommentOnSomething command since they are all very similar?

If going the route of one mother command for all use cases then we will have to make use of Service Locator to pull needed Policies and Repositories on demand (we don't want to inject 10+ dependencies for each use case only to use one). Is this acceptable if we make use of abstract factories and such to hide away service locator use? Meaning we have CommentPolicyFactory and CommentableFactory which use Service Locator internally to deliver the right policy and the right aggregate.

1

There are 1 best solutions below

2
On BEST ANSWER

One command per variation.

I would first focus on creating a very generic "commenting" component (as its own project, decoupled from your core domain). Once you have it, then your commands CommentOnPhoto, CommentsOnWall and others will become very simple with just a few lines of code.