2

我正在尝试通过拦截器实现休眠事务处理,但无法弄清楚如何通过流利的机制注册接口。

我看到一个

Component.For<ServicesInterceptor>().Interceptors

但不确定如何使用它。有人可以帮我吗?这个例子看起来有点复杂。

4

2 回答 2

6

您分两步完成:

  • 您需要将拦截器注册为容器中的服务:
container.Register(Component.For<MyInterceptor>());
  • 您注册要拦截的组件。使用Interceptorsfluent API 上的方法,您可以指定要使用哪个注册的拦截器(按键或类型)来拦截此组件:
container.Register(Component.For<IFoo>().ImplementedBy<Foo>()
   .Interceptors<MyInterceptor>());

有关更多详细信息,请参阅文档

于 2010-04-04T19:34:47.690 回答
1

首先注册拦截器:

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

然后注册被拦截的对象:

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

于 2010-04-04T19:25:31.147 回答