1

给定以下代码:

public interface IMyContext
{
    string subtype { get; set; }
}

public class MyContext : IMyContext
{
    public string subtype { get; set; }
}

public interface IMyExporter
{
    string Export();
}

public class MyExporterXML : IMyExporter
{
    public string Export()
    {
        return "";
    }
}

public class MyExporterJson : IMyExporter
{
    public string Export()
    {
        return "";
    }
}

public class MyExporterFactory
{
    private IMyContext context;
    public MyExporterFactory(IMyContext context)
    {
        this.context = context;
    }

    public IMyExporter Create()
    {
        switch (context.subtype)
        {
            case "JSON" :
                    return new MyExporterJson();
            default:
                    return new MyExporterXML();
        }
    }
}

public class MyService
{
    private IMyContext context;
    private IMyExporter exporter;
    public MyService(IMyContext context, IMyExporter exporter)
    {
        this.context = context;
        this.exporter = exporter;
    }

    public string Extractdata()
    {
        return exporter.Export();
    }
}

[TestClass]
public class UnitTest2
{
    [TestMethod]
    public void TestMethod1()
    {
        var container = new WindsorContainer();
        container.Register(Component.For<IMyContext>().ImplementedBy<MyContext>());
        container.Register(Component.For<MyExporterFactory>());
        container.Register(Component.For<MyService>());
        container.Register(Component.For<IMyExporter>().UsingFactoryMethod(kernel => kernel.Resolve<MyExporterFactory>().Create()));
        var context = container.Resolve<IMyContext>();
        var service = container.Resolve<MyService>();

        context.subtype = "JSON";

        service.Extractdata();

    }
}

有没有办法让 MyService 中注入的导出器在实际使用时解析?IE。运行上述代码时,导出器解析为 MyExporterXML,但由于 context.subtype = "JSON" 设置,我真的希望它是 MyExporterJson。但是,导出器在设置子类型之前就已解决...

我知道 Castle::Windsor 有一些称为基于委托的工厂,但我根本不知道如何使用它....

任何帮助将不胜感激,TIA

索伦

4

1 回答 1

3

使用 TypeFactoryFacility 和自定义 ITypedFactoryComponentSelector 的组合。这应该适用于您的情况。

首先,为工厂创建一个接口:

public interface IMyExporterFactory
{
    IMyExporter GetExporter(IMyContext context);
}

接下来,使用自定义的工厂组件选择器,它将使用上下文的子类型来确定组件名称(并更改注册以命名您的导出器):

public class ExporterComponentSelector : DefaultTypedFactoryComponentSelector
{
    protected override string GetComponentName(MethodInfo method, object[] arguments)
    {
        if (method.Name == "GetExporter")
        {
            var context = (IMyContext) arguments[0];
            return context.subtype;
        }

        return base.GetComponentName(method, arguments);
    }
}

这是您的 Windsor 注册代码的更新,其中包括 TypedFactoryFacility 和自定义选择器(它根据导出器的子类型命名您的导出器):

var container = new WindsorContainer();
container.AddFacility<TypedFactoryFacility>();
container.Register(
    Component.For<IMyExporterFactory>().AsFactory(c => c.SelectedWith(new ExporterComponentSelector())),
    Component.For<IMyExporter>().ImplementedBy<MyExporterJson>().Named("json"),
    Component.For<IMyExporter>().ImplementedBy<MyExporterXML>().Named("xml"),
    Component.For<IMyContext>().ImplementedBy<MyContext>(),
    Component.For<MyService>()
    );

现在您的服务只需接收一个 IMyExporterFactory 并使用它来解析导出器:

public class MyService
{
    private readonly IMyContext context;
    private readonly IMyExporterFactory exporterFactory;

    public MyService(IMyContext context, IMyExporterFactory exporterFactory)
    {
        this.context = context;
        this.exporterFactory = exporterFactory;
    }

    public string Extractdata()
    {
        var exporter = exporterFactory.GetExporter(context);
        return exporter.Export();
    }
}

您可能希望确保如果组件使用小写名称(“xml”、“json”)注册,那么您的代码始终使用小写名称(或在 ExporterComponentSelector 中使用 context.subtype.ToLower())。

于 2011-10-07T12:22:44.683 回答