views:

146

answers:

2

I am trying to implement nhibernate transaction handling through Interceptors and couldn’t figure out how to register the interface through fluent mechanism.

I see a

Component.For<ServicesInterceptor>().Interceptors

but not sure how to use it. Can someone help me out? This example seemed a little complex.

A: 

First register the interceptor:

container.Register(Component.For<IDbInterceptor>().ImplementedBy<DbInterceptor>().Named("transactionInterceptor"));

Then register the objecting being intercepted:

container.Register(Component.For<IMyService>().ImplementedBy<MyService>().Named("MyService"). Interceptors(new InterceptorReference("transactionInterceptor")).Anywhere);

Otávio Décio
+1  A: 

You do it in two steps:

  • You need to register the interceptor as a service in the container:
container.Register(Component.For<MyInterceptor>());
  • You register the component you want to intercept. Using Interceptors method on fluent API you specify which of the registered interceptors (by key, or type) you want to intercept this component with:
container.Register(Component.For<IFoo>().ImplementedBy<Foo>()
   .Interceptors<MyInterceptor>());

See the documentation for more details.

Krzysztof Koźmic
@Krzysztof My interceptor is designed to pick all methods and check for a custom attribute.which means that the class might not implement an interface. Essentially any method that has transaction attribute goes inside a transaction. So how do i add an interceptor to intercept all methods in the app domain? Your example if for `IFoo`
Quintin Par
DynamicProxy is able to intercept calls to methods on classes, but the requirement is that these methods must be virtual, so as long as your components satisfy that requirement you're good to go
Krzysztof Koźmic