【问题标题】:Castle Interceptors With Fluent Interface具有流畅界面的 Castle 拦截器
【发布时间】:2009-07-27 15:48:10
【问题描述】:

我正在尝试让我编写的拦截器工作,但由于某种原因,当我请求我的组件时,它似乎没有实例化拦截器。我正在做这样的事情(如果这不能完全编译,请原谅我,但你应该明白):

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

我在 Interceptor 的构造函数中放置了断点,它似乎根本没有实例化它。

过去我使用 XML 配置注册了我的拦截器,但我热衷于使用流畅的界面。

任何帮助将不胜感激!

【问题讨论】:

    标签: c# castle-windsor fluent-interface iinterceptor


    【解决方案1】:

    我认为你在滥用WithService.FromInterface。文档说:

    使用工具来查找子 界面。例如:如果你有 IService 和 IProductService : ISomeInterface,IService, ISomeOther 接口。你打电话时 FromInterface(typeof(IService)) 然后 将使用 IProductService。有用 当你想注册所有你的 服务,但不想指定 全部。

    您还缺少InterceptorGroup Anywhere。 这是一个工作示例,我尽可能少地从您的示例中更改它以使其工作:

    [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();
        }
    }
    

    【讨论】:

    • 这行得通,看来我只是错过了 InterceptorGroup 上的 Anywhere,我想这只是我没有阅读文档的一个例子(出于兴趣,它们在哪里?)。 WithService.Select() 似乎没有选择我想要的 IView 接口,因为它已实现多次(不要问),但 FromService 似乎可以解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 2012-03-31
    • 2010-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多