1

我知道如何告诉 Castle Windsor 使用 XML 解析来自工厂方法的引用,但我可以通过 Container.AddComponent() 接口以编程方式完成吗?如果没有,还有其他方法可以从代码中做到这一点吗?

编辑: 似乎有些混乱,所以让我澄清一下,我正在寻找一种在代码中执行以下操作的方法:

<facilities>
   <facility
        id="factory.support"
        type="Castle.Facilities.FactorySupport.FactorySupportFacility, Castle.MicroKernel"
    />

</facilities>

<components>

    <component
        id="CustomerRepositoryFactory"
        type="ConsoleApplication2.CustomerRepositoryFactory, ConsoleApplication2"
    />

    <component
        id="CustomerRepository"
        service="ConsoleApplication2.ICustomerRepository, ConsoleApplication2"
        type="ConsoleApplication2.CustomerRepository, ConsoleApplication2"
        factoryId="CustomerRepositoryFactory"
        factoryCreate="Create"
    />

</components>

来自这篇关于 Windsor 和 spring.net 工厂支持的 codebetter 文章

4

1 回答 1

3

直接来自单元测试 FactorySupportTestCase(这是你的朋友):

[Test]
    public void FactorySupport_UsingProxiedFactory_WorksFine()
    {
        container.AddFacility("factories", new FactorySupportFacility());
        container.AddComponent("standard.interceptor", typeof(StandardInterceptor));
        container.AddComponent("factory", typeof(CalulcatorFactory));

        AddComponent("calculator", typeof(ICalcService), typeof(CalculatorService), "Create");

        ICalcService service = (ICalcService) container["calculator"];

        Assert.IsNotNull(service);
    }

    private void AddComponent(string key, Type service, Type type, string factoryMethod)
    {
        MutableConfiguration config = new MutableConfiguration(key);
        config.Attributes["factoryId"] = "factory";
        config.Attributes["factoryCreate"] = factoryMethod;
        container.Kernel.ConfigurationStore.AddComponentConfiguration(key, config);
        container.Kernel.AddComponent(key, service, type);
    }
于 2008-09-15T16:38:42.733 回答