FactorySupport Facility
The FactorySupport Facility make it possible to your application to use external factories.
| Facility Summary | |||||||||||
|
| ||||||||||
Introduction
Sometimes you have to use an existing object model and would like to integrate it with your container, thus allowing you to use auto-wiring and so on. For example, imagine that CompoentX is only created through CompoentXFactory class, which exposes a static method Create.
This facility allows you to use the following strategies:
- An accessor to have access to a component instance
- A static or instance method in a factory to create component instances
Quick start
Just install the facility and add the proper configuration.
<configuration> <facilities> <facility id="factory.support" type="Castle.Facilities.FactorySupport.FactorySupportFacility, Castle.MicroKernel" /> </facilities> </configuration>
Configuration Schema
<components> <component id="mycomp1" instance-accessor="Static accessor name" /> <component id="factory1" /> <component id="mycomp2" factoryId="factory1" factoryCreate="Create" /> </components>
Accessor example
Given the following singleton class:
public class SingletonWithAccessor { private static readonly SingletonWithAccessor instance = new SingletonWithAccessor(); private SingletonWithAccessor() { } public static SingletonWithAccessor Instance { get { return instance; } } }
You may expose its instance to the container through the following configuration:
<components> <component id="mycomp1" type="Company.Components.SingletonWithAccessor, Company.Components" instance-accessor="Instance" /> </components>
Using it:
SingletonWithAccessor comp = (SingletonWithAccessor) container["mycomp1"];
Factory example
Given the following component and factory classes:
public class MyComp { internal MyComp() { } ... } public class MyCompFactory { public MyComp Create() { return new MyComp(); } }
You may expose its instance to the container through the following configuration:
<components> <component id="mycompfactory" type="Company.Components.MyCompFactory, Company.Components"/> <component id="mycomp" type="Company.Components.MyComp, Company.Components" factoryId="mycompfactory" factoryCreate="Create" /> </components>
Using it:
MyComp comp = (MyComp) container["mycomp"];
Factory with parameters example
Given the following component and factory classes:
public class MyComp { internal MyComp(String storeName, IDictionary props) { } ... } public class MyCompFactory { public MyComp Create(String storeName, IDictionary props) { return new MyComp(storeName, props); } }
You may expose its instance to the container through the following configuration:
<components> <component id="mycompfactory" type="Company.Components.MyCompFactory, Company.Components"/> <component id="mycomp" type="Company.Components.MyComp, Company.Components" factoryId="mycompfactory" factoryCreate="Create"> <parameters> <storeName>MyStore</storeName> <props> <dictionary> <entry key="key1">item1</entry> <entry key="key2">item2</entry> </dictionary> </props> </parameters> </component> </components>
Using it:
MyComp comp = (MyComp) container["mycomp"];
Factory using auto-wire example
If your factory request as parameter some other component instance, this facility will be able to resolve it without your aid:
public class MyComp { internal MyComp(IMyService serv) { } ... } public class MyCompFactory { public MyComp Create(IMyService service) { return new MyComp(service); } }
You may expose its instance to the container through the following configuration:
<facilities> <facility id="factorysupport" type="Castle.Facilities.FactorySupport.FactorySupportFacility, Castle.Facilities.FactorySupport"/> </facilities> <components> <component id="myservice" service="SomethingElse.IMyService" type="Company.Components.MyServiceImpl, Company.Components" /> <component id="mycompfactory" type="Company.Components.MyCompFactory, Company.Components" /> <component id="mycomp" type="Company.Components.MyComp, Company.Components" factoryId="mycompfactory" factoryCreate="Create" /> </components>
Using it:
MyComp comp = (MyComp) container["mycomp"];