2008-09-15 35 views
1

我知道如何告訴Castle Windsor使用XML解析工廠方法的引用,但是我可以通過Container.AddComponent()接口以編程方式執行它嗎?如果沒有,有沒有其他的方式來從代碼中做到這一點?Castle Windsor:你如何在不使用xml的情況下添加對工廠設施的調用?

編輯: 似乎有一些混亂,所以讓我澄清,我正在尋找一種方式做的代碼如下:

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

from this codebetter article on factory support in windsor and spring.net

回答

3

直接Unit Test 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); 
    } 
相關問題