I have implemented domain events as prescribed in Udi Dahan's Domain Events - Salvation article.
As far as I understand, domain events can be run asynchronously to the thread from which it was raised (typically from a domain model or service).
There does not need to be any sort of "shared" unit of work or repository implementation and no transactional consistency is required.
Question: Why has Udi not implemented the handling of domain events in a separate thread?
For example, I have added the creation of a Task
to handle events asynchronously:
public static void Raise<T>(T domainEvent) where T : IDomainEvent
{
if (Container != null)
{
foreach (var handler in Container.ResolveAll<IDomainEventHandler<T>>())
{
Task.Factory.StartNew(() =>
{
handler.Handle(domainEvent);
}).ContinueWith(t => {
// Log exception
}, TaskContinuationOptions.OnlyOnFaulted);
}
}
}
Are there any multithreading issues that could arise from this?
EDIT: Note that while these domain events would be light-weight, they will still be running on IIS as this is a MVC web project.