在產(chǎn)品項(xiàng)目進(jìn)行中我們難免會(huì)遇到因?yàn)轫?xiàng)目服務(wù)對(duì)象不同,實(shí)現(xiàn)也不同的功能。
這樣每次修改后重新編譯整個(gè)解決方案后更新耗時(shí)耗力。
其實(shí)使用靜態(tài)工廠,我們可以很容易的引入額外的dll來(lái)動(dòng)態(tài)的配置這些功能的實(shí)現(xiàn)
首先下面是所有工廠的基類,其中包含了一個(gè)讀取配置文件的方法,和一個(gè)生成對(duì)象的靜態(tài)方法
 代碼
/// <summary> /// 所有工廠基類 ///<author name="Jan.David"></author> ///<![CDATA[2010-11-05]]> /// </summary>public abstract class AbstractFactory { /// <summary> /// 讀取設(shè)置文件內(nèi)的值 /// </summary> /// <param name="parentNode"></param> /// <param name="key"></param> /// <returns></returns> public static string ReadSettring(string parentNode,string key) { string FilePath = AppDomain.CurrentDomain.BaseDirectory;//項(xiàng)目路徑 //新建xml文檔對(duì)象 System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); //讀取文檔 doc.Load(FilePath + "/Setting.xml"); //獲取根節(jié)點(diǎn) System.Xml.XmlNodeList Root = doc.FirstChild.NextSibling.ChildNodes;
System.Xml.XmlNodeList nodes = default(System.Xml.XmlNodeList); foreach (System.Xml.XmlNode node in Root) { if (node.Name.ToLower() == parentNode.ToLower()) { nodes = node.ChildNodes; break; } } if (nodes != null && nodes.Count > 0) { foreach (System.Xml.XmlNode node in nodes) { if (node.Attributes["name"].Value.ToLower() == key.ToLower()) { return node.Attributes["value"].Value; } } } return null; } /// <summary> /// 實(shí)例化一個(gè)對(duì)象 /// </summary> /// <typeparam name="T">要實(shí)例化的類型</typeparam> /// <param name="ClassName">需要實(shí)例化的類名</param> /// <param name="obj">實(shí)例化對(duì)象存儲(chǔ)的變量</param> public abstract void CreateInstanc<T>(string className,ref T obj); }

下面是具體的實(shí)現(xiàn)
 代碼
配置文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <BillCode> <add name="Returns" value ="IBillCodeImplt.Returns"></add> </BillCode>
</configuration>
|