Register an Interceptor with Castle Fluent Interface

跟風遠走 提交于 2019-12-10 10:46:41

问题


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.


回答1:


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.




回答2:


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);



来源:https://stackoverflow.com/questions/2575570/register-an-interceptor-with-castle-fluent-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!