2012-08-28 50 views
1

我有攔截註冊到類intance的組件的問題。如果我不使用類實例Castle Windsor無法攔截爲某個類實例註冊的組件

//this get intercepted 
container.Register(Component.For<IService>().ImplementedBy<SampleService>() 
         .Interceptors<Interceptor>()); 

這是一個錯誤或設計註冊的組件

//this does not get intercepted 
container.Register(Component.For<IService>().Instance(instanceService) 
           .Interceptors<Interceptor>()); 

攔截器的工作原理?

謝謝

這是單元測試代碼。

[Test] 
    public void Test_Windsor_Interceptor_With_Instance_Component_Registration() 
    { 
     IService instanceService = new SampleService(); 

     var container = new WindsorContainer(); 
     container.Register(Component.For<Interceptor>()); 

     //this get intercepted 
     container.Register(Component.For<IService>().ImplementedBy<SampleService>() 
          .Interceptors<Interceptor>()); 

     ////this does not get intercepted 
     //container.Register(Component.For<IService>().Instance(instanceService) 
     //     .Interceptors<Interceptor>()); 

     var proxiedService = container.Resolve<IService>(); 

     proxiedService.DoSomething(); 


    } 

    public class Interceptor : Castle.DynamicProxy.IInterceptor 
    { 
     public void Intercept(Castle.DynamicProxy.IInvocation invocation) 
     { 
      throw new System.NotImplementedException("Interceptor succesfully called but not implemented"); 
     } 
    } 

    public interface IService 
    { 
     void DoSomething(); 
    } 

    public class SampleService : IService 
    { 
     public void DoSomething() 
     { 
      string dummy = string.Empty; 
     } 
    } 

回答

2

它是通過設計。如果您手動實例化對象,則無法指望Windsor連接攔截器。

1

實際上,你可以通過使用攔截器選擇器來攔截已註冊的實例,但如果按照上面概述的「風箏」的方式工作,那將會很不錯。就是這樣。這個例子特定於我的實現,但有足夠的細節可以根據您的使用情況進行計算。

StubISecurityService instance= new StubISecurityService(); 
Container.Register(Component.For<ISecurityService>().Instance(instance)); 

//Apply the interceptors. 
Container.Kernel.ProxyFactory.AddInterceptorSelector(new InterceptorSelector<SecurityInterceptor>(model => model.Implementation == typeof(ExampleSecureService))); 

IExampleSecureService exampleSecureService = Container.Resolve<IExampleSecureService>(); 

/// <summary> 
/// A generic implementation of <see cref="IModelInterceptorsSelector"/> used to apply a single interceptor on matching types. 
/// </summary> 
/// <typeparam name="TInterceptor">The type of the interceptor.</typeparam> 
public class InterceptorSelector<TInterceptor> : IModelInterceptorsSelector { 
    private readonly Func<ComponentModel, bool> _selector; 

    /// <summary> 
    /// Initializes a new instance of the <see cref="InterceptorSelector{TInterceptor}"/> class. 
    /// </summary> 
    /// <param name="selector">The function used to find matching types.</param> 
    public InterceptorSelector(Func<ComponentModel, bool> selector) { 
     _selector = selector; 
    } 

    public virtual bool HasInterceptors(ComponentModel model) { 
     bool isNotItself = typeof(TInterceptor) != model.Implementation; 
     bool isMatch = _selector.Invoke(model); 
     return isNotItself && isMatch; 
    } 

    public virtual InterceptorReference[] SelectInterceptors(ComponentModel model, InterceptorReference[] interceptors) { 
     return new[] {InterceptorReference.ForType<TInterceptor>()}; 
    } 
} 
相關問題