A Curious Mind
#tastic

Transactions and Castle

Monday, August 28, 2006 4:52 PM

So here I am working on a bit of code, and it has to execute after a transaction. I am trying to follow a POCO (similar to POJO but stands for Plain Old CLR Object) model so I start wondering where in the hell I am going to put this code.

I see that Castle has some cool Transaction stuff that I haven't used yet in the Castle.Services.Transactions and there is a handy class for doing just what I want, ISynchronization. I wonder if I can borrow some stellar ideas from Rhino.Commons and make a generic ISynchronization object that will automatically register itself?

namespace Rhino.Commons
{
    using Castle.Services.Transaction;

    public class Synchronizer : ISynchronization
    {
        Proc _before;
        Proc _after;
        
        public Synchronizer(Proc before, Proc after)
        {
            this._after = after;
            this._before = before;
            Kernel.Resolve<ITransactionManager>().CurrentTransaction.RegisterSynchronization(this);
        }
        
        #region ISynchronization Members

        public void AfterCompletion()
        {
            this._after();
        }

        public void BeforeCompletion()
        {
            this._before();
        }

        #endregion
    }
}

I recognize that I am still inexperienced in alot of aspects of programming, and unfortunatly I have not spent a lot of time trying to find a similar open source business app that I can study. If anyone has any suggestions I would sure appriciate it.


Feedback

# re: Transactions and Castle

What is the client code going to look like?

new Synchronizer(delegate
{
SendEmail("StartedTransaction");
},
delegate
{
SendEmail("FinishedTransaction");
});

Where do you do that?
I like the idea in general, but I would rather have an AbstractSynchronizer that does the self registration, and then inherit from it and implient the before/after explicitly.

The idea is that the client code eventually turns out to be just:

new EmailSynchronizer();


8/28/2006 6:50 PM | Ayende Rahien

Post a comment





 

Please add 8 and 1 and type the answer here: