gpt4 book ai didi

caSTLe-windsor - CaSTLe Windsor ArrayResolver ServiceOverrides 不被尊重

转载 作者:行者123 更新时间:2023-12-04 04:24:54 26 4
gpt4 key购买 nike

使用 ArrayResolver 时,如果我注册了多个实现相同接口(interface)的依赖项,并且我已经注册了依赖于这些依赖项数组的类,那么人们会期望 ServiceOverrides 受到尊重并允许控制哪些依赖项被注入(inject)到构造函数中。相反,我已经看到并编写了测试,显示每个依赖项都被注入(inject)到注册的类构造函数中。

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

最佳答案

该问题现已修复,该修复将成为 Windsor 下一个主要版本的一部分。

关于caSTLe-windsor - CaSTLe Windsor ArrayResolver ServiceOverrides 不被尊重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4018564/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com