简体   繁体   English

是为装饰器阵列配置温莎城堡的更好方法

[英]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.

相关问题 如何在Castle Windsor中配置此ClientBase类 - How to I configure this ClientBase class in Castle Windsor 城堡温莎配置:为装饰器设置FromAssembly.InDirectory的顺序 - Castle Windsor Configuration: Set Order for FromAssembly.InDirectory for Decorators Castle Windsor到Unity - 您可以在CW中以与CW相同的方式自动配置吗? - Castle Windsor to Unity - can you auto-configure in Unity the same way you can in CW? 温莎城堡:配置中的复杂类型数组 - castle windsor: array of complex types from config Castle windsor解决了具有开放式通用和接口的阵列 - Castle windsor resolve array with open generic and interface 温莎城堡解析通用接口数组 - Castle Windsor resolve array of generic interfaces 温莎城堡FirstInterface()。Configure(c =&gt; c.LifeStyle.PerWebRequest) - Castle Windsor FirstInterface().Configure(c=> c.LifeStyle.PerWebRequest) 温莎城堡WCF设施未处理单向运营 - Castle Windsor WCF Facility is not processing one way operations 有没有安全的方法可以解决Castle.Windsor中的组件? - Is there safe way to resolve component from Castle.Windsor? 温莎城堡:有没有一种方法可以在没有解决呼叫的情况下验证注册? - Castle Windsor: is there a way of validating registration without a resolve call?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM