Queuing domain events in C#
This article 开发者_JAVA百科describes a great pattern called 'Domain Events': http://www.udidahan.com/2009/06/14/domain-events-salvation/
One major flaw with this pattern however is highlighted in comment 27 by user Andy: If a transaction fails, we don't want our domain events to execute. Therefore, we need to create some sort of queuing mechanism.
Unfortunately this sounds like it is going to massively complicate a technique that was supposed to simplify the system.
Does anyone know of some good examples or discussions of queuing domain events, particular a solution that integrates well with NHibernate?
I worked out how to do this: The secret is the RegisterSynchronization method of NHibernate's ITransaction.
As an example, here is how I might send an email to a customer only when the transaction is committed:
public class NotifyCustomerEmail
{
private void MailMessage { get; set; }
public void SendAsyncOnceTransactionCommits()
{
if (MailMessage == null)
ComposeMailMessage();
NHibernateSessionManager
.CurrentSession
.Transaction
.RegisterSynchronization(new SendEmailSynchronization(this.MailMessage));
}
}
精彩评论