Event Centric: super-charge your model with domain events to enable business intelligence
I’ve shown in my previous post how interesting domain events can be mined using the Reactive Extensions for .NET. Now we need to raise those events when things happen to our domain. The typical way you’d publish events from your domain is simply adding .NET events. Say we have a Patient class, with a method Admit that causes the patient to be in the hospital and tracks the date when he was admitted:
public class Patient { public DateTimeOffset? AdmittedDate { get; private set; } public bool IsInHospital { get; private set; } public void Admit(DateTimeOffset when) { if (this.IsInHospital) throw new InvalidOperationException("Already in hospital!"); this.IsInHospital = true; this.AdmittedDate = when; } }
The domain object method typically does some precondition validation (like the one above, the patient can’t be admitted twice), then mutates the state as needed. At some point the resulting object state will be persisted somehow, but it’s a good goal to have ...