Alternatives to Udi's domain events
Almost four years ago Udi Dahan introduced an elegant technique that allows you to have your domain model dispatch events without injecting a dispatcher into the model - keeping your model focused on the business at hand. This works by having a static DomainEvents class which dispatches raised events. This customer aggregate raises an event when a customer moves to a new address. public class Customer { private readonly string _id; private Address _address; private Name _name; public Customer(string id, Name name, Address address) { Guard.ForNullOrEmpty(id, "id"); Guard.ForNull(name, "name"); Guard.ForNull(address, "address"); _id = id; _name = name; _address = address; } public void Move(Address newAddress) { Guard.ForNull(newAddress, "newAddress"); _address = newAddress; DomainEvents.Raise(new CustomerMoved(_id)); } } By having a dispatcher implementation that records the events instead of dispatching them, we can test whether the aggregate raised the correct domain event. ...