构建基于Dbprovider的DataSession
电脑前段时间中毒没办法清理....现在VM装个工作站有点时间把几年前基于数据访问模式设计分享一下.基实在数据访问组件中大家比较关系的就是如何设计一个基于Dbprovier的数据访问模型,这样可以轻松实现多数据库访问.下面通过代码来描述DbDriver,DbDriverProvider,DataSession的实现过程.
首先要描述的当然是抽象出DbDriver,其实大家没有必要一定要自己想,可以借鉴HIBERNATE看大牛们是如何设计.
Code
public interface IDriver
{
string FormatNameForParameter(string parametername);
IDataParameter GetParameter();
IDataParameter GetParameter(string name, object value);
IDataParameter GetParameter(string name, object value, ParameterDirection direction);
IDbCommand Command { get; }
IDbConnection Connection { get; }
IDbDataAdapter DataAdapter { get; }
string NamedPrefix { get; }
string FormatSql(string sql);
}
public interface IDriver
{
string FormatNameForParameter(string parametername);
IDataParameter GetParameter();
IDataParameter GetParameter(string name, object value);
IDataParameter GetParameter(string name, object value, ParameterDirection direction);
IDbCommand Command { get; }
IDbConnection Connection { get; }
IDbDataAdapter DataAdapter { get; }
string NamedPrefix { get; }
string FormatSql(string sql);
}
MSSQL的实现
Code
/// <summary>
/// SqlServer数据设备提供类
/// </summary>
public class SqlDriver:IDriver
{
/// <summary>
/// 构造SqlServer数据设备对象
/// </summary>
public SqlDriver()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region IDriverType 成员
/// <summary>
/// 获取数据连接对象
/// </summary>
public System.Data.IDbConnection Connection
{
get
{
// TODO: 添加 SqlDriver.Connection getter 实现
return new System.Data.SqlClient.SqlConnection();
}
}
/// <summary>
/// 获取数据填充对象
/// </summary>
public System.Data.IDbDataAdapter DataAdapter
{
get
{
// TODO: 添加 SqlDriver.DataAdapter getter 实现
return new System.Data.SqlClient.SqlDataAdapter();
}
}
/// <summary>
/// 获取数据命令处理对象
/// </summary>
public System.Data.IDbCommand Command
{
get
{
// TODO: 添加 SqlDriver.Command getter 实现
return new System.Data.SqlClient.SqlCommand();
}
}
/// <summary>
/// 获取命令参数对象
/// </summary>
/// <returns>System.Data.IDataParameter</returns>
public System.Data.IDataParameter GetParameter()
{
// TODO: 添加 SqlDriver.GetParameter 实现
return new System.Data.SqlClient.SqlParameter();
}
/// <summary>
/// 获取指定名称和值的参数对象
/// </summary>
/// <param name="name">参数名</param>
/// <param name="value">参数值</param>
/// <returns></returns>
System.Data.IDataParameter Data.IDriver.GetParameter(string name, object value)
{
// TODO: 添加 OracleDriver.NClay.Data.IDriverType.GetParameter 实现
System.Data.IDataParameter dp = GetParameter();
dp.ParameterName = name;
if (value == null)
value = DBNull.Value;
if (value.GetType() == typeof(byte[]))
{
if (value == DBNull.Value)
dp.Value = new byte[0];
else
dp.Value = value;
}
else if (value.GetType() == typeof(DateTime))
{
DateTime dt = (DateTime)value;
DateTime min = DateTime.Parse("1753-1-1");
if (dt < min)
{
dp.Value = min;
}
else
{
dp.Value = value;
}
}
else
{
dp.Value = value;
}
return dp;
}
/// <summary>
/// 获取指定名称和值的参数对象
/// </summary>
/// <param name="name">参数名</param>
/// <param name="value">参数值</param>
/// <param name="direction">参数类型</param>
/// <returns>System.Data.IDataParameter</returns>
System.Data.IDataParameter Data.IDriver.GetParameter(string name, object value, System.Data.ParameterDirection direction)
{
// TODO: 添加 SqlDriver.NClay.Data.IDriverType.GetParameter 实现
System.Data.SqlClient.SqlParameter paremter = new System.Data.SqlClient.SqlParameter(name,value);
paremter.Direction = direction;
return paremter;
}
/// <summary>
/// 参数开头标识符
/// </summary>
public string NamedPrefix
{
get
{
return "@";
}
}
/// <summary>
/// 获取相关参数的名称
/// </summary>
/// <param name="parametername">参数名称</param>
/// <returns>string</returns>
public string FormatNameForParameter(string parametername)
{
return NamedPrefix +parametername;
}
#endregion
#region IDriver 成员
public string FormatSql(string sql)
{
return sql.Replace("@", NamedPrefix);
}
#endregion
}
/// <summary>
/// SqlServer数据设备提供类
/// </summary>
public class SqlDriver:IDriver
{
/// <summary>
/// 构造SqlServer数据设备对象
/// </summary>
public SqlDriver()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region IDriverType 成员
/// <summary>
/// 获取数据连接对象
/// </summary>
public System.Data.IDbConnection Connection
{
get
{
// TODO: 添加 SqlDriver.Connection getter 实现
return new System.Data.SqlClient.SqlConnection();
}
}
/// <summary>
/// 获取数据填充对象
/// </summary>
public System.Data.IDbDataAdapter DataAdapter
{
get
{
// TODO: 添加 SqlDriver.DataAdapter getter 实现
return new System.Data.SqlClient.SqlDataAdapter();
}
}
/// <summary>
/// 获取数据命令处理对象
/// </summary>
public System.Data.IDbCommand Command
{
get
{
// TODO: 添加 SqlDriver.Command getter 实现
return new System.Data.SqlClient.SqlCommand();
}
}
/// <summary>
/// 获取命令参数对象
/// </summary>
/// <returns>System.Data.IDataParameter</returns>
public System.Data.IDataParameter GetParameter()
{
// TODO: 添加 SqlDriver.GetParameter 实现
return new System.Data.SqlClient.SqlParameter();
}
/// <summary>
/// 获取指定名称和值的参数对象
/// </summary>
/// <param name="name">参数名</param>
/// <param name="value">参数值</param>
/// <returns></returns>
System.Data.IDataParameter Data.IDriver.GetParameter(string name, object value)
{
// TODO: 添加 OracleDriver.NClay.Data.IDriverType.GetParameter 实现
System.Data.IDataParameter dp = GetParameter();
dp.ParameterName = name;
if (value == null)
value = DBNull.Value;
if (value.GetType() == typeof(byte[]))
{
if (value == DBNull.Value)
dp.Value = new byte[0];
else
dp.Value = value;
}
else if (value.GetType() == typeof(DateTime))
{
DateTime dt = (DateTime)value;
DateTime min = DateTime.Parse("1753-1-1");
if (dt < min)
{
dp.Value = min;
}
else
{
dp.Value = value;
}
}
else
{
dp.Value = value;
}
return dp;
}
/// <summary>
/// 获取指定名称和值的参数对象
/// </summary>
/// <param name="name">参数名</param>
/// <param name="value">参数值</param>
/// <param name="direction">参数类型</param>
/// <returns>System.Data.IDataParameter</returns>
System.Data.IDataParameter Data.IDriver.GetParameter(string name, object value, System.Data.ParameterDirection direction)
{
// TODO: 添加 SqlDriver.NClay.Data.IDriverType.GetParameter 实现
System.Data.SqlClient.SqlParameter paremter = new System.Data.SqlClient.SqlParameter(name,value);
paremter.Direction = direction;
return paremter;
}
/// <summary>
/// 参数开头标识符
/// </summary>
public string NamedPrefix
{
get
{
return "@";
}
}
/// <summary>
/// 获取相关参数的名称
/// </summary>
/// <param name="parametername">参数名称</param>
/// <returns>string</returns>
public string FormatNameForParameter(string parametername)
{
return NamedPrefix +parametername;
}
#endregion
#region IDriver 成员
public string FormatSql(string sql)
{
return sql.Replace("@", NamedPrefix);
}
#endregion
}
ORACLE的实现
Code
namespace NClay.Data
{
/// <summary>
/// OracleDriver 的摘要说明。
/// </summary>
public class OracleDriver:IDriver
{
public OracleDriver()
{
//
// TODO: 在此处添加构造函数逻辑
// type "System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" string
_DataParameter = Type.GetType("System.Data.OracleClient.OracleParameter, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
_ConnectioType = Type.GetType("System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
_CommandType = Type.GetType("System.Data.OracleClient.OracleCommand, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
_DataAdapterType = Type.GetType("System.Data.OracleClient.OracleDataAdapter, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
}
#region IDriverType 成员
private System.Type _ConnectioType = null;
public System.Data.IDbConnection Connection
{
get
{
// TODO: 添加 OracleDriver.Connection getter 实现
return (System.Data.IDbConnection)Activator.CreateInstance(_ConnectioType);
}
}
private System.Type _DataAdapterType = null;
public System.Data.IDbDataAdapter DataAdapter
{
get
{
// TODO: 添加 OracleDriver.DataAdapter getter 实现
return (System.Data.IDbDataAdapter)Activator.CreateInstance(_DataAdapterType);
}
}
private System.Type _CommandType = null;
public System.Data.IDbCommand Command
{
get
{
// TODO: 添加 OracleDriver.Command getter 实现
return (System.Data.IDbCommand)Activator.CreateInstance(_CommandType);
}
}
private System.Type _DataParameter = null;
public System.Data.IDataParameter GetParameter()
{
// TODO: 添加 OracleDriver.GetParameter 实现
return (System.Data.IDbDataParameter)Activator.CreateInstance(_DataParameter);
}
System.Data.IDataParameter Data.IDriver.GetParameter(string name, object value)
{
// TODO: 添加 OracleDriver.NClay.Data.IDriverType.GetParameter 实现
System.Data.IDataParameter dp = GetParameter();
dp.ParameterName = name;
if (value == null)
value = DBNull.Value;
if(value.GetType() == typeof(byte[]))
{
if (DBNull.Value == null)
dp.Value = new byte[0];
else
dp.Value = value;
}
else
{
dp.Value = value;
}
return dp;
}
System.Data.IDataParameter Data.IDriver.GetParameter(string name, object value, System.Data.ParameterDirection direction)
{
// TODO: 添加 OracleDriver.NClay.Data.IDriverType.GetParameter 实现
System.Data.IDataParameter dp = GetParameter();
dp.ParameterName = name;
dp.Value = value;
dp.Direction = direction;
return dp;
}
public string NamedPrefix
{
get
{
// TODO: 添加 OracleDriver.NamedPrefix getter 实现
return ":";
}
}
public string FormatNameForParameter(string parametername)
{
// TODO: 添加 OracleDriver.FormatNameForParameter 实现
return NamedPrefix +parametername;
}
#endregion
#region IDriver 成员
public string FormatSql(string sql)
{
return sql.Replace(":", NamedPrefix);
}
#endregion
}
}
namespace NClay.Data
{
/// <summary>
/// OracleDriver 的摘要说明。
/// </summary>
public class OracleDriver:IDriver
{
public OracleDriver()
{
//
// TODO: 在此处添加构造函数逻辑
// type "System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" string
_DataParameter = Type.GetType("System.Data.OracleClient.OracleParameter, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
_ConnectioType = Type.GetType("System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
_CommandType = Type.GetType("System.Data.OracleClient.OracleCommand, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
_DataAdapterType = Type.GetType("System.Data.OracleClient.OracleDataAdapter, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
}
#region IDriverType 成员
private System.Type _ConnectioType = null;
public System.Data.IDbConnection Connection
{
get
{
// TODO: 添加 OracleDriver.Connection getter 实现
return (System.Data.IDbConnection)Activator.CreateInstance(_ConnectioType);
}
}
private System.Type _DataAdapterType = null;
public System.Data.IDbDataAdapter DataAdapter
{
get
{
// TODO: 添加 OracleDriver.DataAdapter getter 实现
return (System.Data.IDbDataAdapter)Activator.CreateInstance(_DataAdapterType);
}
}
private System.Type _CommandType = null;
public System.Data.IDbCommand Command
{
get
{
// TODO: 添加 OracleDriver.Command getter 实现
return (System.Data.IDbCommand)Activator.CreateInstance(_CommandType);
}
}
private System.Type _DataParameter = null;
public System.Data.IDataParameter GetParameter()
{
// TODO: 添加 OracleDriver.GetParameter 实现
return (System.Data.IDbDataParameter)Activator.CreateInstance(_DataParameter);
}
System.Data.IDataParameter Data.IDriver.GetParameter(string name, object value)
{
// TODO: 添加 OracleDriver.NClay.Data.IDriverType.GetParameter 实现
System.Data.IDataParameter dp = GetParameter();
dp.ParameterName = name;
if (value == null)
value = DBNull.Value;
if(value.GetType() == typeof(byte[]))
{
if (DBNull.Value == null)
dp.Value = new byte[0];
else
dp.Value = value;
}
else
{
dp.Value = value;
}
return dp;
}
System.Data.IDataParameter Data.IDriver.GetParameter(string name, object value, System.Data.ParameterDirection direction)
{
// TODO: 添加 OracleDriver.NClay.Data.IDriverType.GetParameter 实现
System.Data.IDataParameter dp = GetParameter();
dp.ParameterName = name;
dp.Value = value;
dp.Direction = direction;
return dp;
}
public string NamedPrefix
{
get
{
// TODO: 添加 OracleDriver.NamedPrefix getter 实现
return ":";
}
}
public string FormatNameForParameter(string parametername)
{
// TODO: 添加 OracleDriver.FormatNameForParameter 实现
return NamedPrefix +parametername;
}
#endregion
#region IDriver 成员
public string FormatSql(string sql)
{
return sql.Replace(":", NamedPrefix);
}
#endregion
}
}
ODBC的实现
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Odbc;
using System.Data;
using System.Text.RegularExpressions;
namespace NClay.Data
{
public class OdbcDriver:IDriver
{
#region IDriver 成员
public string FormatNameForParameter(string parametername)
{
return "?";
}
public System.Data.IDataParameter GetParameter()
{
return new OdbcParameter();
}
public System.Data.IDataParameter GetParameter(string name, object value)
{
if (value == null)
value = DBNull.Value;
IDataParameter dp = GetParameter();
dp.ParameterName = "?";
dp.Value = value;
return dp;
}
public System.Data.IDataParameter GetParameter(string name, object value, System.Data.ParameterDirection direction)
{
IDataParameter dp = GetParameter(name,value);
dp.Direction = direction;
return dp;
}
public System.Data.IDbCommand Command
{
get {return new OdbcCommand(); }
}
public System.Data.IDbConnection Connection
{
get { return new OdbcConnection(); }
}
public System.Data.IDbDataAdapter DataAdapter
{
get { return new OdbcDataAdapter(); }
}
public string NamedPrefix
{
get { return "?"; }
}
public string FormatSql(string sql)
{
return Regex.Replace(sql, "@[a-zA-Z0-9_]+", "?");
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Odbc;
using System.Data;
using System.Text.RegularExpressions;
namespace NClay.Data
{
public class OdbcDriver:IDriver
{
#region IDriver 成员
public string FormatNameForParameter(string parametername)
{
return "?";
}
public System.Data.IDataParameter GetParameter()
{
return new OdbcParameter();
}
public System.Data.IDataParameter GetParameter(string name, object value)
{
if (value == null)
value = DBNull.Value;
IDataParameter dp = GetParameter();
dp.ParameterName = "?";
dp.Value = value;
return dp;
}
public System.Data.IDataParameter GetParameter(string name, object value, System.Data.ParameterDirection direction)
{
IDataParameter dp = GetParameter(name,value);
dp.Direction = direction;
return dp;
}
public System.Data.IDbCommand Command
{
get {return new OdbcCommand(); }
}
public System.Data.IDbConnection Connection
{
get { return new OdbcConnection(); }
}
public System.Data.IDbDataAdapter DataAdapter
{
get { return new OdbcDataAdapter(); }
}
public string NamedPrefix
{
get { return "?"; }
}
public string FormatSql(string sql)
{
return Regex.Replace(sql, "@[a-zA-Z0-9_]+", "?");
}
#endregion
}
}
ACCESS的实现
Code
//Copyright (c) 2005-2006 JianHan Fan
//Mail:henryfan@msn.com
//All rights reserved.
//-------------------------------------
using System;
using System.Data.OleDb;
namespace NClay.Data
{
/// <summary>
/// OledbAccess 的摘要说明。
/// </summary>
public class OledbAccess:IDriver
{
public OledbAccess()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region IDriverType 成员
public System.Data.IDbConnection Connection
{
get
{
// TODO: 添加 OledbAccess.Connection getter 实现
return new OleDbConnection();
}
}
public System.Data.IDbDataAdapter DataAdapter
{
get
{
// TODO: 添加 OledbAccess.DataAdapter getter 实现
return new OleDbDataAdapter();
}
}
public System.Data.IDbCommand Command
{
get
{
// TODO: 添加 OledbAccess.Command getter 实现
return new OleDbCommand();
}
}
public System.Data.IDataParameter GetParameter()
{
// TODO: 添加 OledbAccess.GetParameter 实现
return new OleDbParameter();
}
System.Data.IDataParameter Data.IDriver.GetParameter(string name, object value)
{
// TODO: 添加 OledbAccess.NClay.Data.IDriverType.GetParameter 实现
if (value == null)
value = DBNull.Value;
System.Data.IDataParameter dp = GetParameter();
dp.ParameterName = name;
if (value.GetType() == typeof(DateTime))
{
DateTime dt = (DateTime)value;
dp.Value = string.Format("{0}-{1}-{2} {3}:{4}:{5}", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);
}
else if (value.GetType() == typeof(byte[]))
{
if (value == DBNull.Value)
dp.Value = new byte[0];
else
dp.Value = value;
}
else
{
dp.Value = value;
}
return dp;
}
System.Data.IDataParameter Data.IDriver.GetParameter(string name, object value, System.Data.ParameterDirection direction)
{
// TODO: 添加 OledbAccess.NClay.Data.IDriverType.GetParameter 实现
System.Data.IDataParameter dp = GetParameter();
dp.ParameterName = name;
dp.Value = value;
dp.Direction = direction;
return dp;
}
public string NamedPrefix
{
get
{
// TODO: 添加 OledbAccess.NamedPrefix getter 实现
return "@";
}
}
public string FormatNameForParameter(string parametername)
{
// TODO: 添加 OledbAccess.FormatNameForParameter 实现
return NamedPrefix +parametername;
}
#endregion
#region IDriver 成员
public string FormatSql(string sql)
{
return sql.Replace("@", NamedPrefix);
}
#endregion
}
}
//Copyright (c) 2005-2006 JianHan Fan
//Mail:henryfan@msn.com
//All rights reserved.
//-------------------------------------
using System;
using System.Data.OleDb;
namespace NClay.Data
{
/// <summary>
/// OledbAccess 的摘要说明。
/// </summary>
public class OledbAccess:IDriver
{
public OledbAccess()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region IDriverType 成员
public System.Data.IDbConnection Connection
{
get
{
// TODO: 添加 OledbAccess.Connection getter 实现
return new OleDbConnection();
}
}
public System.Data.IDbDataAdapter DataAdapter
{
get
{
// TODO: 添加 OledbAccess.DataAdapter getter 实现
return new OleDbDataAdapter();
}
}
public System.Data.IDbCommand Command
{
get
{
// TODO: 添加 OledbAccess.Command getter 实现
return new OleDbCommand();
}
}
public System.Data.IDataParameter GetParameter()
{
// TODO: 添加 OledbAccess.GetParameter 实现
return new OleDbParameter();
}
System.Data.IDataParameter Data.IDriver.GetParameter(string name, object value)
{
// TODO: 添加 OledbAccess.NClay.Data.IDriverType.GetParameter 实现
if (value == null)
value = DBNull.Value;
System.Data.IDataParameter dp = GetParameter();
dp.ParameterName = name;
if (value.GetType() == typeof(DateTime))
{
DateTime dt = (DateTime)value;
dp.Value = string.Format("{0}-{1}-{2} {3}:{4}:{5}", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);
}
else if (value.GetType() == typeof(byte[]))
{
if (value == DBNull.Value)
dp.Value = new byte[0];
else
dp.Value = value;
}
else
{
dp.Value = value;
}
return dp;
}
System.Data.IDataParameter Data.IDriver.GetParameter(string name, object value, System.Data.ParameterDirection direction)
{
// TODO: 添加 OledbAccess.NClay.Data.IDriverType.GetParameter 实现
System.Data.IDataParameter dp = GetParameter();
dp.ParameterName = name;
dp.Value = value;
dp.Direction = direction;
return dp;
}
public string NamedPrefix
{
get
{
// TODO: 添加 OledbAccess.NamedPrefix getter 实现
return "@";
}
}
public string FormatNameForParameter(string parametername)
{
// TODO: 添加 OledbAccess.FormatNameForParameter 实现
return NamedPrefix +parametername;
}
#endregion
#region IDriver 成员
public string FormatSql(string sql)
{
return sql.Replace("@", NamedPrefix);
}
#endregion
}
}
IDataSession
Code
/// <summary>
/// 数据处理环境描述接口
/// </summary>
public interface IDataSession : IDisposable
{
/// <summary>
/// 启用事务服务
/// </summary>
void BeginTran();
/// <summary>
/// 关键处理环境
/// </summary>
void Close();
/// <summary>
/// 提交环境服务
/// </summary>
void Commit();
/// <summary>
/// 删除实体对象
/// </summary>
/// <param name="entity">实体对象</param>
void Delete(object entity);
/// <summary>
/// 删除符合条件的实体对象
/// </summary>
/// <param name="entity">实体类型</param>
/// <param name="expression">条件对象</param>
void Delete(Type entity,IExpression expression);
/// <summary>
/// 删除符合条件的实体对象
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="expression">条件对象</param>
void Delete<T>(IExpression expression);
/// <summary>
/// 批执行命令对象
/// </summary>
/// <param name="cmd">命令对象集</param>
void ExcuteCmds(params IDbCommand[] cmd);
/// <summary>
/// 执行相关查询命令并返回DataSet数据集
/// </summary>
/// <param name="cmd">命令对象</param>
/// <returns>DataSet</returns>
DataSet ExecuteDataSet(IDbCommand cmd);
/// <summary>
/// 执行命令对象并返回受影响的行数
/// </summary>
/// <param name="cmd">命令对象</param>
/// <returns>int</returns>
int ExecuteNonQuery(IDbCommand cmd);
/// <summary>
/// 执行相关查询命令并返回DataReader对象
/// </summary>
/// <param name="cmd">命令对象</param>
/// <returns>IDataReader</returns>
IDataReader ExecuteReader(IDbCommand cmd);
/// <summary>
/// 执行相关查询命令,并返回第一行第一列值
/// </summary>
/// <param name="cmd">命令对象</param>
/// <returns>object</returns>
object ExecuteScalar(IDbCommand cmd);
/// <summary>
/// 获取符合条件的实体对象
/// </summary>
/// <param name="type">实体类型</param>
/// <param name="expression">条件对象</param>
/// <returns>实体对象集</returns>
IList List(Type type, IExpression expression);
/// <summary>
/// 获取符合条件的实体对象
/// </summary>
/// <param name="type">实体类型</param>
/// <param name="expression">条件对象</param>
/// <param name="region">获取区域</param>
/// <param name="orders">排序对象</param>
/// <returns>实体对象集</returns>
IList List(Type type, IExpression expression,Region region,params Mappings.FieldAdapter[] orders);
/// <summary>
/// 获取符合条件的实体对象
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="expression">条件对象</param>
/// <returns>实体对象集</returns>
IList<T> List<T>(IExpression expression);
/// <summary>
/// 获取符合条件的第一个实体对象
/// </summary>
/// <param name="type">实体类型</param>
/// <param name="expression">条件对象</param>
/// <returns>实体对象</returns>
object ListFirst(Type type, IExpression expression);
object ListFirst(Type type, IExpression expression,params Mappings.FieldAdapter[] orders);
/// <summary>
/// 获取符合条件的第一个实体对象
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="expression">条件对象</param>
/// <returns>实体对象</returns>
T ListFirst<T>(IExpression expression);
T ListFirst<T>(IExpression expression,params Mappings.FieldAdapter[] orders);
/// <summary>
/// 获取指定索引的实体对象
/// </summary>
/// <param name="type">实体类型</param>
/// <param name="id">索引值</param>
/// <returns>实体对象</returns>
object Load(Type type, object id);
/// <summary>
/// 获取指定索引的实体对象
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="id">索引值</param>
/// <returns>实体对象</returns>
T Load<T>(object id);
/// <summary>
/// 开启数据处理环境
/// </summary>
//void Open();
/// <summary>
/// 回滚当前事务服务
/// </summary>
void RollBack();
/// <summary>
/// 保存实体对象
/// </summary>
/// <param name="entity">实体对象</param>
void Save(object entity);
/// <summary>
/// 更新实体对象
/// </summary>
/// <param name="entity">实体对象</param>
void Update(object entity);
void Update<T>(Expressions.IExpression exp, params IFieldValue[] fields);
/// <summary>
/// 获取当前数据处理环境的数据连接对象
/// </summary>
IDbConnection Connection { get; }
/// <summary>
/// 获取或设置数据连接字符串
/// </summary>
string ConnectionString { get; set; }
/// <summary>
/// 获取或设置环境所在的信息映射容器
/// </summary>
MappingContainer Container { get; set; }
/// <summary>
/// 获取或设置环境相关的数据库提供对象
/// </summary>
IDriver Driver { get; set; }
/// <summary>
/// 获取当前环境的事务服务对象
/// </summary>
IDbTransaction Transaction { get; }
}
/// <summary>
/// 数据处理环境描述接口
/// </summary>
public interface IDataSession : IDisposable
{
/// <summary>
/// 启用事务服务
/// </summary>
void BeginTran();
/// <summary>
/// 关键处理环境
/// </summary>
void Close();
/// <summary>
/// 提交环境服务
/// </summary>
void Commit();
/// <summary>
/// 删除实体对象
/// </summary>
/// <param name="entity">实体对象</param>
void Delete(object entity);
/// <summary>
/// 删除符合条件的实体对象
/// </summary>
/// <param name="entity">实体类型</param>
/// <param name="expression">条件对象</param>
void Delete(Type entity,IExpression expression);
/// <summary>
/// 删除符合条件的实体对象
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="expression">条件对象</param>
void Delete<T>(IExpression expression);
/// <summary>
/// 批执行命令对象
/// </summary>
/// <param name="cmd">命令对象集</param>
void ExcuteCmds(params IDbCommand[] cmd);
/// <summary>
/// 执行相关查询命令并返回DataSet数据集
/// </summary>
/// <param name="cmd">命令对象</param>
/// <returns>DataSet</returns>
DataSet ExecuteDataSet(IDbCommand cmd);
/// <summary>
/// 执行命令对象并返回受影响的行数
/// </summary>
/// <param name="cmd">命令对象</param>
/// <returns>int</returns>
int ExecuteNonQuery(IDbCommand cmd);
/// <summary>
/// 执行相关查询命令并返回DataReader对象
/// </summary>
/// <param name="cmd">命令对象</param>
/// <returns>IDataReader</returns>
IDataReader ExecuteReader(IDbCommand cmd);
/// <summary>
/// 执行相关查询命令,并返回第一行第一列值
/// </summary>
/// <param name="cmd">命令对象</param>
/// <returns>object</returns>
object ExecuteScalar(IDbCommand cmd);
/// <summary>
/// 获取符合条件的实体对象
/// </summary>
/// <param name="type">实体类型</param>
/// <param name="expression">条件对象</param>
/// <returns>实体对象集</returns>
IList List(Type type, IExpression expression);
/// <summary>
/// 获取符合条件的实体对象
/// </summary>
/// <param name="type">实体类型</param>
/// <param name="expression">条件对象</param>
/// <param name="region">获取区域</param>
/// <param name="orders">排序对象</param>
/// <returns>实体对象集</returns>
IList List(Type type, IExpression expression,Region region,params Mappings.FieldAdapter[] orders);
/// <summary>
/// 获取符合条件的实体对象
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="expression">条件对象</param>
/// <returns>实体对象集</returns>
IList<T> List<T>(IExpression expression);
/// <summary>
/// 获取符合条件的第一个实体对象
/// </summary>
/// <param name="type">实体类型</param>
/// <param name="expression">条件对象</param>
/// <returns>实体对象</returns>
object ListFirst(Type type, IExpression expression);
object ListFirst(Type type, IExpression expression,params Mappings.FieldAdapter[] orders);
/// <summary>
/// 获取符合条件的第一个实体对象
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="expression">条件对象</param>
/// <returns>实体对象</returns>
T ListFirst<T>(IExpression expression);
T ListFirst<T>(IExpression expression,params Mappings.FieldAdapter[] orders);
/// <summary>
/// 获取指定索引的实体对象
/// </summary>
/// <param name="type">实体类型</param>
/// <param name="id">索引值</param>
/// <returns>实体对象</returns>
object Load(Type type, object id);
/// <summary>
/// 获取指定索引的实体对象
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="id">索引值</param>
/// <returns>实体对象</returns>
T Load<T>(object id);
/// <summary>
/// 开启数据处理环境
/// </summary>
//void Open();
/// <summary>
/// 回滚当前事务服务
/// </summary>
void RollBack();
/// <summary>
/// 保存实体对象
/// </summary>
/// <param name="entity">实体对象</param>
void Save(object entity);
/// <summary>
/// 更新实体对象
/// </summary>
/// <param name="entity">实体对象</param>
void Update(object entity);
void Update<T>(Expressions.IExpression exp, params IFieldValue[] fields);
/// <summary>
/// 获取当前数据处理环境的数据连接对象
/// </summary>
IDbConnection Connection { get; }
/// <summary>
/// 获取或设置数据连接字符串
/// </summary>
string ConnectionString { get; set; }
/// <summary>
/// 获取或设置环境所在的信息映射容器
/// </summary>
MappingContainer Container { get; set; }
/// <summary>
/// 获取或设置环境相关的数据库提供对象
/// </summary>
IDriver Driver { get; set; }
/// <summary>
/// 获取当前环境的事务服务对象
/// </summary>
IDbTransaction Transaction { get; }
}
DataSession实现
Code
class DataSession:IDataSession
{
public DataSession()
{
}
public DataSession(IDriver driver)
{
mDriver = driver;
}
#region IDataSession 成员
public void BeginTran()
{
OpenConnection();
mTransaction = Connection.BeginTransaction();
}
public void Close()
{
if(mTransaction!= null)
{
mTransaction.Rollback();
mTransaction = null;
}
mConnection.Close();
}
public void Commit()
{
mTransaction.Commit();
mTransaction = null;
}
public void Delete(object entity)
{
OnDelete(entity);
}
private void OnDelete(object entity)
{
Type type = entity.GetType();
Container.InspectMapper(type);
Mappings.ObjectMapper om = Container[type];
if (om.ID == null)
throw ThrowException.ObjectDelete(type);
eqExpression eq = new eqExpression(om.ID.Column,
om.ID.GetValue(entity));
Delete(entity.GetType(), eq);
}
public void Delete(Type entity, IExpression expression)
{
OnDelete(entity, expression);
}
private void OnDelete(Type entity, IExpression expression)
{
ObjectDelete delete = new ObjectDelete(entity, this);
delete.Expression = expression;
delete.Execute();
}
public void Delete<T>(IExpression expression)
{
OnDelete(expression);
}
private void OnDelete<T>(IExpression expression)
{
OnDelete(typeof(T), expression);
}
public void ExcuteCmds(params IDbCommand[] cmd)
{
foreach (IDbCommand item in cmd)
{
SetCommandState(item);
item.ExecuteNonQuery();
}
}
public DataSet ExecuteDataSet(IDbCommand cmd)
{
DataSet ds = new DataSet();
SetCommandState(cmd);
IDbDataAdapter da = Driver.DataAdapter;
da.SelectCommand = cmd;
da.Fill(ds);
return ds;
}
public int ExecuteNonQuery(IDbCommand cmd)
{
SetCommandState(cmd);
return cmd.ExecuteNonQuery();
}
public IDataReader ExecuteReader(IDbCommand cmd)
{
SetCommandState(cmd);
return cmd.ExecuteReader();
}
public object ExecuteScalar(IDbCommand cmd)
{
SetCommandState(cmd);
return cmd.ExecuteScalar();
}
void SetCommandState(IDbCommand cmd)
{
OpenConnection();
cmd.Connection = mConnection;
cmd.Transaction = mTransaction;
if (DaoContext.CommandExecute != null)
DaoContext.CommandExecute(this, cmd);
}
public IList List(Type type, IExpression expression)
{
return OnList(type, expression);
}
private IList OnList(Type type, IExpression expression)
{
ObjectLoader loader = new ObjectLoader(type, this);
loader.Expression = expression;
return (IList)loader.Execute();
}
public IList List(Type type, IExpression expression, Region region, params Mappings.FieldAdapter[] orders)
{
return OnList(type, expression, region, orders);
}
private IList OnList(Type type, IExpression expression, Region region, params Mappings.FieldAdapter[] orders)
{
ObjectLoader loader = new ObjectLoader(type, this);
loader.Expression = expression;
loader.Region = region;
loader.Orders = orders;
return (IList)loader.Execute();
}
public IList<T> List<T>(IExpression expression)
{
return (IList<T>)OnList(typeof(T), expression);
}
private object OnListFirst(Type type, IExpression expression,params FieldAdapter[] orders)
{
ObjectFirstLoader loader = new ObjectFirstLoader(type, this);
loader.Expression = expression;
loader.Orders = orders;
return loader.Execute();
}
public object ListFirst(Type type, IExpression expression)
{
return OnListFirst(type, expression,null);
}
public T ListFirst<T>(IExpression expression)
{
return (T)ListFirst(typeof(T), expression);
}
#region IDataSession 成员
public object ListFirst(Type type, IExpression expression, params FieldAdapter[] orders)
{
return OnListFirst(type, expression,orders);
}
public T ListFirst<T>(IExpression expression, params FieldAdapter[] orders)
{
return (T)ListFirst(typeof(T), expression,orders);
}
#endregion
public object Load(Type type, object id)
{
ObjectMapper om = Container[type];
eqExpression eq = new eqExpression(om.ID.Column,id);
ObjectFirstLoader loader = new ObjectFirstLoader(type, this);
loader.Expression = eq;
return loader.Execute();
}
public T Load<T>(object id)
{
return (T)Load(typeof(T), id);
}
//public void Open()
//{
// //mConnection = Driver.Connection;
// //mConnection.ConnectionString = ConnectionString;
// mConnection.Open();
//}
private void OpenConnection()
{
if (Connection.State == ConnectionState.Closed)
Connection.Open();
}
public void RollBack()
{
mTransaction.Rollback();
mTransaction = null;
}
public void Save(object entity)
{
ObjectInsert insert = new ObjectInsert(entity, this);
insert.Execute();
}
public void Update(object entity)
{
ObjectUpdate update = new ObjectUpdate(entity, this);
update.Execute();
}
public void Update<T>(Expressions.IExpression exp, params IFieldValue[] fields)
{
FieldUpdate fu = new FieldUpdate(typeof(T), this);
fu.Fields = fields;
fu.Expression = exp;
fu.Execute();
}
private IDbConnection mConnection = null;
public IDbConnection Connection
{
get {
if (mConnection == null)
{
mConnection = Driver.Connection;
mConnection.ConnectionString = ConnectionString;
}
return mConnection;
}
}
private string mConnectionString;
public string ConnectionString
{
get
{
return mConnectionString;
}
set
{
mConnectionString = value;
}
}
private MappingContainer mContainer;
public MappingContainer Container
{
get
{
return mContainer;
}
set
{
mContainer = value;
}
}
private IDriver mDriver;
public IDriver Driver
{
get
{
return mDriver;
}
set
{
mDriver = value;
}
}
private IDbTransaction mTransaction;
public IDbTransaction Transaction
{
get { return mTransaction; }
}
#endregion
#region IDisposable 成员
public void Dispose()
{
if (Transaction != null)
{
Transaction.Rollback();
mTransaction = null;
}
if (Connection != null)
{
if (Connection.State != ConnectionState.Closed)
Connection.Close();
Connection.Dispose();
mConnection = null;
}
}
#endregion
}
class DataSession:IDataSession
{
public DataSession()
{
}
public DataSession(IDriver driver)
{
mDriver = driver;
}
#region IDataSession 成员
public void BeginTran()
{
OpenConnection();
mTransaction = Connection.BeginTransaction();
}
public void Close()
{
if(mTransaction!= null)
{
mTransaction.Rollback();
mTransaction = null;
}
mConnection.Close();
}
public void Commit()
{
mTransaction.Commit();
mTransaction = null;
}
public void Delete(object entity)
{
OnDelete(entity);
}
private void OnDelete(object entity)
{
Type type = entity.GetType();
Container.InspectMapper(type);
Mappings.ObjectMapper om = Container[type];
if (om.ID == null)
throw ThrowException.ObjectDelete(type);
eqExpression eq = new eqExpression(om.ID.Column,
om.ID.GetValue(entity));
Delete(entity.GetType(), eq);
}
public void Delete(Type entity, IExpression expression)
{
OnDelete(entity, expression);
}
private void OnDelete(Type entity, IExpression expression)
{
ObjectDelete delete = new ObjectDelete(entity, this);
delete.Expression = expression;
delete.Execute();
}
public void Delete<T>(IExpression expression)
{
OnDelete(expression);
}
private void OnDelete<T>(IExpression expression)
{
OnDelete(typeof(T), expression);
}
public void ExcuteCmds(params IDbCommand[] cmd)
{
foreach (IDbCommand item in cmd)
{
SetCommandState(item);
item.ExecuteNonQuery();
}
}
public DataSet ExecuteDataSet(IDbCommand cmd)
{
DataSet ds = new DataSet();
SetCommandState(cmd);
IDbDataAdapter da = Driver.DataAdapter;
da.SelectCommand = cmd;
da.Fill(ds);
return ds;
}
public int ExecuteNonQuery(IDbCommand cmd)
{
SetCommandState(cmd);
return cmd.ExecuteNonQuery();
}
public IDataReader ExecuteReader(IDbCommand cmd)
{
SetCommandState(cmd);
return cmd.ExecuteReader();
}
public object ExecuteScalar(IDbCommand cmd)
{
SetCommandState(cmd);
return cmd.ExecuteScalar();
}
void SetCommandState(IDbCommand cmd)
{
OpenConnection();
cmd.Connection = mConnection;
cmd.Transaction = mTransaction;
if (DaoContext.CommandExecute != null)
DaoContext.CommandExecute(this, cmd);
}
public IList List(Type type, IExpression expression)
{
return OnList(type, expression);
}
private IList OnList(Type type, IExpression expression)
{
ObjectLoader loader = new ObjectLoader(type, this);
loader.Expression = expression;
return (IList)loader.Execute();
}
public IList List(Type type, IExpression expression, Region region, params Mappings.FieldAdapter[] orders)
{
return OnList(type, expression, region, orders);
}
private IList OnList(Type type, IExpression expression, Region region, params Mappings.FieldAdapter[] orders)
{
ObjectLoader loader = new ObjectLoader(type, this);
loader.Expression = expression;
loader.Region = region;
loader.Orders = orders;
return (IList)loader.Execute();
}
public IList<T> List<T>(IExpression expression)
{
return (IList<T>)OnList(typeof(T), expression);
}
private object OnListFirst(Type type, IExpression expression,params FieldAdapter[] orders)
{
ObjectFirstLoader loader = new ObjectFirstLoader(type, this);
loader.Expression = expression;
loader.Orders = orders;
return loader.Execute();
}
public object ListFirst(Type type, IExpression expression)
{
return OnListFirst(type, expression,null);
}
public T ListFirst<T>(IExpression expression)
{
return (T)ListFirst(typeof(T), expression);
}
#region IDataSession 成员
public object ListFirst(Type type, IExpression expression, params FieldAdapter[] orders)
{
return OnListFirst(type, expression,orders);
}
public T ListFirst<T>(IExpression expression, params FieldAdapter[] orders)
{
return (T)ListFirst(typeof(T), expression,orders);
}
#endregion
public object Load(Type type, object id)
{
ObjectMapper om = Container[type];
eqExpression eq = new eqExpression(om.ID.Column,id);
ObjectFirstLoader loader = new ObjectFirstLoader(type, this);
loader.Expression = eq;
return loader.Execute();
}
public T Load<T>(object id)
{
return (T)Load(typeof(T), id);
}
//public void Open()
//{
// //mConnection = Driver.Connection;
// //mConnection.ConnectionString = ConnectionString;
// mConnection.Open();
//}
private void OpenConnection()
{
if (Connection.State == ConnectionState.Closed)
Connection.Open();
}
public void RollBack()
{
mTransaction.Rollback();
mTransaction = null;
}
public void Save(object entity)
{
ObjectInsert insert = new ObjectInsert(entity, this);
insert.Execute();
}
public void Update(object entity)
{
ObjectUpdate update = new ObjectUpdate(entity, this);
update.Execute();
}
public void Update<T>(Expressions.IExpression exp, params IFieldValue[] fields)
{
FieldUpdate fu = new FieldUpdate(typeof(T), this);
fu.Fields = fields;
fu.Expression = exp;
fu.Execute();
}
private IDbConnection mConnection = null;
public IDbConnection Connection
{
get {
if (mConnection == null)
{
mConnection = Driver.Connection;
mConnection.ConnectionString = ConnectionString;
}
return mConnection;
}
}
private string mConnectionString;
public string ConnectionString
{
get
{
return mConnectionString;
}
set
{
mConnectionString = value;
}
}
private MappingContainer mContainer;
public MappingContainer Container
{
get
{
return mContainer;
}
set
{
mContainer = value;
}
}
private IDriver mDriver;
public IDriver Driver
{
get
{
return mDriver;
}
set
{
mDriver = value;
}
}
private IDbTransaction mTransaction;
public IDbTransaction Transaction
{
get { return mTransaction; }
}
#endregion
#region IDisposable 成员
public void Dispose()
{
if (Transaction != null)
{
Transaction.Rollback();
mTransaction = null;
}
if (Connection != null)
{
if (Connection.State != ConnectionState.Closed)
Connection.Close();
Connection.Dispose();
mConnection = null;
}
}
#endregion
}
补充一个ObjectLoader好理解
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
namespace NClay.Data
{
abstract class Loader: DataAccessAdapter
{
public Loader(Type type,IDataSession session)
{
session.Container.InspectMapper(type);
mSession = session;
mEntityType = type;
mOM = Session.Container[type];
mSession = session;
}
private Type mEntityType;
public Type EntityType
{
get
{
return mEntityType;
}
}
private Mappings.ObjectMapper mOM;
public Mappings.ObjectMapper OM
{
get
{
return mOM;
}
}
private Expressions.IExpression mExpression;
public Expressions.IExpression Expression
{
get
{
return mExpression;
}
set
{
mExpression = value;
}
}
protected override void OnCreateSqlStream(SqlStream stream)
{
OM.CreateSelect(Expression, stream);
if (!NClay.CoreFun.IsEmpty(Orders))
{
stream.SqlText.Append(" order by ");
for (int i = 0; i < Orders.Length; i++)
{
if (i > 0)
stream.SqlText.Append(",");
stream.SqlText.Append(Orders[0].Name);
}
}
}
public Region Region= Region.Default;
public Mappings.FieldAdapter[] Orders;
}
class ObjectLoader: Loader
{
public ObjectLoader(Type type, IDataSession session) : base(type, session) { }
protected override object OnExecute(IDbCommand command)
{
Type listtype = typeof(List<>);
object newobj = null;
IList list = (IList)Activator.CreateInstance(listtype.MakeGenericType(EntityType));
int index = 0;
using (IDataReader reader = Session.ExecuteReader(command))
{
while (reader.Read())
{
if (index >= Region.Start && index < Region.End)
{
newobj = OM.CreateHandler.Instance();
OM.Binder(reader, newobj);
list.Add(newobj);
}
index++;
if (index > Region.End)
break;
}
}
return list;
}
}
class ObjectFirstLoader : Loader
{
public ObjectFirstLoader(Type type, IDataSession session) : base(type, session) { }
protected override object OnExecute(IDbCommand command)
{
object newobj = null;
using (IDataReader reader = Session.ExecuteReader(command))
{
while (reader.Read())
{
newobj = OM.CreateHandler.Instance();
OM.Binder(reader, newobj);
break;
}
}
return newobj;
}
}
class ObjectLoaderHash : Loader
{
public ObjectLoaderHash(Type type, IDataSession session,Mappings.PropertyMapper property) : base(type, session) {
mProperty = property;
}
protected override object OnExecute(IDbCommand command)
{
Type dictype = typeof(Dictionary<,>).MakeGenericType(Property.Property.PropertyType, EntityType);
object newobj = null;
IDictionary dict = (IDictionary)Activator.CreateInstance(dictype);
int index = 0;
using (IDataReader reader = Session.ExecuteReader(command))
{
while (reader.Read())
{
if (index >= Region.Start && index < Region.End)
{
newobj = OM.CreateHandler.Instance;
OM.Binder(reader, newobj);
//dict.Add(OM.PropertyHandler[Property.Property].GetValue(newobj), newobj);
dict.Add(reader[OM.ID.ColumnIndex], newobj);
}
index++;
if (index > Region.End)
break;
}
}
return dict;
}
private Mappings.PropertyMapper mProperty;
public Mappings.PropertyMapper Property
{
get
{
return mProperty;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
namespace NClay.Data
{
abstract class Loader: DataAccessAdapter
{
public Loader(Type type,IDataSession session)
{
session.Container.InspectMapper(type);
mSession = session;
mEntityType = type;
mOM = Session.Container[type];
mSession = session;
}
private Type mEntityType;
public Type EntityType
{
get
{
return mEntityType;
}
}
private Mappings.ObjectMapper mOM;
public Mappings.ObjectMapper OM
{
get
{
return mOM;
}
}
private Expressions.IExpression mExpression;
public Expressions.IExpression Expression
{
get
{
return mExpression;
}
set
{
mExpression = value;
}
}
protected override void OnCreateSqlStream(SqlStream stream)
{
OM.CreateSelect(Expression, stream);
if (!NClay.CoreFun.IsEmpty(Orders))
{
stream.SqlText.Append(" order by ");
for (int i = 0; i < Orders.Length; i++)
{
if (i > 0)
stream.SqlText.Append(",");
stream.SqlText.Append(Orders[0].Name);
}
}
}
public Region Region= Region.Default;
public Mappings.FieldAdapter[] Orders;
}
class ObjectLoader: Loader
{
public ObjectLoader(Type type, IDataSession session) : base(type, session) { }
protected override object OnExecute(IDbCommand command)
{
Type listtype = typeof(List<>);
object newobj = null;
IList list = (IList)Activator.CreateInstance(listtype.MakeGenericType(EntityType));
int index = 0;
using (IDataReader reader = Session.ExecuteReader(command))
{
while (reader.Read())
{
if (index >= Region.Start && index < Region.End)
{
newobj = OM.CreateHandler.Instance();
OM.Binder(reader, newobj);
list.Add(newobj);
}
index++;
if (index > Region.End)
break;
}
}
return list;
}
}
class ObjectFirstLoader : Loader
{
public ObjectFirstLoader(Type type, IDataSession session) : base(type, session) { }
protected override object OnExecute(IDbCommand command)
{
object newobj = null;
using (IDataReader reader = Session.ExecuteReader(command))
{
while (reader.Read())
{
newobj = OM.CreateHandler.Instance();
OM.Binder(reader, newobj);
break;
}
}
return newobj;
}
}
class ObjectLoaderHash : Loader
{
public ObjectLoaderHash(Type type, IDataSession session,Mappings.PropertyMapper property) : base(type, session) {
mProperty = property;
}
protected override object OnExecute(IDbCommand command)
{
Type dictype = typeof(Dictionary<,>).MakeGenericType(Property.Property.PropertyType, EntityType);
object newobj = null;
IDictionary dict = (IDictionary)Activator.CreateInstance(dictype);
int index = 0;
using (IDataReader reader = Session.ExecuteReader(command))
{
while (reader.Read())
{
if (index >= Region.Start && index < Region.End)
{
newobj = OM.CreateHandler.Instance;
OM.Binder(reader, newobj);
//dict.Add(OM.PropertyHandler[Property.Property].GetValue(newobj), newobj);
dict.Add(reader[OM.ID.ColumnIndex], newobj);
}
index++;
if (index > Region.End)
break;
}
}
return dict;
}
private Mappings.PropertyMapper mProperty;
public Mappings.PropertyMapper Property
{
get
{
return mProperty;
}
}
}
}
如何详细文字描述不是我的强项,直接代码相信大家能看明白和知道实现原理.
这几年我一直执着地去搞这东西,后来还是喜欢抽象基础+XML模型+VSGenerator(扩展生成模型代码)这样的好处是封装的复杂度降低了,利于其他人员调试和维护.