IbatisNet一下簡稱Ibatis包括DataAccess和DataMapper兩部分。整個(gè)Solution包括三個(gè)主項(xiàng)目:
IBatisNet.Common
IBatisNet.DataAccess
IBatisNet.DataMapper
和一個(gè)輔助項(xiàng)目:IBatisNet.Common.Logging.Log4Net。
Common項(xiàng)目是DataAccess和DataMapper的公共基礎(chǔ),提供通用功能和公共服務(wù)。DataAccess是DAO框架,DataMapper是SqlMap映射框架。Common.Logging.Log4Net是對log4net日志服務(wù)的代理,利用log4net日志框架產(chǎn)生和輸出日志。
二.各項(xiàng)目介紹
Commons
根目錄:
DataSource類封裝了數(shù)據(jù)源信息,包括數(shù)據(jù)源的標(biāo)志名Name,數(shù)據(jù)源的提供者Provider和數(shù)據(jù)源所代表的連接字符串ConnectionString。DataSource類有下列屬性:
[Serializable]
[XmlRoot("dataSource", Namespace="http://ibatis.apache.org/dataMapper")]
類中的Property(屬性)也有[XmlAttribute]屬性(Attribute),例如public string ConnectionString屬性有[XmlAttribute("connectionString")]。
所以類可被反序列化為xml文件,也可以直接通過XmlNode解析為對象。DataSourceDeSerializer類就是從XmlNode中讀取信息并生成DataSource類。
DataSource屬性和配置文件中的dataSource節(jié)點(diǎn)對應(yīng)。
Provider類封裝了data provider(數(shù)據(jù)提供者)信息,例如程序集名稱AssemblyName,是否是默認(rèn)提供者IsDefault,ConnectionClass,DataAdapterClass等提供者程序的信息。并提供了IDbCommand GetCommand(),IDbDataAdapter GetDataAdapter()等方法,為上層應(yīng)用隱藏了具體驅(qū)動(dòng)(數(shù)據(jù)提供程序)的細(xì)節(jié)。與DataSource一樣Provider也是可以序列化為xml文件,并通過ProviderDeSerializer.cs中的ProviderDeSerializer類從XmlNode中構(gòu)造Provider實(shí)例。
IDalSession類是Ibatis中的基礎(chǔ)接口。封裝了數(shù)據(jù)訪問相關(guān)的會(huì)話信息,比如數(shù)據(jù)源,連接和事物對象。用于管理數(shù)據(jù)源相關(guān)信息,比如:
IDbCommand CreateCommand(CommandType commandType);
///
/// Create an IDataParameter
///
/// An IDataParameter.
IDataParameter CreateDataParameter();
///
/// Create a DataAdapter
///
/// The statement or stored procedure
/// used to select records in the data source.
/// An IDbDataAdapter.
IDbDataAdapter CreateDataAdapter(IDbCommand command);
///
/// Create a DataAdapter
///
/// An IDbDataAdapter.
IDbDataAdapter CreateDataAdapter();
連接管理的相關(guān)方法,比如:
///
/// Open a connection.
///
void OpenConnection();
///
/// Open a connection, on the specified connection string.
///
/// The connection string
void OpenConnection(string connectionString);
///
/// close a connection
///
void CloseConnection();
事務(wù)管理相關(guān)的方法,比如:
///
/// Begins a transaction on the current connection
/// with the specified IsolationLevel value.
///
/// The transaction isolation level for this connection.
/// Open a connection.
void BeginTransaction(bool openConnection, IsolationLevel isolationLevel);
///
/// Commit a transaction and close the associated connection
///
void CommitTransaction();
///
/// Commits the database transaction.
///
/// Close the connection
void CommitTransaction(bool closeConnection);
///
/// Rollbak a transaction and close the associated connection
///
void RollBackTransaction();
///
/// Rolls back a transaction from a pending state.
///
/// Close the connection
void RollBackTransaction(bool closeConnection);
等。
Exception目錄:
Exception目錄中的類定義了框架的異常類型。
IBatisNetException是框架基礎(chǔ)類,其他框架異常都繼承自它。
ConfigurationException類用于處理配置過程中出現(xiàn)的異常。
ForeignKeyException類用于代表外鍵異常和錯(cuò)誤。
ProbeException類代表較表層的異常,通過該異常可以探究深層原因,也用于通過流節(jié)序列化并組裝成異常實(shí)例的過程(不是太清楚)。框架中在ReflectionInfo和ObjectProbe中有使用。
Logging目錄:
Logging目錄類似于apache jarkata中的commons log,對log進(jìn)行了封裝,可以同時(shí)支持多種log實(shí)現(xiàn),并通過配置文件進(jìn)行配置。不詳細(xì)說明。
Pagination目錄:
Pagination目錄輔助查詢分類。
PaginatedArrayList類實(shí)現(xiàn)了IPaginatedList接口。用于維護(hù)頁碼和頁中的數(shù)據(jù)內(nèi)容。維護(hù)分頁狀態(tài)實(shí)現(xiàn)分頁算法。PaginatedArrayList和具體的數(shù)據(jù)訪問無關(guān),內(nèi)部通過ArrayList容器分類。
Transaction目錄:
Transaction目錄中的類用于事務(wù)管理。
IsolationLevel枚舉定義了事務(wù)的隔離級別。
TransactionOptions結(jié)構(gòu)指明了事務(wù)行為:事務(wù)時(shí)間段和隔離級別。
TransactionScopeOptions枚舉描述了事務(wù)范圍(Transaction scope)和事務(wù)的關(guān)聯(lián)關(guān)系。
TransactionScope用于管理分布式事務(wù),不過只支持1.1而且對操作系統(tǒng)由要求,不支持win2000。
Utilities目錄:
Utilities目錄中是一些公用類。
ConfigWatcherHandler類用于監(jiān)視配置文件以便在配置文件修改的時(shí)候進(jìn)行相應(yīng)的處理,比如重新加載并處理配置文件。該類中注意Timer類和FileSystemWatcher類的用法。
DBHelperParameterCache類用于對存儲過程參數(shù)的緩存。
HashCodeProvider類的public static int GetIdentityHashCode(object obj)方法返回obj的HashCode。
Resources類用于簡化資源文件的訪問。可以通過流,文件和url獲取資源文件,并解析為XmlDocument。
ScriptRunner類用于執(zhí)行sql腳本語句。
StringTokenizer類類似于java中的StringTokenizer,可以分割字符串為字符串?dāng)?shù)組。
Utilities/Objects目錄:
Utilities/Objects目錄下的類主要用于反射。
ObjectProbe由于獲取對象的反射信息和運(yùn)行時(shí)值信息。
ReflectionInfo主要緩存了類的定義信息,可以使property和get/set方法的映射更加簡單。
Utilities/Proxy目錄:
Utilities/Proxy目錄利用Castle DynamicProxy的代理類。
CachedProxyGenerator繼承自Castle DynamicProxy的ProxyGenerator,通過CreateProxy方法利用ProxyBuilder.CreateInterfaceProxy(interfaces, targetType )創(chuàng)建動(dòng)態(tài)代理。
IProxyGenerator一個(gè)標(biāo)志接口。
ProxyGeneratorFactory工廠類,獲取Castle DynamicProxy的一個(gè)ProxyGenerator實(shí)例。
Utilities/TypesResolver目錄:
Utilities/TypesResolver目錄中的類用于類型解析。也就是利用反射和程序集從字符串構(gòu)造出相應(yīng)的類型。
TypeResolver的Type Resolve (string typeName)方法從程序集中獲取類型信息。
CachedTypeResolver繼承自TypeResolver,增加了緩存功能。
TypeAliasResolver類定義了Ibatis內(nèi)部的別名和實(shí)際類型的映射,例如list-〉new ArrayList(),以及將系統(tǒng)的內(nèi)置值類型變換為相應(yīng)的對象類型。
Xml目錄:
Xml目錄包含一個(gè)NodeUtils類,主要用于從XmlNode中解析屬性,和從NameValueCollection中獲取值,ParsePropertyTokens(string str, NameValueCollection properties) 方法用于將str中的占位符用相應(yīng)的properties中的值替代。str中可以有多個(gè)占位符屬性。