国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

nhibernate源碼分析之二:會話工廠

2024-07-21 02:21:02
字體:
來源:轉載
供稿:網友
會話工廠是nhibernate中的關鍵類,它與數據庫連接、數據庫事務等進行交互,還存儲著與所有持久對象關聯的持久化對象,持久化類是持久化的關鍵,它實現基本的crud操作。
當用戶需要持久操作時,由會話工廠創建一個會話供用戶進行持久操作。

1. 會話工廠的創建

會話工廠由isessionfactory接口實現,由configuration的buildsessionfactory方法創建,會話工廠應該使用singleton模式。
如果要訪問多個數據庫,應建立多個會話工廠。

//*** configuration.cs ***

public isessionfactory buildsessionfactory() {
// ...

hashtable copy = new hashtable();
foreach(dictionaryentry de in properties) {
copy.add(de.key, de.value);
}
return new sessionfactoryimpl(this, copy, interceptor);
}
其中sessionfactoryimpl為實現isessionfactory的類,這個類的修飾為internal。

2. 持久化類的創建

持久化類用于對持久對象進行持久化操作,每一個持久對象都有一個與之關聯的持久化對象。
持久化類繼承自iclasspersister接口,這個接口定義了用于持久對象的crud操作。

//*** sessionfactoryimpl ***

private idictionary classpersisters;
持久化對象集合,key為持久對象的類型;

private idictionary classpersistersbyname;
持久化對象集合,key為持久對象的類名;

public sessionfactoryimpl(configuration cfg, idictionary properties, iinterceptor interceptor) {
// ...

foreach(persistentclass model in cfg.classmappings) {
system.type persisterclass = model.persister;
iclasspersister cp;
//todo: h2.0.3 created a persisterfactory
if (persisterclass==null || persisterclass==typeof(entitypersister)) {
cp = new entitypersister(model, this);
} else if (persisterclass==typeof(normalizedentitypersister)) {
cp = new normalizedentitypersister(model, this);
} else {
cp = instantiatepersister(persisterclass, model);
}
classpersisters[model.persistentclazz] = cp;
classpersistersbyname[model.name] = cp ;
}

// ...
}
在構造函數中遍歷所有持久類映射信息,然后根據持久類的持久類型建立一個持久化對象,并將此對象加入到集合中。
instantiatepersister用于創建一個自定義的持久化對象,類名稱由映射文件中class節點的persister屬性指定,自定義持久化類必須實現iclasspersister接口.

3. 連接提供者

連接提供者由iconnectionprovider接口實現,會話工廠通過連接提供者與持久機制(數據庫等)進行交互,例如取得數據庫連接等。

//*** sessionfactoryimpl.cs ***

public sessionfactoryimpl(configuration cfg, idictionary properties, iinterceptor interceptor) {
// ...
connectionprovider = connectionproviderfactory.newconnectionprovider(properties);
// ...
}
還是在構造函數中,連接提供者由連接提供者工廠根據配置屬性來創建。

//*** connectionproviderfactory ***

public static iconnectionprovider newconnectionprovider(idictionary settings) {
iconnectionprovider connections = null;
string providerclass = settings[cfg.environment.connectionprovider] as string;
if (providerclass != null) {
try {
connections = (iconnectionprovider) activator.createinstance(system.type.gettype(providerclass));
}
catch (exception e) {
throw new hibernateexception("could not instantiate connection provider: " + providerclass);
}
}
else {
throw new notimplementedexception("we have not implemented user supplied connections yet.");
}

connections.configure(settings);
return connections;
}
newconnectionprovider方法通過配置中connectionprovider的值來創建連接提供者。當前版本(v0.0.5)唯一可用的提供者只有driverconnectionprovider類。

// 部分內容等有空再補充。

菜鳥學堂:
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 利辛县| 定安县| 榆林市| 蒙自县| 竹溪县| 五莲县| 连江县| 定兴县| 自治县| 丰原市| 桂平市| 南皮县| 福海县| 华阴市| 上思县| 阳城县| 长宁区| 宁明县| 汨罗市| 喀什市| 普宁市| 丹巴县| 石首市| 上林县| 海丰县| 岳池县| 阜南县| 子长县| 松阳县| 长兴县| 云和县| 高要市| 遂平县| 颍上县| 仁寿县| 新邵县| 泗水县| 松江区| 洛川县| 濮阳市| 日土县|