gautema / CQRSlite

A lightweight framework to help creating CQRS and Eventsourcing applications in C#
Other
1.1k stars 266 forks source link

What is a proper place for cross-aggregate validation #83

Closed kolipka closed 5 years ago

kolipka commented 5 years ago

I have Project aggregate with ProjectCode property provided by User. I must ensure that it is unique among all projects. Should I do it in CommandHandler using Query to ReadModel? Or should I create AllProjects aggregate that will hold list of project codes and throw on CreateProjectCommand attempt?

gautema commented 5 years ago

Hi.

As long as you keep everything synchronous it doesn't matter that much, but querying the read model is probably the simplest.

A good idea would be to query the read side either from the client before sending the command as well as do a check in the command handler. If you search for CQRS and uniqueness, you will find a lot of discussion around this, and you will see that there is no clear answer. But as long as everything is synchronous my suggestion is to keep it simple and query the read model. In an asynchronous context you will need to store the ProjectCodes in another aggregate or some place you can query and save in the same transaction. If this is not possible there are other more complex ways as well, involving compensating actions or other techniques.

kolipka commented 5 years ago

Thank you! Awesome work btw!