2010-10-25 143 views
3

當使用ArrayResolver,如果我寄存器實現相同的接口的多個依賴關係和我有註冊類依賴這些依賴關係在陣列上,可以預期被尊重ServiceOverrides並允許控制哪些依賴關係被注入到構造器中。相反,我已經看到並編寫了測試,顯示每個依賴項被注入到註冊類構造器中。城堡溫莎ArrayResolver ServiceOverrides不遵守

using System.Linq; 
using Castle.MicroKernel.Registration; 
using Castle.MicroKernel.Resolvers.SpecializedResolvers; 
using Castle.MicroKernel.SubSystems.Configuration; 
using Castle.Windsor; 
using NUnit.Framework; 

namespace ServiceFramework.UnitTests 
{ 
    public class ArrayResolver_ServiceOverridesNotRespected 
    { 
     private class TestInstaller : IWindsorInstaller 
     { 
      public void Install(IWindsorContainer container, IConfigurationStore store) 
      { 
       container.Register(
        Component.For<IDependency>().ImplementedBy<Foo>().Named("foo"), 
        Component.For<IDependency>().ImplementedBy<Bar>().Named("bar"), 
        Component.For<IDependency>().ImplementedBy<Baz>().Named("baz"), 
        Component.For<IDependOnArray>().ImplementedBy<DependsOnArray>().Named("InjectAll"), 
        Component.For<IDependOnArray>().ImplementedBy<DependsOnArray>().Named("InjectFooOnly").ServiceOverrides(
         ServiceOverride.ForKey("steps").Eq(new[] {"foo"})), 
        Component.For<IDependOnArray>().ImplementedBy<DependsOnArray>().Named("InjectFooAndBarOnly").ServiceOverrides(
         ServiceOverride.ForKey("steps").Eq(new[] {"foo", "bar"}))); 
      } 
     } 

     public interface IDependency 
     { 
     } 

     public class Foo : IDependency 
     { 
     } 

     public class Bar : IDependency 
     { 
     } 

     public class Baz : IDependency 
     { 
     } 

     public interface IDependOnArray 
     { 
      IDependency[] GetDependencies(); 
     } 

     public class DependsOnArray : IDependOnArray 
     { 
      private readonly IDependency[] _steps; 
      public DependsOnArray(IDependency[] steps) 
      { 
       _steps = steps; 
      } 

      public IDependency[] GetDependencies() 
      { 
       return _steps; 
      } 
     } 

     [Test] 
     public void InjectFooOnly_WithoutArrayResolver() 
     { 
      using (var container = new WindsorContainer()) 
      { 
       container.Install(new TestInstaller()); 
       var fooItemTest = container.Resolve<IDependOnArray>("InjectFooOnly"); 
       var dependencies = fooItemTest.GetDependencies().Select(d => d.GetType()).ToList(); 
       Assert.That(dependencies, Has.Count.EqualTo(1)); 
       Assert.That(dependencies, Has.Member(typeof(Foo))); 
      } 
     } 

     [Test] 
     [ExpectedException(typeof(AssertionException))] 
     public void InjectFooOnly_WithArrayResolver() 
     { 
      using (var container = new WindsorContainer()) 
      { 
       container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel, true)); 
       container.Install(new TestInstaller()); 
       var fooItemTest = container.Resolve<IDependOnArray>("InjectFooOnly"); 
       var dependencies = fooItemTest.GetDependencies().Select(d => d.GetType()).ToList(); 
       Assert.That(dependencies, Has.Count.EqualTo(1)); 
       Assert.That(dependencies, Has.Member(typeof(Foo))); 
      } 
     } 

     [Test] 
     public void InjectFooAndBarOnly_WithoutArrayResolver() 
     { 
      using (var container = new WindsorContainer()) 
      { 
       container.Install(new TestInstaller()); 
       var fooItemTest = container.Resolve<IDependOnArray>("InjectFooAndBarOnly"); 
       var dependencies = fooItemTest.GetDependencies().Select(d => d.GetType()).ToList(); 
       Assert.That(dependencies, Has.Count.EqualTo(2)); 
       Assert.That(dependencies, Has.Member(typeof(Foo))); 
       Assert.That(dependencies, Has.Member(typeof(Bar))); 
      } 
     } 

     [Test] 
     [ExpectedException(typeof(AssertionException))] 
     public void InjectFooAndBarOnly_WithArrayResolver() 
     { 
      using (var container = new WindsorContainer()) 
      { 
       container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel, true)); 
       container.Install(new TestInstaller()); 
       var fooItemTest = container.Resolve<IDependOnArray>("InjectFooAndBarOnly"); 
       var dependencies = fooItemTest.GetDependencies().Select(d => d.GetType()).ToList(); 
       Assert.That(dependencies, Has.Count.EqualTo(2)); 
       Assert.That(dependencies, Has.Member(typeof(Foo))); 
       Assert.That(dependencies, Has.Member(typeof(Bar))); 
      } 
     } 

     [Test] 
     public void InjectAll() 
     { 
      using (var container = new WindsorContainer()) 
      { 
       container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel, true)); 
       container.Install(new TestInstaller()); 
       var fooItemTest = container.Resolve<IDependOnArray>("InjectAll"); 
       var dependencies = fooItemTest.GetDependencies().Select(d => d.GetType()).ToList(); 
       Assert.That(dependencies, Has.Count.EqualTo(3)); 
       Assert.That(dependencies, Has.Member(typeof(Foo))); 
       Assert.That(dependencies, Has.Member(typeof(Bar))); 
       Assert.That(dependencies, Has.Member(typeof(Baz))); 
      } 
     } 
    } 
} 
+0

的海報是我的同事,我們已經證實,在這兩個溫莎城堡2.5.0和2.5.1存在這種行爲,那CollectionResolver顯示相同的行爲。在使用「DependsOn」語法指定2.5.1中的服務覆蓋時,我也會看到相同的行爲。我不知道這是由設計或沒有,但是這肯定看起來像非直觀的行爲。我希望顯式指定參數優先於子依賴關係解析器。 – 2010-10-25 20:51:45

+1

這裏是在城堡issuetracker問題:http://issues.castleproject.org/issue/IOC-240 – roelofb 2010-10-26 07:28:22

回答

1

現在問題已經解決,修復程序將成爲下一個主要版本的溫莎的一部分。