views:

1178

answers:

4

We are trying out CQRS. We have a validation situation where a CustomerService (domain service) needs to know whether or not a Customer exists. Customers are unique by their email address. Our Customer repository (a generic repository) only has Get(id) and Add(customer). How should the CustomerService find out if the Customer exists?

+2  A: 

Take a look at this blog post: Set based validation in the CQRS Architecture.

It addresses this very issue. It's a complex issue to deal with in CQRS. What Bjarte is suggesting is to query the reporting database for existing Customer e-mail addresses and issue a Compensating Command (such as CustomerEmailAddressIsNotUniqueCompensatingCommand) back to the Domain Model if an e-mail address was found. You can then fire off appropriate events, which may include an UndoCustomerCreationEvent.

Read through the comments on the above blog post for alternative ideas.

Adam D. suggests in a comment that validation is a domain concern. As a result, you can store ReservedEmailAddresses in a service that facilitates customer creation and is hydrated by events in your event store.

I'm not sure there's an easy solution to this problem that feels completely clean. Let me know what you come up with!

Good luck!

Kevin Swiber
@JontyMC any news on this?
Arnis L.
As mentioned in the other answer, you shouldn't perform queries against your view model as a part of processing commands. If anything, those queries should be performed by the client before sending the command, choosing not to send the command based on the results.
Udi Dahan
A: 

Just wanted to add that there's a discussion about this.

Arnis L.
+3  A: 

This issues does not have to be that complex:

  1. Check your reporting store for customer uniqueness before submitting the UpdateCustomer command.
  2. Add a constraint to your DB for uniqueness on email address. When executing the command, handle the exception and send a notification to the user using a reply channel. (hences never firing CustomerUpdated events to the reporting store.

Use the database for what it's good for and don't get hung up on ORM limitations.

Mark Lindell
+2  A: 

This post by Udi Dahan http://www.udidahan.com/2009/12/09/clarified-cqrs/ contains the following paragraph:

"Also, we shouldn’t need to access the query store to process commands – any state that is needed should be managed by the autonomous component – that’s part of the meaning of autonomy."

I belive Udi suggested simply adding a unique constraint to the database.

But if you don't feel like doing that, based on the statement above, I would suggest just adding the "ByEmail" method to the repository and be done with it - but then again Udi would probably have a better suggestion..

t0PPy