-
-
Save leonfs/d73a54cbfc84f89035df to your computer and use it in GitHub Desktop.
Non Anemic Super Simple Event Sourced
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is wrong! You should only change state in the When. Your validating property sounds like feature envy.