Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

yreynhout commented Jan 28, 2014

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