Skip to content

Instantly share code, notes, and snippets.

@ToJans
Created November 15, 2011 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ToJans/1366502 to your computer and use it in GitHub Desktop.
Save ToJans/1366502 to your computer and use it in GitHub Desktop.
Uniqueness in CQRS
When a user registers with a unique username
Then that registration should be approved
Given a user registration with a certain username was approved
When a new user registers with the same username
Then that registration should be rejected
When a user registers with a certain username
Then that registration should be pending
Given a user registration with a certain username was approved
When a new user registers with the same username
Then that registration should be pending
Given a user registration with a certain username is pending
When the information is processed
Then it will approve the registration with that username
Given a user registration with a certain username has been approved
Given a user registration with the same username is pending
When the information is processed
Then it will reject the registration with that username
Given [something happened]
When [all the sagas have processed the events]
Then [a command should be issued]
public class UserRegistrationSaga
{
IIndexStore indexstore;
IRunCommand bus;
public UserRegistrationSaga(IRunCommand bus,IIndexStore indexstore )
{
this.indexstore= indexstore;
this.bus = bus;
}
public void OnUserRegistrationPending(string UserRegistrationId,string Username)
{
if (indexstore.ContainsValue<string>("RegistrationUsername",Username))
{
bus.RunCommand(new RejectUserRegistration { UserRegistrationId = UserRegistrationId,Reason = "Username "+Username+" is allready in use"});
}
else
{
indexstore.Add<string>("RegistrationUsername",Username);
bus.RunCommand(new ApproveUserRegistration { UserRegistrationId = UserRegistrationId, Username=Username });
}
}
public void OnUserRegistrationApproved(string UserRegistrationId,string Username)
{
if (!indexstore.ContainsValue<string>("RegistrationUsername",Username))
{
indexstore.Add<string>("RegistrationUsername",Username);
}
}
public void OnUserRegistrationRejected(string UserRegistrationId,string Username)
{
if (indexstore.ContainsValue<string>("RegistrationUsername",Username))
{
indexstore.Remove<string>("RegistrationUsername",Username);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment