温莎城堡流畅API:定义明确的依赖 [英] Castle Windsor Fluent API: Define dependency explicitly
问题描述
由于以下配置。
Container.Register(Component.For<A>().Named("foo"));
Container.Register(Component.For<B>().Named("foobar"));
Container.Register(
AllTypes.Pick()
.FromAssemblyNamed("MyAssembly")
.If(t => t.Name.EndsWith("ABC"))
.Configure(c => c.LifeStyle.Is(LifestyleType.Transient))
.WithService.Select(i => typeof(I))
);
Container.Register(
AllTypes.Pick()
.FromAssemblyNamed("MyAssembly")
.If(t => t.Name.EndsWith("123"))
.Configure(c => c.LifeStyle.Is(LifestyleType.Transient))
.WithService.Select(i => typeof(I))
);
如果我知道,界面中的我暴露属性P,那类A和B可以被分配为P;我如何明确规定从AllTypes呼叫类型的第一集合应具有属性P设置为与foo的ID的类型,而第二个集合应具有相同的属性设置为类型与foobar的的id
If I know that the interface "I" exposes a property "P", and that the classes A and B can be assigned to P; how do I explicitly state that the first collection of types from the AllTypes call should have the property P set to the type with id of "foo", and the second collection should have the same property set to the type with the id of "foobar"?
使用XML配置,这可以通过显式设置使用$ {ID}符号的参数来完成。我认为它的流畅API类似。
Using XML config this can be done by explicitly setting the parameters using the ${id} notation. I assume its similar in the fluent API.
感谢。
推荐答案
您在正确的轨道上 - 你需要做的是配置每个组件的参数是什么,提供名为P,其值为$ {参数富}或$ {} FOOBAR根据您的情况,这里有一个工作的xUnit事实(向下滚动向底部的实际注册code)这说明您的情况。
You are on the right track - what you need to do is configure the parameters of each component, supplying the parameter named "P" with the value "${foo}" or "${foobar}" depending on your scenario, here's a working xunit fact (scroll down towards the bottom for the actual registration code) which demonstrates your scenario.
namespace Question651392
{
public class First123 : I
{
public AbstractLetter P { get; set; }
}
public class Second123 : I
{
public AbstractLetter P { get; set; }
}
public class FirstABC : I
{
public AbstractLetter P { get; set; }
}
public class SecondABC : I
{
public AbstractLetter P { get; set; }
}
public interface I
{
AbstractLetter P { get; set; }
}
public abstract class AbstractLetter
{
}
public class B : AbstractLetter
{
}
public class A : AbstractLetter
{
}
public class RegistrationFacts
{
[Fact]
public void EnsureParametersCanBeSetWhenRegisteringComponentsInBulk()
{
WindsorContainer Container = new WindsorContainer();
Container.Register(Component.For<A>().Named("foo"));
Container.Register(Component.For<B>().Named("foobar"));
Container.Register(
AllTypes.Pick()
.FromAssembly(GetType().Assembly)
.If(t => t.Name.EndsWith("ABC"))
.Configure(c => c.LifeStyle.Is(LifestyleType.Transient))
.Configure(c=>c.Parameters(Parameter.ForKey("P").Eq("${foo}")))
.WithService.Select(new[] { typeof(I) })
);
Container.Register(
AllTypes.Pick()
.FromAssembly(GetType().Assembly)
.If(t => t.Name.EndsWith("123"))
.Configure(c => c.LifeStyle.Is(LifestyleType.Transient))
.Configure(c => c.Parameters(Parameter.ForKey("P").Eq("${foobar}")))
.WithService.Select(new[] { typeof(I)})
);
var all = Container.ResolveAll<I>();
var firstABC = all.Single(i => i is FirstABC);
Assert.IsType(typeof(A), firstABC.P);
var first123 = all.Single(i => i is First123);
Assert.IsType(typeof (B), first123.P);
Assert.Equal(4, all.Count());
}
}
}
希望这有助于!
这篇关于温莎城堡流畅API:定义明确的依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!