Uniqueness validation when using CQRS and Event sourcing?

Eventual Consistency and Set Validation by Greg Young.

Up vote 1 down vote favorite share g+ share fb share tw.

I'm trying to implement my own CQRS infrastructure with Event Sourcing to learn it better. As a sample project I'm imeplementing a blog enginge, I know it might not be a perfect fit but I just want to work on something real. The problem I've come to know is validation.

Every post have a shortUrl, and the shortUrl should be unique, but where should I put this validation in the domain? I know that I will have that validation before I even send the command by reading from my read store to check if it is valid when creating a create post command or update post command. I can think of two "solutions".

Have a Blog aggregate that keep tracks of all blog related settings and also references to all the posts. But the problem with this in my eyes is that I have to handle communication between aggregates in that scenario as well as every time I need to validate the uniqueness of a shortUrl I need to read all the events from the event store to create all the posts and that seems to complicated. The second alternative I have is when the event gets fired and my event handler that creates the read model fires a duplicate short url event when it notice that it will have two short urls that points to different posts.

Is it valid to have the read model to fire events when it detects errors? Are there any more alternatives. Note that I know that my domain might not be the best fit for cqrs and DDD, but I'm doing this to learn in a small domain.

Validation domain-driven-design cqrs link|improve this question asked Feb 26 at 17:26Tomas Jansson3,735517 74% accept rate.

Thank you for showing me the post, I've totally missed it. So basically the suggested solution is my number 2, or some variant of it. – Tomas Jansson Feb 26 at 19:47.

It depends what 'the business' wants to happen. If you want the client (creator of the commands) to be responsible for choosing a short URL, it should have a read store that it verifies the uniqueness of it from. When the user types in a short URL, the view should check that the short URL is unique and present a validation error if it is not.

Whenever a post is saved, an event will publish the updated info (including the short URL) which keeps the read store in sync.

That is what I mean with "I know that I will have that validation before I even send the command by reading from my read store to check if it is valid when creating a create post command or update post command. " But it is no guarantee that it is still unique when it comes down to the domain. This is very unlikely to happen when creating blog posts, but I think it is an interesting scenario that I still need to figure out how to solve when using CQRS.

What I mean is how do I handle the check in the domain? – Tomas Jansson Feb 26 at 17:42 In the system I'm working on, there is a unique constraint in the data store so that if the edge case of two simultaneous saves of different entities with a conflicting field happens, the constraint causes an exception to bubble up to the UI where it can be manually fixed. Unless the domain logic can rectify that scenario, it must be dealt with by a human.

– RyanR Feb 26 at 20:32.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions