I’ve got a web application with multiple clients. For the most part, everyone plays in the same application working against the same database. But now I’ve got a problem. I have a really good client that has a custom business rule. It’s a rule that just does not apply to any other customer. It’s really important, and I don’t want to lose this customer. As a bonus, this customer is willing to pay for this custom rule to be implemented against the existing code base.
Queue up domain events. Domain events are not nothing new in the DDD world. And implementing them in ASP.NET MVC 3 is really easy.
public interface IDomainEvent { }
public interface IHandles<in T> where T : IDomainEvent {
bool CanHandle(T domainEvent);
void Handle(T domainEvent);
}
public class DomainEvents {
public static Raise<T>(T domainEvent) where T : IDomainEvent {
var handlers = DependencyResolver.Current.GetServices<IHandles<T>>();
if (handlers != null) {
handlers.Where(handler => handler.CanHandle(domainEvent)).ForEach(handler => handler.Handle(domainEvent));
}
}
}
So I can create my IDomainEvent events, my IHandles event handler, and get on with my job. Or can I?
But I still have the problem where I only Account #17 wants this event to fire. How do I do this? I could just say in my CanHandle implementation, “If the account ID is 17, then return true. Otherwise, return false.” That is just sloppy, but it was my first version. The only problem is that when I run on localhost, Account #17 isn’t the same as in my production environment. Instead, I’ve added a new table of applied rules. Every rule gets a rule name, and then I can inspect the AppliedRules table and see that Account #17 should use rule “my_custom_rule”.