关于ORM中只有XML没有映射实体的思考?期待大家的建议
開篇
?????? 很久沒有寫文章了,之前開了太多的系列,一方面是由于自己對于工作中的思考,另一方面是自己在業務時間中的對知識的總結,這里也是比較抱歉,因為之前開的系列,一直都是
開,完整寫完的不多,這里實在是對不住大家了,我也是想等過年期間好好的整理下,這些系列和思路,將要寫的內容,都梳理清楚,然后在年后,將一一發布,完善如下的幾個系列:
?????? 1、Step by Step-構建自己的ORM系列-索引
?????? 2、設計模式-系列索引
?????? 3、系統架構師-基礎到企業應用架構-系列索引
?????? 4、云計算-從基礎到應用架構系列索引
???????? 當然,我期間可能會還有其他的相關文章會開啟,但是我不會影響這些文章的進步,不然就太對不起大伙了,也希望大家多多提出自己的想法和意見,有了你們的幫助,我才能寫的
更完善。謝謝!
摘要
???????? 最近在項目中也是遇到如下的問題,也是查看了一些相應的資料,但是都沒有很好的解決,問題如下:
???????? 1、如何只通過XML文件來完成映射,當然這里的XML負責ORM中的數據庫表列的相應信息。
???????? 2、不想為數據庫中的每個表都建立實體對象。
???????? 3、能夠仍然在界面使用的過程中方便的操作。
???????? 4、還要考慮一些適應性和低耦合性方面的要求。
???????? 有了上面的幾個要求,經過思考,得出一下的幾個思路,但是都不是理想的解決方案,還請大家看后以下的幾個解決方案,給出意見。
解決思路
??????????????????? 上面也是給出了幾個問題,我對這幾個問題進行了整理和思考,得出下面的幾個解決方案,但是都不是特別的理想,稍微的深入提出需求,就無法滿足。
?????? 一、通過NHibernate
??????????? 當然,Nhibernate本身已經提供了這方面的操作,我特別的舉例說明如下,如果想詳細的查看,請參考園子里面的高手博客《李永京》的。
??????????? 我這里將代碼貼出:
??????????? 1、我們先看映射文件
<class entity-name="Contract"><id name="Id" type="int"><generator class="Test"/></id><property name="Name" not-null="true" length="25" type="string"/><property name="Pay" not-null="true" type="decimal"/> <property name="CreateTime" not-null="true" type="datetime"/> <property name="Description" not-null="true" length="200" type="string"/></class>??????????? 2、其他的配置,我就不詳細說了,關于Nhibernate的配置。下面給出相應的示例代碼:
??? using (ISession session = new SessionFactory().OpenSession())
??? {
??????? using (ITransaction trans = session.BeginTransaction())
??????? {
??????????? IDictionary contract = new Hashtable();
??????????? contract["Name"] = "合同名稱";
??????????? contract["Pay"] = 0.0M;//合同的金額
??????????? contract["Description"] = "合同的描述信息";
??????????? //第一個參數為映射中使用的實體名稱,第二個參數為實例
??????????? session.Save("Contract", contract);
??????????? trans.Commit();
??????? }
??? }
??????????? 上面給出的保存的代碼,更新的代碼雷同。給出查詢代碼:
??? using (ISession session = new SessionFactory().OpenSession())
??? {
??????? using (ITransaction trans = session.BeginTransaction())
??????? {
??????????? IDictionary contract =(IDictionary) session.CreateQuery(“ from Contract where ContractID=:id”)
?????????????????????????????????????????????????? .Add(“id”,1);
?????????????????????????????????????????????????? .UniqueResult();
??????????? session.Clear();
??????????? trans.Commit();
??????? }
??? }
??????????? 通過上面的代碼,經過測試,的確能夠將數據,通過Nhibernate提供的相應方法,完成訪問。其他的相應的操作,我就不給出了,但是通過上面我們知道,我們沒有通過創建相應的實體,我
們就能完成映射的操作,的確不錯,不過,這樣的實現,的確有些不便。
?
????????????? 二、通過自定義對象
????????????? 這個自定義對象應該如何來做呢?自定義對象的思路如下:
?????????????
???????????? 下面來配合這個圖給出示例代碼,不一定可以運行,具體的公共對象類代碼如下:
public class CommonObject : IList
??? {
??????? private IDictionary<string, Column> columns = null;
??????? public CommonObject()
??????? {
??????????? columns = new Dictionary<string, Column>();
??????? }
??????? public CommonObject(int capacity)
??????? {
??????????? columns = new Dictionary<string, Column>(capacity);
??????? }
??????? public Column Add(Column col)
??????? {
??????????? this.columns.Add(col.Name,col);
??????????? return col;
??????? }
??????? public bool Remove(Column col)
??????? {
??????????? return this.columns.Remove(col.Name);
??????? }
??????? public Column this[string key]
??????? {
??????????? get
??????????? {
??????????????? return this.columns[key];
??????????? }
??????????? set
??????????? {
??????????????? this.columns[key] = value;
??????????? }
??????? }
??????? public int Add(object value)
??????? {
??????????? throw new NotImplementedException();
??????? }
??????? public void Clear()
??????? {
??????????? throw new NotImplementedException();
??????? }
??????? public bool Contains(object value)
??????? {
??????????? throw new NotImplementedException();
??????? }
??????? public int IndexOf(object value)
??????? {
??????????? throw new NotImplementedException();
??????? }
??????? public void Insert(int index, object value)
??????? {
??????????? throw new NotImplementedException();
??????? }
??????? public bool IsFixedSize
??????? {
??????????? get
??????????? {
??????????????? throw new NotImplementedException();
??????????? }
??????? }
??????? public bool IsReadOnly
??????? {
??????????? get
??????????? {
??????????????? throw new NotImplementedException();
??????????? }
??????? }
??????? public void Remove(object value)
??????? {
??????????? throw new NotImplementedException();
??????? }
??????? public void RemoveAt(int index)
??????? {
??????????? throw new NotImplementedException();
??????? }
??????? public object this[int index]
??????? {
??????????? get
??????????? {
??????????????? throw new NotImplementedException();
??????????? }
??????????? set
??????????? {
??????????????? throw new NotImplementedException();
??????????? }
??????? }
??????? public void CopyTo(Array array, int index)
??????? {
??????????? throw new NotImplementedException();
??????? }
??????? public int Count
??????? {
??????????? get
??????????? {
??????????????? throw new NotImplementedException();
??????????? }
??????? }
??????? public bool IsSynchronized
??????? {
??????????? get
??????????? {
??????????????? throw new NotImplementedException();
??????????? }
??????? }
??????? public object SyncRoot
??????? {
??????????? get
??????????? {
??????????????? throw new NotImplementedException();
??????????? }
??????? }
??????? public IEnumerator GetEnumerator()
??????? {
??????????? throw new NotImplementedException();
??????? }
??? }
?????????? 具體的代碼上面已經給出,下面給出Column的示例代碼:
class Column
??? {
??????? public string Name
??????? {
??????????? get;
??????????? set;
??????? }
??????? public object Value
??????? {
??????????? get;
??????????? set;
??????? }
??????? public System.Data.DbType DataType
??????? {
??????????? get;
??????????? set;
??????? }
??????? public string? DbColumnName
??????? {
??????????? get;
??????????? set;
??????? }
??????? public int Length
??????? {
??????????? get;
??????????? set;
??????? }
??????? public bool IsNull
??????? {
??????????? get;
??????????? set;
??????? }
??? }
??? 當然上面給出的不是完整的,只是為了演示說明。這樣就能完成與XML之間的映射,具體的訪問的過程中也可以相對來說更友好一些。
????? public void Test()
?????? {
?????????? CommonObject comObj = CommonFactory<CommonObject>();
?????????? comObj.Add(new Column());
?????????? comObj["test"] = new Column();
?????? }
?????? private T CommonFactory<T>()
?????? {
?????????? return (T)Activator.CreateInstance(typeof(T));
?????? }
?????? 三、通過.NET 4.0中的動態類型來完成
???????????????? 下面給出實現的思路吧:
????????????????? 我們通過利用.NET4.0中的dynimic來定義一個內存中的實例,該實例是通過XML文件 中的配置的列屬性來動態的創建,具體的代碼如下:
static void Main(string[] args)
??????? {
??????????? dynamic obj=? GetTest();
??????????? Console.WriteLine(obj.name);
??????????? Console.WriteLine(obj.tt);
??????????? System.Threading.Thread.Sleep(10000);
??????? }
??????? private static dynamic GetTest()
??????? {
??????????? dynamic obj = new ExpandoObject();
??????????? var person = obj as IDictionary<string, object>;
??????????? person["name"] = "test";
??????????? person["tt"] = "aaa";
??????????? return obj;
??????? }
??????? 通過上面的這個思路,我們就可以如下來做:
?????? private static dynamic GetTest1(string xmlFile)
??????? {
??????????? dynamic obj = new ExpandoObject();
??????????? var person = obj as IDictionary<string, object>;
??????????? XmlDocument doc = new XmlDocument();
??????????? doc.LoadXml(xmlFile);
??????????? Property [] list = XMLHelper.GetPropertys(doc);
??????????? foreach (Property property in list)
??????????? {
??????????????? person[property.Name] = property.Value;
??????????? }
??????????? person["name"] = "test";
??????????? person["tt"] = "aaa";
??????????? return obj;
??????? }
??????? 這樣就完成了,動態創建對象的過程,并且在應用可以之間使用動態創建的對象,但是有如下問題
???????
??????? 通過上圖中的描述,我想大家都知道,動態類型是動態運行時的編譯,不是靜態編譯,必須在運行時才能解析出動態對象的屬性信息,因此我們使用起來并不方便,所以我想到如下辦法。
?
???????
??????? 我思考完,發現如果要是可以實現這樣的插件也就好了,當然目前的思路就到這,查看了相關職能感知的相關文件,但是沒有發現API可以直接設置或者可以之間進行控制的方法。
其他可行思路
????????? 當然我上面只是給了幾種可能可行的方案,但是不一定是合適的方案,還請大家多多指教和拍磚,如果您有好的思路或者實現方案,還請您提出來,多謝您的指
教。
后續
???????? 下篇如果我有好的實現思路,我會給出完整的實現,當然如果沒有思路,那就暫時寫到這里,多謝大家的集思廣益,可能我鉆牛角尖了,也說不定。
轉載于:https://www.cnblogs.com/hegezhou_hot/archive/2011/01/21/1941450.html
總結
以上是生活随笔為你收集整理的关于ORM中只有XML没有映射实体的思考?期待大家的建议的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle修改表字段
- 下一篇: 了解模型、视图和控制器