Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

AutoTx Facility Per Transaction Lifestyle and Per Top Transaction Lifestyle

haf edited this page · 1 revision

This is what NHibernate Facility is using. It allows components to live as long as the transaction in which they are resolved.

using Castle.Services.Transaction.Lifestyles;
using Castle.MicroKernel;
using Castle.MicroKernel.Facilities;
using Castle.MicroKernel.Registration;
// ...

var c = new WindsorContainer().Register(
    Component.For<IMyRepository>().LifeStyle.HybridPerTransactionTransient(),
    Component.For<IMyService>().LifeStyle.PerWebRequest)
);

// ...

// from your controller/its constructor
var service = c.Resolve<IMyService>();
service.PerformTransactionalWork();

// ... service impl:

public MyService(Func<IMyRepository> repoFactory) {
    _R = repoFactory;
}

[Transaction] // no virtual required as it's an interface
public void PerformTransactionalWork() {
    var repo = _R();
    repo.Save(new Thing("hello world"));
} // repo is disposed/released here through Windsor's Component Burden implementation
Something went wrong with that request. Please try again.