문제

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?

도움이 되었습니까?

해결책

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));
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top