2

This article 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?

cbp
  • 25,252
  • 29
  • 125
  • 205
  • possible duplicate of [Where to raise persistence-dependent domain events - service, repository, or UI?](http://stackoverflow.com/questions/5886174/where-to-raise-persistence-dependent-domain-events-service-repository-or-ui) – cbp Jul 25 '11 at 06:37

1 Answers1

5

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));
    }
}
cbp
  • 25,252
  • 29
  • 125
  • 205