創(chuàng)建對象一般有3中方式:1.構造器創(chuàng)建。2.靜態(tài)工廠創(chuàng)建。3.實例工廠創(chuàng)建。
多數(shù)情況下,容器會根據(jù)對象定義中的type屬性值去直接調用響應類型的某個構造器。另外,容器也可以調用工廠方法來創(chuàng)建對象,這時type屬性的值就應該是包含工廠方法的類型(注:而不是要創(chuàng)建的類型,但通過該對象定義的名稱獲取的是由工廠方法所創(chuàng)建的對象)。工廠方法的產(chǎn)生對象可以是工廠方法所在的類型,也可以是其他類型(注:很多情況下工廠方法位于單獨的類型中),這無關緊要。
一、通過構造器創(chuàng)建對象、
通過構造器創(chuàng)建隊形需要滿足這幾個條件:1.指明對象類型type=”類全名,程序集名”
2.有一個無參的構造函數(shù)或者默認構造函數(shù)。
- <!--構造器-->
- <object id="PersonDao" type="Dao.PersonDao, Dao">
- </object>
- /// <summary>
- /// 構造器創(chuàng)建
- /// </summary>
- static void CreateByConstructor()
- {
- string[] XMLFiles = new string[]
- {
- "assembly://CreateObjects/CreateObjects/Objects.xml"
- };
- IApplicationContext context = new XmlApplicationContext(XMLFiles);
- IObjectFactory factory = (IObjectFactory)context;
- Console.Write(factory.GetObject("PersonDao").ToString());
- }
嵌套類型對象的創(chuàng)建需要“+”號來連接被嵌套的類型。如果在PersonDao中嵌套了類型Person
- <!--嵌套類型-->
- <object id="Person" type="Dao.PersonDao+Person, Dao">
- </object>
- /// <summary>
- /// 嵌套類型創(chuàng)建
- /// </summary>
- static void CreateNested()
- {
- string[] XMLFiles = new string[]
- {
- "assembly://CreateObjects/CreateObjects/Objects.xml"
- };
- IApplicationContext context = new XmlApplicationContext(XMLFiles);
- IObjectFactory factory = (IObjectFactory)context;
- Console.Write(factory.GetObject("Person").ToString());
- }
二、靜態(tài)工廠創(chuàng)建
使用靜態(tài)工廠創(chuàng)建對象需要配置factory-mothod屬性
- <!--靜態(tài)工廠-->
- <object id="StaticObjectsFactory" type="DaoFactory.DataAccess, DaoFactory" factory-method="CreatePersonDao">
- </object>
- /// <summary>
- /// 靜態(tài)工廠創(chuàng)建
- /// </summary>
- static void CreateByStaticFactory()
- {
- string[] XMLFiles = new string[]
- {
- "assembly://CreateObjects/CreateObjects/Objects.xml"
- };
- IApplicationContext context = new XmlApplicationContext(XMLFiles);
- IObjectFactory factory = (IObjectFactory)context;
- Console.Write(factory.GetObject("StaticObjectsFactory").ToString());
- }
三、使用實例工廠創(chuàng)建對象
使用實例工廠創(chuàng)建隊形需要先定義一個工廠,然后設factory-object和factory-method屬性,且滿足實例工廠方法所在的對象必須也要配置在同一容器(或父容器)中 和 對象定義就不能包含type屬性
- <!--實例工廠-->
- <!--工廠-->
- <object id="instanceObjectsFactory" type="DaoFactory.DataAccess, DaoFactory">
- </object>
- <!--創(chuàng)建的對象-->
- <object id="instancePersonDao" factory-method="CreateInstance" factory-object="instanceObjectsFactory">
- </object>
- /// <summary>
- /// 實例工廠創(chuàng)建
- /// </summary>
- static void CreateByInstanceFactory()
- {
- string[] xmlFiles = new string[]
- {
- "assembly://CreateObjects/CreateObjects/Objects.xml"
- };
- IApplicationContext context = new XmlApplicationContext(xmlFiles);
- IObjectFactory factory = (IObjectFactory)context;
- Console.WriteLine(factory.GetObject("instancePersonDao").ToString());
- }
四、泛型類型的創(chuàng)建
泛型類型的創(chuàng)建比較類型于以上幾種創(chuàng)建,可以有通過構造器創(chuàng)建,還可以通過靜態(tài)或者工廠創(chuàng)建,但是設置type屬性的時候要注意:左尖括號<要替換成字符串”<”,因為在XML中左尖括號會被認為是小于號。
- <!--創(chuàng)建泛型-->
- <object id="genericClass" type="Dao.GenericClass<int>, Dao" >
- </object>
- /// <summary>
- /// 泛型創(chuàng)建
- /// </summary>
- static void CreateByGenericClass()
- {
- string[] xmlFiles = new string[]
- {
- "assembly://CreateObjects/CreateObjects/Objects.xml"
- };
- IApplicationContext context = new XmlApplicationContext(xmlFiles);
- IObjectFactory factory = (IObjectFactory)context;
- Console.WriteLine(factory.GetObject("genericClass").ToString());
- }