Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@leonfs
Created January 28, 2014 16:47
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 leonfs/d73a54cbfc84f89035df to your computer and use it in GitHub Desktop.
Save leonfs/d73a54cbfc84f89035df to your computer and use it in GitHub Desktop.
Non Anemic Super Simple Event Sourced
public class Customer : AggregateRoot {
private string _name;
public Customer(Id customerId, string name)
{
Id = customerId;
Name = name;
ApplyEvent(new CustomerCreated(Id, Name)); //Again, passing already modified values;
}
public string Name {
get {
return _name;
}
private set {
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException("Name is required"); //Single place where Name gets validated
_name = value;
}
}
public void ChangeName(string newName) {
this.Name = newName;
ApplyEvent(new CustomerNameChanged(this.Id, Name)); //PASSING THE MODIFIED NAME NOT THE PARAMETER
}
public void When(CustomerNameChanged @event){
this._name = @event.Name; //The event handler only modifies the internal state by passing the Setter that contains the validation
}
}
@yreynhout
Copy link

This is wrong! You should only change state in the When. Your validating property sounds like feature envy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment