我正在尝试通过拦截器实现休眠事务处理,但无法弄清楚如何通过流利的机制注册接口。
我看到一个
Component.For<ServicesInterceptor>().Interceptors
但不确定如何使用它。有人可以帮我吗?这个例子看起来有点复杂。
您分两步完成:
container.Register(Component.For<MyInterceptor>());
Interceptorsfluent API 上的方法,您可以指定要使用哪个注册的拦截器(按键或类型)来拦截此组件:container.Register(Component.For<IFoo>().ImplementedBy<Foo>() .Interceptors<MyInterceptor>());
有关更多详细信息,请参阅文档。
首先注册拦截器:
container.Register(Component.For<IDbInterceptor>().ImplementedBy<DbInterceptor>().Named("transactionInterceptor"));
然后注册被拦截的对象:
container.Register(Component.For<IMyService>().ImplementedBy<MyService>().Named("MyService").Interceptors(new InterceptorReference("transactionInterceptor")).Anywhere);