[英]Is better way to configure Windsor Castle for array of decorators
I have in my project interface 我在项目界面中
public interface IUpdater
{
void Update();
}
and some implementations UpdaterA, UpdaterB, UpdaterC
I have also decorator DecoratorUpdater
. 还有一些实现UpdaterA, UpdaterB, UpdaterC
我还有装饰器DecoratorUpdater
。 All these classes implement interface IUpdater
and I need configure Windsor Castle for using as array decorated implementations in class UpdaterService
所有这些类都实现了IUpdater
接口,我需要配置Windsor Castle以便在UpdaterService
类中用作数组修饰的实现。
My solution is following, but it not so easy how can be (currently I have 7 updaters and 2 decorators) 我的解决方案是遵循的,但是怎么做并不容易(目前我有7个更新器和2个装饰器)
// decorators
Container.Register(
Component.For<IUpdater, DecoratorUpdater>().LifestylePerWebRequest().Named("DecoratorUpdaterA").DependsOn(Dependency.OnComponent("underlyingUpdater", "NamedUpdaterA")),
Component.For<IUpdater, DecoratorUpdater>().LifestylePerWebRequest().Named("DecoratorUpdaterB").DependsOn(Dependency.OnComponent("underlyingUpdater", "NamedUpdaterB")),
Component.For<IUpdater, DecoratorUpdater>().LifestylePerWebRequest().Named("DecoratorUpdaterC").DependsOn(Dependency.OnComponent("underlyingUpdater", "NamedUpdaterC")));
// updaters
Container.Register(
Component.For<IUpdater, UpdaterA>().LifestylePerWebRequest().Named("NamedUpdaterA"),
Component.For<IUpdater, UpdaterB>().LifestylePerWebRequest().Named("NamedUpdaterB"),
Component.For<IUpdater, UpdaterC>().LifestylePerWebRequest().Named("NamedUpdaterC"));
// usages
Container.Register(Component.For<UpdaterService>().ServiceOverrides(new
{
updaters = new[]
{
"DecoratedUpdaterA",
"DecoratedUpdaterB",
"DecoratedUpdaterC",
}
}));
Is there better way how configure Windsor Castle? 有没有更好的方法来配置温莎城堡?
好的,没有比我的解决方案更好的解决方案了。
Other a little bit shorter solution 其他稍微短一点的解决方案
Container.Register(
Component.For<IUpdater>().ImplementedBy<DecoratorUpdater>().DependsOn(Dependency.OnComponent(typeof(IUpdater), typeof(UpdaterA))).Named("DecoratedUpdaterA").LifestylePerWebRequest(),
Component.For<UpdaterA>().ImplementedBy<UpdaterA>().LifestylePerWebRequest(),
Component.For<IUpdater>().ImplementedBy<DecoratorUpdater>().DependsOn(Dependency.OnComponent(typeof(IUpdater), typeof(UpdaterB))).Named("DecoratedUpdaterB").LifestylePerWebRequest(),
Component.For<UpdaterB>().ImplementedBy<UpdaterB>().LifestylePerWebRequest(),
Component.For<IUpdater>().ImplementedBy<DecoratorUpdater>().DependsOn(Dependency.OnComponent(typeof(IUpdater), typeof(UpdaterC))).Named("DecoratedUpdaterC").LifestylePerWebRequest(),
Component.For<UpdaterC>().ImplementedBy<UpdaterC>().LifestylePerWebRequest());
There are ways to register lots of components at once. 有几种方法可以一次注册很多组件。
For example we can register all of the IUpdator in assemblies in this application, making them all PerWebRequest and named what their implementation name is by the following: 例如,我们可以在此应用程序的程序集中注册所有IUpdator,使它们全部成为PerWebRequest并通过以下方式命名其实现名称:
container.Register(
Classes.FromAssemblyInThisApplication()
.BasedOn<IUpdator>()
.Configure(registration => registration
.Named(registration.Implementation.Name)
.LifestylePerWebRequest()
));
We can also have components receive all of the components of a particular type if we use the built in castle collection resolver. 如果我们使用内置的城堡集合解析器,我们也可以让组件接收特定类型的所有组件。
If we have a class like this: 如果我们有这样的课程:
public class DecoratedComponent : IComponentThatNeedsAnArrayOfDecorators
{
public DecoratedComponent(IDecorator[] decorators)
{
}
}
We can resolve it like so: 我们可以这样解决:
IWindsorContainer container = new WindsorContainer();
container.Kernel.Resolver.AddSubResolver(new CollectionResolver(container.Kernel));
container.Register(
Component.For<IComponentThatNeedsAnArrayOfDecorators>()
.ImplementedBy<DecoratedComponent>());
container.Resolve<IComponentThatNeedsAnArrayOfDecorators>();
Our decorated component will now have all of our decorators in it. 现在,我们的装饰组件中将包含所有装饰器。
I can't exactly tell what you're attempting to do, but it feels like it can be cleaned up with methods like these. 我无法确切地说出您要尝试执行的操作,但是感觉可以使用此类方法清除它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.