2010-11-21 60 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