2010-11-21 63 views
2

我想弄清楚如何註冊具有2個通用參數的泛型類型的裝飾器。下面的第一個測試用例只是一個1-generic參數案例的完整性檢查,我在2參數案例未按預期工作時添加了該案例。城堡溫莎註冊的開放式通用裝飾類型

帶有1個通用參數(此次逆變)的第三個(失敗)案例似乎表明溫莎不同地處理共變和逆變的泛型參數,所以我的問題是:我在這裏做了什麼錯?

我正在使用.NET 4.0。

[Test] 
    public void RepoSingleGenericTest() 
    { 
     var windsorContainer = new Castle.Windsor.WindsorContainer(); 

     windsorContainer 
      .Register(AllTypes.Of(typeof(IRepoSingle<>)) 
      .FromAssembly(typeof(StringRepoSingle).Assembly) 
      .If(t => typeof(CachingRepoSingle<>).IsAssignableFrom(t)) 
      .WithService.FromInterface(typeof(IRepoSingle<>))); 

     windsorContainer 
      .Register(AllTypes.Of(typeof(IRepoSingle<>)) 
      .FromAssembly(typeof(BoolRepoSingle).Assembly) 
      .Unless(t => typeof(CachingRepoSingle<>).IsAssignableFrom(t)) 
      .WithService.FromInterface(typeof(IRepoSingle<>))); 

     var stringRepo = windsorContainer.Resolve<IRepoSingle<string>>(); 
     Assert.IsInstanceOf<StringRepoSingle>(stringRepo); 

     var boolRepo = windsorContainer.Resolve<IRepoSingle<bool>>(); 
     Assert.IsInstanceOf<BoolRepoSingle>(boolRepo); 
    } 

    [Test] 
    public void RepoDoublyGenericTest() 
    { 
     var windsorContainer = new Castle.Windsor.WindsorContainer(); 

     windsorContainer 
      .Register(AllTypes.Of(typeof(IRepoDoublyGeneric<,>)) 
      .FromAssembly(typeof(StringRepoDoublyGeneric).Assembly) 
      .If(t => typeof(CachingRepoDoublyGeneric<,>).IsAssignableFrom(t)) 
      .WithService.FromInterface(typeof(IRepoDoublyGeneric<,>))); 

     windsorContainer 
      .Register(AllTypes.Of(typeof(IRepoDoublyGeneric<,>)) 
      .FromAssembly(typeof(DateTimeRepoDoublyGeneric).Assembly) 
      .Unless(t => typeof(CachingRepoDoublyGeneric<,>).IsAssignableFrom(t)) 
      .WithService.FromInterface(typeof(IRepoDoublyGeneric<,>))); 

     var stringRepo = windsorContainer.Resolve<IRepoDoublyGeneric<Guid, string>>(); 
     Assert.IsInstanceOf<CachingRepoDoublyGeneric<Guid, string>>(stringRepo); 

     var dateTimeRepo = windsorContainer.Resolve<IRepoDoublyGeneric<Guid, DateTime>>(); 
     Assert.IsInstanceOf<CachingRepoDoublyGeneric<Guid, DateTime>>(dateTimeRepo); 
    } 

    [Test] 
    public void CommandTest() 
    { 
     var windsorContainer = new Castle.Windsor.WindsorContainer(); 

     windsorContainer 
      .Register(AllTypes.Of(typeof(ICommand<>)) 
      .FromAssembly(typeof(GuidCommand).Assembly) 
      .If(t => typeof(DecoratorCommand<>).IsAssignableFrom(t)) 
      .WithService.FromInterface(typeof(ICommand<>))); 

     windsorContainer 
      .Register(AllTypes.Of(typeof(ICommand<>)) 
      .FromAssembly(typeof(StringCommand).Assembly) 
      .Unless(t => typeof(DecoratorCommand<>).IsAssignableFrom(t)) 
      .WithService.FromInterface(typeof(ICommand<>))); 

     var stringComand = windsorContainer.Resolve<ICommand<string>>(); 
     Assert.IsInstanceOf<DecoratorCommand<string>>(stringComand); 

     var guidCommand = windsorContainer.Resolve<ICommand<Guid>>(); 
     Assert.IsInstanceOf<DecoratorCommand<Guid>>(guidCommand); 
    } 

    public interface IRepoSingle<out TValue> 
    { 
     TValue Get(); 
    } 

    public class StringRepoSingle : IRepoSingle<string> 
    { 
     public string Get() 
     { 
      throw new NotImplementedException(); 
     } 
    } 

    public class BoolRepoSingle : IRepoSingle<bool> 
    { 
     public bool Get() 
     { 
      throw new NotImplementedException(); 
     } 
    } 

    public class CachingRepoSingle<T> : IRepoSingle<T> 
    { 
     private readonly IRepoSingle<T> realRepo; 

     public CachingRepoSingle(IRepoSingle<T> realRepo) 
     { 
      if (realRepo == null) throw new ArgumentNullException("realRepo"); 

      this.realRepo = realRepo; 
     } 

     public T Get() 
     { 
      throw new NotImplementedException(); 
     } 
    } 

    public interface IRepoDoublyGeneric<in TKey, out TValue> 
    { 
     TValue Get(TKey key); 
    } 

    public class StringRepoDoublyGeneric : IRepoDoublyGeneric<Guid, string> 
    { 
     public string Get(Guid key) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

    public class DateTimeRepoDoublyGeneric : IRepoDoublyGeneric<Guid, DateTime> 
    { 
     public DateTime Get(Guid key) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

    public class CachingRepoDoublyGeneric<TKey, TValue> : IRepoDoublyGeneric<TKey, TValue> 
    { 
     private readonly IRepoDoublyGeneric<TKey, TValue> realRepo; 

     public CachingRepoDoublyGeneric(IRepoDoublyGeneric<TKey, TValue> realRepo) 
     { 
      if (realRepo == null) throw new ArgumentNullException("realRepo"); 

      this.realRepo = realRepo; 
     } 

     public TValue Get(TKey key) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

    public interface ICommand<in T> 
    { 
     void Do(T t); 
    } 

    public class StringCommand : ICommand<string> 
    { 
     public void Do(string t) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

    public class GuidCommand : ICommand<Guid> 
    { 
     public void Do(Guid t) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

    public class DecoratorCommand<T> : ICommand<T> 
    { 
     private readonly ICommand<T> realComamnd; 

     public DecoratorCommand(ICommand<T> realComamnd) 
     { 
      if (realComamnd == null) throw new ArgumentNullException("realComamnd"); 

      this.realComamnd = realComamnd; 
     } 

     public void Do(T t) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

UPDATE:以下是卓有成效的,但我不能說,我認爲這是最優雅的解決方案...:

 [Test] 
    public void RepoDoublyGenericWithReflection() 
    { 
     var windsorContainer = new Castle.Windsor.WindsorContainer(); 

     // Register components for a caching decorator for all types implementing IRepoDoublyGeneric<,> - except for the generic cache itself 
     (from t in typeof (DateTimeRepoDoublyGeneric).Assembly.GetTypes() 
      let iRepo = t.GetInterfaces().SingleOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IRepoDoublyGeneric<,>)) 
      where 
       t.IsClass && !t.IsAbstract && !typeof(CachingRepoDoublyGeneric<,>).IsAssignableFrom(t) 
       && iRepo != null 
      select iRepo) 
     .ForEach(rt => 
      windsorContainer.Register(Component.For(rt).ImplementedBy((typeof(CachingRepoDoublyGeneric<,>)).MakeGenericType(rt.GetGenericArguments()))) 
     ); 

     // Real repositories 
     windsorContainer.Register(
      AllTypes.Of(typeof(IRepoDoublyGeneric<,>)) 
      .FromAssembly(typeof(DateTimeRepoDoublyGeneric).Assembly) 
      .Unless(t => typeof(CachingRepoDoublyGeneric<,>).IsAssignableFrom(t)) 
      .WithService.FromInterface(typeof(IRepoDoublyGeneric<,>)) 
     ); 

     var stringRepo = windsorContainer.Resolve<IRepoDoublyGeneric<Guid, string>>(); 
     Assert.IsInstanceOf<CachingRepoDoublyGeneric<Guid, string>>(stringRepo); 

     var dateTimeRepo = windsorContainer.Resolve<IRepoDoublyGeneric<Guid, DateTime>>(); 
     Assert.IsInstanceOf<CachingRepoDoublyGeneric<Guid, DateTime>>(dateTimeRepo); 
    } 

繼剋日什托夫的建議,我也嘗試:

 [Test] 
    public void CommandTestUsingBasedOn() 
    { 
     var windsorContainer = new Castle.Windsor.WindsorContainer(); 

     windsorContainer 
      .Register(AllTypes.Of(typeof(ICommand<>)) 
      .FromAssembly(typeof(GuidCommand).Assembly) 
      .If(t => typeof(DecoratorCommand<>).IsAssignableFrom(t)) 
      .BasedOn(typeof(ICommand<>)) 
      .WithService.Base()); 

     windsorContainer 
      .Register(AllTypes.Of(typeof(ICommand<>)) 
      .FromAssembly(typeof(StringCommand).Assembly) 
      .Unless(t => typeof(DecoratorCommand<>).IsAssignableFrom(t)) 
      .BasedOn(typeof(ICommand<>)) 
      .WithService.Base()); 

     var stringComand = windsorContainer.Resolve<ICommand<string>>(); 
     Assert.IsInstanceOf<DecoratorCommand<string>>(stringComand); 

     var guidCommand = windsorContainer.Resolve<ICommand<Guid>>(); 
     Assert.IsInstanceOf<DecoratorCommand<Guid>>(guidCommand); 
    } 
  • 但這並不改變行爲。

回答

4

當您註冊爲AllTypes.BasedOn(typeof(typeof(ICommand<>))WithService.Base()時它工作嗎?

+0

您好Krzysztof - 謝謝回覆:)我沒有AllTypes上的BasedOn(我使用版本2.1.0.0),所以我無法驗證是否可行。現在,我有一個基於反射的解決方法: – 2010-11-23 20:08:46

+0

請參閱上面的解決方法以供解決... – 2010-11-23 20:11:36

+0

爲什麼你不能在v2.1中做到這一點? – 2010-11-24 05:11:18