温莎城堡:你如何添加一个调用的XML工厂设施不? [英] Castle Windsor: How do you add a call to a factory facility not in xml?

查看:136
本文介绍了温莎城堡:你如何添加一个调用的XML工厂设施不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何告诉温莎城堡解决从使用XML工厂的方法的引用,但我可以通过Container.AddComponent()接口编程方式做到这一点?如果不是还有什么其他办法从code办呢?

I know how to tell Castle Windsor to resolve a reference from a factory's method using XML, but can I do it programmatically via the Container.AddComponent() interface? If not is there any other way to do it from code?

编辑: 似乎有一些混乱,所以让我澄清,我正在寻找一种方式做code以下内容:

There seems to be some confusion so let me clarify, I am looking for a way to do the following in code:

<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>

(<一href="http://$c$cbetter.com/blogs/david.hayden/archive/2007/12/13/factory-method-support-in-castle-windsor-and-spring-net.aspx"相对=nofollow>从温莎的工厂支持这个codebetter文章和spring.net )

推荐答案

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

Directly from the Unit Test FactorySupportTestCase (which are your friends):

[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);
	}

这篇关于温莎城堡:你如何添加一个调用的XML工厂设施不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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