Main content
Principle Analysis of Typedfactory facility
......
In Typedfactory facility, there is a factoryentry class that is similar to the entity classes in our usual project development, which is used to record information about the factory, including the ID of the factory, the factory interface, the method of creation, and the method of destruction. This class is implemented as follows:
public class FactoryEntry
{
private String _id;
private Type _factoryInterface;
private String _creationMethod;
private String _destructionMethod;
public FactoryEntry(String id, Type factoryInterface, String creationMethod, String destructionMethod)
{
// 省略了验证及异常处理
_id = id;
_factoryInterface = factoryInterface;
_creationMethod = creationMethod;
_destructionMethod = destructionMethod;
}
public String Id
{
get { return _id; }
}
public Type FactoryInterface
{
get { return _factoryInterface; }
}
public String CreationMethod
{
get { return _creationMethod; }
}
public String DestructionMethod
{
get { return _destructionMethod; }
}
}
Typedfactoryfacility is also inherited from Abstractfacility, about facility's inheritance relationship I have already said in the previous article. Typedfactory facility first gets the type of the factory in the initialization, through the subsystem:
protected override void Init ()
{
Kernel.addcomponent ("Typed.fac.interceptor", typeof ( Factoryinterceptor));
Itypeconverter converter = (itypeconverter)
Kernel.getsubsystem (Subsystemconstants.conversionmanagerkey);
Addfactories (Facilityconfig, Converter);
}
Protected virtual void addfactories (IConfiguration facilityconfig, itypeconverter Converter)
{
if (FA Cilityconfig!= null)
{
foreach (iconfiguration config in facilityconfig.children["factories"). Children)
{
String id = config. attributes["id"];
String creation = CONFIG. attributes["Creation"];
String destruction = config. Attributes["Destruction"];
Type Factorytype = (type)
Converter. Performconversion (config. Attributes["Interface"], typeof (Type));
Try
{
Addtypedfactoryentry (
New Factoryentry (ID, factorytype, creation, des truction));
}
CATCH (Exception ex)
{
throw new ConfigurationException ("Invalid Factory entry in Configuration", ex); br>}
}
}