基于温莎城堡中的名称约定动态解析 [英] Resolving dynamically base on name convention in Castle Windsor

查看:62
本文介绍了基于温莎城堡中的名称约定动态解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看似简单的用例。有一个ICsvReader组件。让我们在这里简单地命名为读者。我们加载了一组已知的CSV文件,其中有些带有标头,有些则没有。当前有多个读取器:Reader_Skips1Row,Reader_Skips2Rows等。

I have a seemingly simple use case. There is a ICsvReader component. Let's name it simply Reader here. We load a known set of CSV files and some of them have headers and some don't. Currently there are multiple readers: Reader_Skips1Row, Reader_Skips2Rows etc.

是否可以仅注册一个组件并使Windsor查看组件密钥,然后剥离 _Skips。 。并使用相关属性集解决所需的组件?

Is there a way to register only one component and have Windsor look at the component key, strip the "_Skips..." part and resolve the required component with relevant properties set?

我尝试了运气好的子分解器和设施。

I have tried subresolver and facility with no luck.

编辑

是的,只有一个实现,但它用作依赖项,并配置为按名称解析。读取器在代码中配置

Yes there is only one implementation but it is used as a dependency and configured to be resolved by name. The reader is configured in code

Component.For<ICsvReader>()
         .ImplementedBy<CommaSeparetedCsvReader>()
         .DependsOn(new { SkipHeader = true, HeaderRowsToSkip = 2 } )
         .Named("CommaSeparetedCsvReader_Skips2Rows")
         .Lifestyle.Transient

Component.For<ICsvReader>()
         .ImplementedBy<CommaSeparetedCsvReader>()
         .DependsOn(new { SkipHeader = true, HeaderRowsToSkip = 1 } )
         .Named("CommaSeparetedCsvReader_Skips1Row")
         .Lifestyle.Transient

Component.For<ICsvReader>()
         .ImplementedBy<CommaSeparetedCsvReader>()
         .Named("CommaSeparetedCsvReader")
         .Lifestyle.Transient

这些在处理器类中用作依赖项。它是用XML配置的,因此可以在运行时对其进行操作

These are used as dependency in a processor class. It is configured in XML, so that in can be manipulated at runtime

<component id="Processor 
   type="Processor">
   <parameters>
      <reader>CommaSeparetedCsvReader_Skips2Rows</reader>
   </parameters>
</component>

理想情况下,我只想注册CommaSeparetedCsvReader组件,但是当尝试解析CommaSeparetedCsvReader_Skips2Rows时,应删除后缀,进行解析

Ideally I would like to register only the CommaSeparetedCsvReader component but when an attempt is made to resolve CommaSeparetedCsvReader_Skips2Rows it should strip the suffix, parse it and change the properties accordingly.

是否可以通过某种方式修改Resolve()行为?

Is it possible to somehow modify the Resolve() behavior?

谢谢,
Tom

Thanks, Tom

推荐答案

如果使用TypedFactoryFacility解析组件,则创建自定义的ITypedFactoryComponentSelectors可能会对您有所帮助。将需要更多有关如何创建读者的详细信息,以便为您提供更多信息。

If you are resolving your components using the TypedFactoryFacility, creating a custom ITypedFactoryComponentSelectors might help you. I would need more detail on how you create the Readers to give you more info.

亲切的问候,
Marwijn。

Kind regards, Marwijn.

Edi t ===================================

Edit =====================================

让我们举个例子:

public interface IFoo
{
}

public class Foo1 : IFoo
{
}

public class Foo2 : IFoo
{
}

public interface IFooFactory
{
    IFoo CreateFoo(string which);
}

public class FooFactoryComponentSelector : DefaultTypedFactoryComponentSelector
{
    protected override string GetComponentName(MethodInfo method, object[] arguments)
    {
        return (string)arguments[0];
    }
}

---注册

container.AddFacility<TypedFactoryFacility>();
Component.For<IFoo>().Named("Foo1Name").ImplementedBy<Foo1>(), 
Component.For<IFoo>().Named("Foo2Name").ImplementedBy<Foo2>(),
Component.For<IFooFactory>().AsFactory(f => f.SelectedWith(new FooFactoryComponentSelector())),

---用法

var factory = _container.Resolve<IFooFactory>(); // in general this would just be a dependency in the constructor.
var foo = factory.CreateFoo("Foo2Name");

只需根据您的需求调整组件选择器即可。如果构造函数需要容器未提供的参数,则如有必要,还可以将其他参数传递给CreateFoo。

Just adapt the component selector to your needs. If necessary you can also pass additional arguments to CreateFoo, if the constructor requires arguments not provided by the container.

更多信息: http://docs.castleproject.org/Windsor.Typed-Factory-Facility-interface-based-factories.ashx

这篇关于基于温莎城堡中的名称约定动态解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆