nhibernate原始碼分析之一 物件對映

2021-04-13 00:23:57 字數 1952 閱讀 7543

1. 持久物件對映檔案

關於持久物件對映檔案,這裡就不多說了,可參考nhibernate的例子和文件。

2. 對映資訊的讀取

通過configuration類,可以用多種方式讀取對映資訊,一些以add開頭的方法就是用來加入對映資訊的,這些方法最終將呼叫add(xmldocument doc)。

//** configuration.cs **

private hashtable classes = new hashtable();

classes集合用於存放所有的持久物件對映資訊,

它的key為持久類的型別;value為permissionclass類的子類。

private void add(xmldocument doc)

throw me;

} // end try/catch

}3. 建立物件對映資訊

binder類的bindroot用於繫結對映資訊中的所有對映內容。

//** binder.cs **

// ...

foreach(xmlnode n in hmnode.selectnodes(nsprefix + ":class", n**gr) )

// ...

}遍歷所有的類對映節點,然後呼叫bindrootclass來繫結類對映資訊,最後將類對映資訊加到集合中。

其中rootclass為permissionclass的子類。

//tablename

xmlattribute tablenamenode = node.attributes["table"];

string tablename = (tablenamenode==null)

? stringhelper.unqualify( model.persistentclazz.name )

: tablenamenode.value;

xmlattribute schemanode = node.attributes["schema"];

model.table = table;

// ...

}bindrootclass首先呼叫bindclass繫結持久類對映資訊,然後呼叫propertiesfromxml來繫結類屬性。

string classname = node.attributes["name"] == null ? null : node.attributes["name"].value;

// class

try

catch ( exception cnfe )

// ...

}bindclass通過反射來取得持久物件的型別。

string path = model.name;

table table = model.table;

foreach(xmlnode subnode in node.childnodes)

else if ( "many-to-one".equals(name) )

else if ( "any".equals(name) )

else if ( "one-to-one".equals(name) )

else if ( "property".equals(name) )

else if ( "component".equals(name) )

else if ( "subclass".equals(name) )

else if ( "joined-subclass".equals(name) )

if ( value!=null) }}

遍歷所有子節點,然後根據節點型別對進行繫結。(注: 部分內容已刪除)

關於屬性的對映以後有空再詳細研究,只需要知道屬性已加入到rootclass的properties屬性就行了。

NHibernate原始碼分析之三 續 資料持久化

當持久化物件時,顯然必須存在把記錄的值賦值到物件屬性和取得物件屬性的值用於持久化操作,對於更新操作,還需要檢查物件的值是否已發生變化,即是否為dirty,這些操作都是由物件的持久化類來完成的。有關持久化類可參考 會話和持久化操作 一文。下面對nh的原始碼進行分析,以了解nh中資料載入和更新的過程。一...

nhibernate原始碼七 HQL資料載入

nh中,hql是乙個十分強大的物件導向的查詢語言,簡單的說,就是不需要使用實際的表名和列名來查詢資料,而改用類名和屬性。有兩種方式來執行hql資料載入,一種是直接使用isession的find方法,另一種是使用iquery介面。iquery介面提供了一些額外的設定,最重要的就是分頁了,這個和icri...

nhibernate原始碼分析之一 物件對映

1.持久物件對映檔案 關於持久物件對映檔案,這裡就不多說了,可參考nhibernate的例子和文件。2.對映資訊的讀取 通過configuration類,可以用多種方式讀取對映資訊,一些以add開頭的方法就是用來加入對映資訊的,這些方法最終將呼叫add xmldocument doc configu...