城堡拦截器凭借流畅的界面 [英] Castle Interceptors With Fluent Interface

查看:191
本文介绍了城堡拦截器凭借流畅的界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我写工作拦截器,但由于某些原因,它似乎并没有被实例化时,拦截我要求我的部件。我在做这样的事情(原谅我,如果这不挺编译,但你应该得到的想法):

I'm trying to get an interceptor I've written to work, but for some reason it doesn't seem to be instantiating the interceptor when I request my components. I'm doing something like this (forgive me if this doesn't quite compile, but you should get the idea):

container.Register(
    Component.For<MyInterceptor>().LifeStyle.Transient,
    AllTypes.Pick().FromAssembly(...).If(t => typeof(IView).IsAssignableFrom(t)).
    Configure(c => c.LifeStyle.Is(LifestyleType.Transient).Named(...).
                   Interceptors(new InterceptorReference(typeof(MyInterceptor)).
    WithService.FromInterface(typeof(IView)));

我已经把断点在构造用于拦截,它似乎并没有在所有实例化。

I've put breakpoints in the constructor for the Interceptor and it doesn't seem to be instantiating it at all.

在过去,我注册使用XML配置我的拦截器,但我渴望使用流畅的界面。

In the past I've registered my interceptors using the XML configuration, but I'm keen to use the fluent interface.

任何帮助将不胜感激!

推荐答案

我觉得你滥用 WithService.FromInterface 文档说:

使用工具来查找子
接口。例如:如果你有
IService和IProductService:
ISomeInterface,IService,
ISomeOtherInterface。当你调用
FromInterface(typeof运算(IService)),那么
IProductService将被使用。当你想注册的所有的你
服务,但不希望指定
所有的人都有益

Uses implements to lookup the sub interface. For example: if you have IService and IProductService : ISomeInterface, IService, ISomeOtherInterface. When you call FromInterface(typeof(IService)) then IProductService will be used. Useful when you want to register all your services and but not want to specify all of them.

你还缺少 InterceptorGroup Anywhere的
这里的工作示例,我改变了它尽可能少地从你的样品,使其工作:

You're also missing the InterceptorGroup Anywhere. Here's a working sample, I changed it as little as possible from your sample to make it work:

[TestFixture]
public class PPTests {
    public interface IFoo {
        void Do();
    }

    public class Foo : IFoo {
        public void Do() {}
    }

    public class MyInterceptor : IInterceptor {
        public void Intercept(IInvocation invocation) {
            Console.WriteLine("intercepted");
        }
    }

    [Test]
    public void Interceptor() {
        var container = new WindsorContainer();

        container.Register(
            Component.For<MyInterceptor>().LifeStyle.Transient,
            AllTypes.Pick()
                .From(typeof (Foo))
                .If(t => typeof (IFoo).IsAssignableFrom(t))
                .Configure(c => c.LifeStyle.Is(LifestyleType.Transient)
                                    .Interceptors(new InterceptorReference(typeof (MyInterceptor))).Anywhere)
                .WithService.Select(new[] {typeof(IFoo)}));

        container.Resolve<IFoo>().Do();
    }
}

这篇关于城堡拦截器凭借流畅的界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆