Skip to content
shorttp edited this page Feb 17, 2012 · 3 revisions

My SharpArchContrib.Castle.NHibernate.Transaction Attribute is not working!

You've done some work, but something failed, however, something still gets written to the database instead of rolling back. First off, ensure you've followed these steps found here:

Using the SharpArchContrib.Castle.NHibernate.Transaction attribute

Injection is the key to effectively using the Sharp Architecture Transaction attribute. Anything that is using the Transaction attribute must be injected. The reason for this is the Castle interceptors need to get to it in order for it to do its work properly. For example you could have this method that has a transaction attribute:

[Transaction]
public void DoTransaction() {
    //do some work here
}

There are two ways to inject this:

1. Inherit from an interface (assume DoTransaction() is a method in the interface):

public MyTransactionClass : ITransactionClass
{
    [Transaction]
    public void DoTransaction() 
    {
        //do some work here
    }
}

In your controller, then you could do this:

public MyController(ITransactionClass transactionClass)
{
    transactionClass.DoTransaction();
}

2. Use a concrete class, but mark the method virtual:

public MyTransactionClass
{
    [Transaction]
    public virtual void DoTransaction() 
    {
        //do some work here
    }
}

In your controller, then you could do this:

public MyController(MyTransactionClass transactionClass)
{
    transactionClass.DoTransaction();
}

Most importantly, you must ensure that you have registered where these classes reside. This is usually done in the ComponentRegistrar class that is called from your Global.asax file (see SharpArchCookbook example found here: SharpArchCookbook). In the example, the ComponentRegistrar.cs file calls a method AddCustomRepositoriesTo() that registers them with the Castle container.