该篇主要讲解工作单元的运用 和DbHlper抽象封装 

  工作单元仓储基类:BaseRepository (DbHlper抽象封装) 

  仓储接口(CRUD):IRepository (CRUD接口)

  工作单元仓储基类:IUnitOfWorkRepository (工作单元仓储接口)

  工作单元仓储基类:  UnitOfWorkRepositoryBase (实现注册工作单元接口) 

  工作单元仓储基类:  SqlRepositoryBase (创建数据读取工厂,加载子对象委托,创建实体对象方法) 

  仓储实现: Repository(改层主要是持久化数据) 

  BaseRepository (Code):

 

using System;
using System.Data;
using Notify.DbCommon.UnitOfWork;
using Notify.Infrastructure.UnitOfWork; namespace Notify.DbCommon.Repositroies
{
/// <summary>
/// 数据库持久化基类
/// </summary>
public abstract class BaseRepository : IDisposable
{
/// <summary>
/// 数据库链接
/// </summary>
private IDbConnection connection; /// <summary>
/// Command对象
/// </summary>
private IDbCommand cmd; /// <summary>
/// 工作单元接口
/// </summary>
public IUnitOfWork UnitOfWork { get; private set; } /// <summary>
/// 数据库链接
/// </summary>
public string Name { get; private set; } /// <summary>
/// Initializes a new instance of the <see cref="BaseRepository"/> class.
/// 构造函数
/// </summary>
/// <param name="unit">工作单元</param>
/// <param name="name">数据库链接</param>
protected BaseRepository(IUnitOfWork unit, string name)
{
this.Name = name;
this.UnitOfWork = unit;
if (unit != null)
{
this.cmd = unit.Command;
}
else
{
if (this.connection == null)
{
this.connection = DbFactories.GetConnection(name);
} if (this.connection.State != ConnectionState.Open)
{
this.connection.Open();
} this.cmd = this.connection.CreateCommand();
}
} #region Parameter
/// <summary>
/// 添加参数
/// </summary>
/// <param name="name">参数名称</param>
/// <returns>参数</returns>
protected IDbDataParameter AddParameter(string name)
{
IDbDataParameter param = this.CreateParameter(name);
this.cmd.Parameters.Add(param);
return param;
} /// <summary>
/// 添加参数
/// </summary>
/// <param name="name">参数名称</param>
/// <param name="value">参数值</param>
/// <returns>参数</returns>
protected IDbDataParameter AddParameter(string name, object value)
{
IDbDataParameter param = this.CreateParameter(name, value);
this.cmd.Parameters.Add(param);
return param;
} /// <summary>
/// 添加参数
/// </summary>
/// <param name="name">参数名称</param>
/// <param name="value">参数值</param>
/// <param name="type">参数类型</param>
/// <returns>参数</returns>
protected IDbDataParameter AddParameter(string name, object value, DbType type)
{
IDbDataParameter param = this.CreateParameter(name, value, type);
this.cmd.Parameters.Add(param);
return param;
} /// <summary>
/// 添加参数
/// </summary>
/// <param name="name">参数名称</param>
/// <param name="value">参数值</param>
/// <param name="type">参数类型</param>
/// <param name="direction">参数输出类型</param>
/// <returns>参数</returns>
protected IDbDataParameter AddParameter(string name, object value, DbType type, ParameterDirection direction)
{
IDbDataParameter param = this.CreateParameter(name, value, type, direction);
this.cmd.Parameters.Add(param);
return param;
} /// <summary>
/// 添加参数
/// </summary>
/// <param name="name">参数名称</param>
/// <param name="value">参数值</param>
/// <param name="type">参数类型</param>
/// <param name="direction">参数输出类型</param>
/// <param name="size">参数大小</param>
/// <returns>参数</returns>
protected IDbDataParameter AddParameter(string name, object value, DbType type, ParameterDirection direction, int size)
{
IDbDataParameter param = this.CreateParameter(name, value, type, direction, size);
this.cmd.Parameters.Add(param);
return param;
} /// <summary>
/// 添加参数
/// </summary>
/// <param name="name">参数名称</param>
/// <param name="value">参数值</param>
/// <param name="type">参数类型</param>
/// <param name="direction">参数输出类型</param>
/// <param name="size">参数大小</param>
/// <param name="scale">显示数字参数的规模(精确到小数点后几位)</param>
/// <returns>参数</returns>
protected IDbDataParameter AddParameter(string name, object value, DbType type, ParameterDirection direction, int size, byte scale)
{
IDbDataParameter param = this.CreateParameter(name, value, type, direction, size, scale);
this.cmd.Parameters.Add(param);
return param;
} /// <summary>
/// 清除参数
/// </summary>
protected void ClearParameters()
{
this.cmd.Parameters.Clear();
}
#endregion #region ExecuteReader /// <summary>
/// 读取数据
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="type">读取类型(sql文本,存储过程,表)</param>
/// <param name="behavior">提供了一个查询的结果的描述和对数据库的影响</param>
/// <param name="timeout">超时时间</param>
/// <returns>IDataReader</returns>
protected IDataReader ExecuteReader(string sql, CommandType type, CommandBehavior behavior, int timeout)
{
if (string.IsNullOrWhiteSpace(sql))
{
throw new ArgumentNullException("sql");
} this.cmd.CommandText = sql;
this.cmd.CommandType = type;
this.cmd.CommandTimeout = timeout;
return this.cmd.ExecuteReader(behavior);
} /// <summary>
/// 读取数据
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="type">读取类型(sql文本,存储过程,表)</param>
/// <param name="behavior">提供了一个查询的结果的描述和对数据库的影响</param>
/// <returns>IDataReader</returns>
protected IDataReader ExecuteReader(string sql, CommandType type, CommandBehavior behavior)
{
return this.ExecuteReader(sql, type, behavior, );
} /// <summary>
/// 读取数据
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="type">读取类型(sql文本,存储过程,表)</param>
/// <param name="timeout">超时时间</param>
/// <returns>IDataReader</returns>
protected IDataReader ExecuteReader(string sql, CommandType type, int timeout)
{
return this.ExecuteReader(sql, type, CommandBehavior.Default, timeout);
} /// <summary>
/// 读取数据
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="type">读取类型(sql文本,存储过程,表)</param>
/// <returns>IDataReader</returns>
protected IDataReader ExecuteReader(string sql, CommandType type)
{
return this.ExecuteReader(sql, type, CommandBehavior.Default, );
} /// <summary>
/// 读取数据
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="behavior">提供了一个查询的结果的描述和对数据库的影响</param>
/// <param name="timeout">超时时间</param>
/// <returns>IDataReader</returns>
protected IDataReader ExecuteReader(string sql, CommandBehavior behavior, int timeout)
{
return this.ExecuteReader(sql, CommandType.Text, behavior, timeout);
} /// <summary>
/// 读取数据
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="behavior">提供了一个查询的结果的描述和对数据库的影响</param>
/// <returns>IDataReader</returns>
protected IDataReader ExecuteReader(string sql, CommandBehavior behavior)
{
return this.ExecuteReader(sql, CommandType.Text, behavior, );
} /// <summary>
/// 读取数据
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="timeout">超时时间</param>
/// <returns>IDataReader</returns>
protected IDataReader ExecuteReader(string sql, int timeout)
{
return this.ExecuteReader(sql, CommandType.Text, CommandBehavior.Default, timeout);
} /// <summary>
/// 读取数据
/// </summary>
/// <param name="sql">sql语句</param>
/// <returns>IDataReader</returns>
protected IDataReader ExecuteReader(string sql)
{
return this.ExecuteReader(sql, CommandType.Text, CommandBehavior.Default, );
} #endregion #region ExecuteTable /// <summary>
/// 读取数据
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="type">读取类型(sql文本,存储过程,表)</param>
/// <param name="behavior">提供了一个查询的结果的描述和对数据库的影响</param>
/// <param name="timeout">超时时间</param>
/// <returns>DataTable</returns>
protected DataTable ExecuteTable(string sql, CommandType type, CommandBehavior behavior, int timeout)
{
using (IDataReader dr = this.ExecuteReader(sql, type, behavior, timeout))
{
DataTable dt = new DataTable();
dt.Load(dr);
return dt;
}
} /// <summary>
/// 读取数据
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="type">读取类型(sql文本,存储过程,表)</param>
/// <param name="behavior">提供了一个查询的结果的描述和对数据库的影响</param>
/// <returns>DataTable</returns>
protected DataTable ExecuteTable(string sql, CommandType type, CommandBehavior behavior)
{
return this.ExecuteTable(sql, type, behavior, );
} /// <summary>
/// 读取数据
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="type">读取类型(sql文本,存储过程,表)</param>
/// <param name="timeout">超时时间</param>
/// <returns>DataTable</returns>
protected DataTable ExecuteTable(string sql, CommandType type, int timeout)
{
return this.ExecuteTable(sql, type, CommandBehavior.Default, timeout);
} /// <summary>
/// 读取数据
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="type">读取类型(sql文本,存储过程,表)</param>
/// <returns>DataTable</returns>
protected DataTable ExecuteTable(string sql, CommandType type)
{
return this.ExecuteTable(sql, type, CommandBehavior.Default, );
} /// <summary>
/// 读取数据
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="behavior">提供了一个查询的结果的描述和对数据库的影响</param>
/// <param name="timeout">超时时间</param>
/// <returns>DataTable</returns>
protected DataTable ExecuteTable(string sql, CommandBehavior behavior, int timeout)
{
return this.ExecuteTable(sql, CommandType.Text, behavior, timeout);
} /// <summary>
/// 读取数据
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="behavior">提供了一个查询的结果的描述和对数据库的影响</param>
/// <returns>DataTable</returns>
protected DataTable ExecuteTable(string sql, CommandBehavior behavior)
{
return this.ExecuteTable(sql, CommandType.Text, behavior, );
} /// <summary>
/// 读取数据
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="timeout">超时时间</param>
/// <returns>DataTable</returns>
protected DataTable ExecuteTable(string sql, int timeout)
{
return this.ExecuteTable(sql, CommandType.Text, CommandBehavior.Default, timeout);
} /// <summary>
/// 读取数据
/// </summary>
/// <param name="sql">sql语句</param>
/// <returns>DataTable</returns>
protected DataTable ExecuteTable(string sql)
{
return this.ExecuteTable(sql, CommandType.Text, CommandBehavior.Default, );
} #endregion #region ExecuteDataSet /// <summary>
/// 读取数据
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="tableName">表名</param>
/// <returns>DataTable</returns>
protected DataSet ExecuteDataSet(string sql, params string[] tableName)
{
return this.ExecuteDataSet(sql, CommandType.Text, tableName);
} /// <summary>
/// 读取数据
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="type">读取类型</param>
/// <param name="tableName">表名</param>
/// <returns>DataTable</returns>
protected DataSet ExecuteDataSet(string sql, CommandType type, params string[] tableName)
{
using (IDataReader dr = this.ExecuteReader(sql, type, CommandBehavior.Default, ))
{
DataSet ds = new DataSet();
ds.Load(dr, LoadOption.Upsert, tableName);
return ds;
}
} #endregion #region ExecuteScalar
/// <summary>
/// ExecuteScalar
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="type">执行类型</param>
/// <param name="timeout">超时时间</param>
/// <returns>结果</returns>
protected object ExecuteScalar(string sql, CommandType type, int timeout)
{
if (string.IsNullOrWhiteSpace(sql))
{
throw new ArgumentNullException("sql");
} this.cmd.CommandText = sql;
this.cmd.CommandType = type;
this.cmd.CommandTimeout = timeout;
object result = this.cmd.ExecuteScalar();
return result == DBNull.Value ? null : result;
} /// <summary>
/// ExecuteScalar
/// </summary>
/// <param name="sql">sql语句</param>
/// <returns>结果</returns>
protected object ExecuteScalar(string sql)
{
return this.ExecuteScalar(sql, CommandType.Text, );
}
#endregion #region ExecuteNonQuery /// <summary>
/// ExecuteNonQuery
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="type">执行类型</param>
/// <param name="timeout">超时时间</param>
/// <returns>结果</returns>
protected int ExecuteNonQuery(string sql, CommandType type, int timeout)
{
if (string.IsNullOrWhiteSpace(sql))
{
throw new ArgumentNullException("sql");
} this.cmd.CommandText = sql;
this.cmd.CommandType = type;
this.cmd.CommandTimeout = timeout;
return this.cmd.ExecuteNonQuery();
} /// <summary>
/// ExecuteNonQuery
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="type">执行类型</param>
/// <returns>结果</returns>
protected int ExecuteNonQuery(string sql, CommandType type)
{
return this.ExecuteNonQuery(sql, type, );
} /// <summary>
/// ExecuteNonQuery
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="timeout">超时时间</param>
/// <returns>结果</returns>
protected int ExecuteNonQuery(string sql, int timeout)
{
return this.ExecuteNonQuery(sql, CommandType.Text, timeout);
} /// <summary>
/// ExecuteNonQuery
/// </summary>
/// <param name="sql">sql语句</param>
/// <returns>结果</returns>
protected int ExecuteNonQuery(string sql)
{
return this.ExecuteNonQuery(sql, CommandType.Text, );
} #endregion #region CreateParameter
/// <summary>
/// 创建参数
/// </summary>
/// <param name="name">参数名称</param>
/// <returns>参数</returns>
private IDbDataParameter CreateParameter(string name)
{
IDbDataParameter param = this.cmd.CreateParameter();
param.ParameterName = name;
return param;
} /// <summary>
/// 创建参数
/// </summary>
/// <param name="name">参数名称</param>
/// <param name="value">参数值</param>
/// <returns>参数</returns>
private IDbDataParameter CreateParameter(string name, object value)
{
IDbDataParameter param = this.CreateParameter(name);
param.Value = value ?? DBNull.Value;
return param;
} /// <summary>
/// 创建参数
/// </summary>
/// <param name="name">参数名称</param>
/// <param name="value">参数值</param>
/// <param name="type">参数类型</param>
/// <returns>参数</returns>
private IDbDataParameter CreateParameter(string name, object value, DbType type)
{
IDbDataParameter param = this.CreateParameter(name, value);
param.DbType = type;
return param;
} /// <summary>
/// 创建参数
/// </summary>
/// <param name="name">参数名称</param>
/// <param name="value">参数值</param>
/// <param name="type">参数类型</param>
/// <param name="direction">参数输出类型</param>
/// <returns>参数</returns>
private IDbDataParameter CreateParameter(string name, object value, DbType type, ParameterDirection direction)
{
IDbDataParameter param = this.CreateParameter(name, value, type);
param.Direction = direction;
return param;
} /// <summary>
/// 创建参数
/// </summary>
/// <param name="name">参数名称</param>
/// <param name="value">参数值</param>
/// <param name="type">参数类型</param>
/// <param name="direction">参数输出类型</param>
/// <param name="size">参数大小</param>
/// <returns>参数</returns>
private IDbDataParameter CreateParameter(string name, object value, DbType type, ParameterDirection direction, int size)
{
IDbDataParameter param = this.CreateParameter(name, value, type, direction);
param.Size = size;
return param;
} /// <summary>
/// 创建参数
/// </summary>
/// <param name="name">参数名称</param>
/// <param name="value">参数值</param>
/// <param name="type">参数类型</param>
/// <param name="direction">参数输出类型</param>
/// <param name="size">参数大小</param>
/// <param name="scale">显示数字参数的规模(精确到小数点后几位)</param>
/// <returns>参数</returns>
private IDbDataParameter CreateParameter(string name, object value, DbType type, ParameterDirection direction, int size, byte scale)
{
IDbDataParameter param = this.CreateParameter(name, value, type, direction, size);
param.Scale = scale;
return param;
}
#endregion /// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
if (this.cmd != null)
{
this.cmd.Dispose();
this.cmd = null;
} if (this.connection == null)
{
return;
} if (this.connection.State == ConnectionState.Open)
{
this.connection.Close();
} this.connection.Dispose();
this.connection = null;
}
}
}

  IRepository (Code)

using Notify.Infrastructure.DomainBase;

namespace Notify.Infrastructure.RepositoryFramework
{
/// <summary>
/// 仓储接口(CRUD)
/// </summary>
/// <typeparam name="TValue">实体</typeparam>
public interface IRepository<TValue> : IAddRepository<TValue>, IRemoveRepository<TValue>, IUpdateRepository<TValue>, IQueryRepository<TValue> where TValue : IEntity
{
} /// <summary>
/// 仓储接口(CRUD)
/// </summary>
/// <typeparam name="TKey">主键</typeparam>
/// <typeparam name="TValue">实体</typeparam>
public interface IRepository<in TKey, TValue> : IAddRepository<TValue>, IRemoveRepository<TValue>, IUpdateRepository<TValue>, IQueryRepository<TKey, TValue> where TValue : IEntity
{
}
}

  IUnitOfWorkRepository (Code)

using Notify.Infrastructure.DomainBase;

namespace Notify.Infrastructure.RepositoryFramework
{
/// <summary>
/// 工作单元仓储接口
/// </summary>
public interface IUnitOfWorkRepository
{
/// <summary>
/// 持久化新增实体
/// </summary>
/// <param name="item">待新增实体接口</param>
void PersistNewItem(IEntity item); /// <summary>
/// 持久化更新实体
/// </summary>
/// <param name="item">待更新实体接口</param>
void PersistUpdatedItem(IEntity item); /// <summary>
/// 持久化删除实体
/// </summary>
/// <param name="item">待删除实体接口</param>
void PersistDeletedItem(IEntity item);
}
}

  UnitOfWorkRepositoryBase (Code)

 

using Notify.Infrastructure.DomainBase;
using Notify.Infrastructure.RepositoryFramework;
using Notify.Infrastructure.UnitOfWork; namespace Notify.DbCommon.Repositroies
{
/// <summary>
/// 工作单元仓储基类
/// </summary>
/// <typeparam name="TKey">主键</typeparam>
/// <typeparam name="TValue">实体</typeparam>
public abstract class UnitOfWorkRepositoryBase<TKey, TValue> : BaseRepository, IRepository<TKey, TValue>, IUnitOfWorkRepository
where TValue : IEntity
{
/// <summary>
/// 工作单元接口
/// </summary>
public new IPowerUnitOfWork UnitOfWork { get; private set; } /// <summary>
/// Initializes a new instance of the <see cref="UnitOfWorkRepositoryBase{TKey,TValue}"/> class.
/// 构造函数
/// </summary>
/// <param name="unitOfWork">工作单元</param>
/// <param name="name">数据连接</param>
protected UnitOfWorkRepositoryBase(IPowerUnitOfWork unitOfWork, string name)
: base(unitOfWork, name)
{
this.UnitOfWork = unitOfWork;
} /// <summary>
/// 根据主键查询实体
/// </summary>
/// <param name="key">主键</param>
/// <returns>实体</returns>
public abstract TValue Query(TKey key); /// <summary>
/// 根据主键查询实体(索引)
/// </summary>
/// <param name="key">主键</param>
/// <returns>实体</returns>
public TValue this[TKey key]
{
get
{
return this.Query(key);
} set
{
if (this.Query(key) == null)
{
this.Add(value);
}
else
{
this.Update(value);
}
}
} /// <summary>
/// 添加实体(注册到工作单元)
/// </summary>
/// <param name="item">实体</param>
public void Add(TValue item)
{
if (this.UnitOfWork != null)
{
this.UnitOfWork.RegisterAdded(item, this);
}
else
{
this.PersistNewItem(item);
}
} /// <summary>
/// 删除实体(注册到工作单元)
/// </summary>
/// <param name="item">实体</param>
public void Remove(TValue item)
{
if (this.UnitOfWork != null)
{
this.UnitOfWork.RegisterRemoved(item, this);
}
else
{
this.PersistDeletedItem(item);
}
} /// <summary>
/// 修改实体(注册到工作单元)
/// </summary>
/// <param name="item">实体</param>
public void Update(TValue item)
{
if (this.UnitOfWork != null)
{
this.UnitOfWork.RegisterChanged(item, this);
}
else
{
this.PersistUpdatedItem(item);
}
} /// <summary>
/// 添加实体(持久化)
/// </summary>
/// <param name="item">实体</param>
public abstract void PersistNewItem(IEntity item); /// <summary>
/// 修改实体(持久化)
/// </summary>
/// <param name="item">实体</param>
public abstract void PersistUpdatedItem(IEntity item); /// <summary>
/// 删除实体(持久化)
/// </summary>
/// <param name="item">实体</param>
public abstract void PersistDeletedItem(IEntity item);
}
}

  SqlRepositoryBase (Code)

using System.Collections.Generic;
using System.Data;
using System.Linq;
using Notify.Infrastructure.DomainBase;
using Notify.Infrastructure.EntityFactoryFramework;
using Notify.Infrastructure.UnitOfWork; namespace Notify.DbCommon.Repositroies
{
/// <summary>
/// 工作单元仓储基类
/// </summary>
/// <typeparam name="TKey">主键</typeparam>
/// <typeparam name="TValue">实体</typeparam>
public abstract class SqlRepositoryBase<TKey, TValue> : UnitOfWorkRepositoryBase<TKey, TValue>
where TValue : IEntity
{
/// <summary>
/// 有子对象的回调委托
/// </summary>
/// <param name="entityAggregate">实体聚合根</param>
/// <param name="childEntityKeyValue">子实体键</param>
public delegate void AppendChildData(TValue entityAggregate, object childEntityKeyValue); /// <summary>
/// 实体工厂
/// </summary>
protected readonly IEntityFactory<TValue> m_entityFactory; /// <summary>
/// 子对象集
/// </summary>
private readonly Dictionary<string, AppendChildData> m_childCallbacks; /// <summary>
/// 子对象数据集
/// </summary>
private readonly Dictionary<string, object> m_childKeyDatas; /// <summary>
/// 构造函数
/// </summary>
/// <param name="unitOfWork">工作单元</param>
/// <param name="name">数据连接</param>
protected SqlRepositoryBase(IPowerUnitOfWork unitOfWork, string name)
: base(unitOfWork, name)
{
this.m_entityFactory = this.BuildEntityFactory();
this.m_childCallbacks = new Dictionary<string, AppendChildData>();
this.m_childKeyDatas = new Dictionary<string, object>();
this.BuildChildCallbacks(this.m_childCallbacks);
} /// <summary>
/// 改为由子类创建实体,不使用工厂
/// </summary>
/// <returns>TValue</returns>
protected abstract IEntityFactory<TValue> BuildEntityFactory(); /// <summary>
/// 创建子对象回调
/// </summary>
/// <param name="childCallbacks">子对象集</param>
protected abstract void BuildChildCallbacks(Dictionary<string, AppendChildData> childCallbacks); /// <summary>
/// 子对象回调集
/// </summary>
protected Dictionary<string, AppendChildData> ChildCallbacks
{
get
{
return this.m_childCallbacks;
}
} /// <summary>
/// 创建实体对象
/// </summary>
/// <param name="reader">IDataReader</param>
/// <returns>实体对象</returns>
protected virtual TValue BuildEntityFromReader(IDataReader reader)
{
TValue entity = this.m_entityFactory.BuildEntity(reader);
if (this.m_childCallbacks != null && this.m_childCallbacks.Count > )
{
DataTable columnData = reader.GetSchemaTable();
foreach (string childKeyName in this.m_childCallbacks.Keys)
{
object childKeyValue;
////判断 DataReader 的数据集合中是否存在一个特定的列名(或字段名)
if (columnData != null && columnData.Rows.Cast<DataRow>().Any(row => row["ColumnName"].ToString() == childKeyName))
{
childKeyValue = reader[childKeyName];
}
else
{
childKeyValue = null;
}
if (m_childKeyDatas.ContainsKey(childKeyName))
{
m_childKeyDatas[childKeyName] = childKeyValue;
}
else
{
m_childKeyDatas.Add(childKeyName, childKeyValue);
}
}
}
return entity;
} /// <summary>
/// 创建实体对象
/// </summary>
/// <param name="sql">sql语句</param>
/// <returns>实体对象</returns>
protected virtual TValue BuildEntityFromSql(string sql)
{
TValue entity = default(TValue);
using (IDataReader reader = this.ExecuteReader(sql))
{
if (reader.Read())
{
entity = this.BuildEntityFromReader(reader);
}
}
if (entity != null)
{
this.InvokeChildCallbacks(entity);
}
return entity;
} /// <summary>
/// 创建实体对象集合
/// </summary>
/// <param name="sql">sql语句</param>
/// <returns>实体对象集合</returns>
protected virtual List<TValue> BuildEntitiesFromSql(string sql)
{
List<TValue> entities = new List<TValue>();
using (IDataReader reader = this.ExecuteReader(sql))
{
while (reader.Read())
{
entities.Add(this.BuildEntityFromReader(reader));
}
}
return entities;
} /// <summary>
/// 加载子对象
/// </summary>
/// <param name="entity">实体对象</param>
private void InvokeChildCallbacks(TValue entity)
{
if (this.m_childCallbacks != null && this.m_childCallbacks.Any())
{
foreach (string childKeyName in this.m_childKeyDatas.Keys)
{
object childKeyValue;
this.m_childKeyDatas.TryGetValue(childKeyName, out childKeyValue);
this.m_childCallbacks[childKeyName](entity, childKeyValue);
}
}
}
}
}

  Repository(Code):

  该层代码将再后面文章讲解

 

   上篇--工作单元

  下篇--Common-Code额外篇

EsayUI + MVC + ADO.NET(仓储基类)的更多相关文章

  1. EsayUI + MVC + ADO.NET(仓储基础接口)

      1.RepositoryFramework(仓储接口:无外乎就是CRUD) 1.IAddRepository(添加接口) using System; namespace Notify.Infras ...

  2. EsayUI + MVC + ADO.NET(项目架构)

    项目目录   1.项目架构: http://www.cnblogs.com/liuxiaoji/p/3922506.html  2.仓储基础接口: http://www.cnblogs.com/liu ...

  3. ASP.NET MVC项目实现BasePage基类用作ASPX.CS网页继承

    在ASP.NET MVC项目开发,还是需要创建一些Web Page来实现一些功能,如呈现报表等... 但是一旦项目的.ASPX网页太多了,其中的程序代码也会有代码冗余,出现这些情况,我们得需要对这些代 ...

  4. EsayUI + MVC + ADO.NET(工作单元)

    关联的设计 关联本身不是一个模式,但它在领域建模的过程中非常重要,所以需要在探讨各种模式之前,先讨论一下对象之间的关联该如何设计.我觉得对象的关联的设计可以遵循如下的一些原则: 关联尽量少,对象之间的 ...

  5. 本版本延续MVC中的统一验证机制~续的这篇文章,本篇主要是对验证基类的扩展和改善(转)

    本版本延续MVC中的统一验证机制~续的这篇文章,本篇主要是对验证基类的扩展和改善 namespace Web.Mvc.Extensions { #region 验证基类 /// <summary ...

  6. 修改tt模板让ADO.NET C# POCO Entity Generator With WCF Support 生成的实体类继承自定义基类

    折腾几天记载一下,由于项目实际需要,从edmx生成的实体类能自动继承自定义的基类,这个基类不是从edmx文件中添加的Entityobject. 利用ADO.NET C# POCO Entity Gen ...

  7. MVC Controller 基类中的Request

    今天在测试自己MVC程序的时候发现之前写代码的一个BUG,需求是每个页面要获取当前URL链接中包含的城市ID,我把获取url的方法写到了Controller的基类BaseController(Base ...

  8. .net mvc 基类属性覆盖问题

    一,问题是这样的 我使用.net mvc设计架构时, 为了方便大家的获取UserInfo信息, 把UserInfo对象,放在了自定义的基类BaseController中, 二,问题出现了 我发觉多个人 ...

  9. iOS 基于MVC设计模式的基类设计

    iOS 基于MVC设计模式的基类设计 https://www.jianshu.com/p/3b580ffdae00

随机推荐

  1. python3中替换python2中cmp函数

    python 3.4.3 的版本中已经没有cmp函数,被operator模块代替,在交互模式下使用时,需要导入模块. 在没有导入模块情况下,会出现 提示找不到cmp函数了,那么在python3中该如何 ...

  2. iOS 常用小功能 总结

    常用小功能 iOS中的很多小功能都是非常简单的,几行代码就搞定了,比如打电话.打开网址.发邮件.发短信等 打电话 方法一(不被采用): 拨号之前会弹框询问用户是否拨号,拨完后能自动回到原应用 NSUR ...

  3. Twitter OA prepare: Two Operations

    准备T家OA,网上看的面经 最直接的方法,从target降到1,如果是奇数就减一,偶数就除2 public static void main(String[] args) { int a = shor ...

  4. 更换mysql数据库的datadir目录

    更换过程如下: 1.新建指定的datadir目录,这里举例如:E:/mysql_datadir/data. 2.关闭mysql57服务器. 3.将原来的datadir目录下面的所有文件拷贝到E:/my ...

  5. wordpress 修改域名后的403

    wordpress的好处就是方便,不好呢,额,反正就是有. 最近,修改域名,也遇到了这个问题[修改域名后,出现403]. 网上的办法似乎有很多,但有一些并不好,比如修改数据库什么的,如果是新手,数据库 ...

  6. Hdu dp

    4856 这题说的是给了一个图 这个图有很多的隧道每个隧道是单向的 只能从一个入口进入从另一个入口出来 要求计算出走完这些隧道花的总时间 因为这个图是一个网格行的然后 先用bfs算出隧道的出口到每个隧 ...

  7. 开启你的kali linux DIY之旅

    更新源 首先 是kali2016.2更新源的问题,网上找了好久,都不是很满意.后来把kali 2016.2安装到实体机中才发现,里面自带了更新源.下面我把这个官方自带更新源分享出来(速度蛮快的) de ...

  8. echart知识点、常用图形

    原文地址:https://www.cnblogs.com/kewenxin/p/9338272.html 本文是自己在项目中需要运用到的echarts图形进行整理,都有完整的代码.echarts原型, ...

  9. Python Web学习笔记之进程与线程

    要了解二者的区别与联系,首先得对进程与线程有一个宏观上的了解. 进程,是并发执行的程序在执行过程中分配和管理资源的基本单位,是一个动态概念,竟争计算机系统资源的基本单位.每一个进程都有一个自己的地址空 ...

  10. 20145105 《Java程序设计》第9周学习总结

    20145105 <Java程序设计>第9周学习总结 教材学习内容总结 第十六章 整合数据库 一.JDBC入门 (一)JDBC简介 厂商在操作JDBC驱动程序时,依操作方式可将驱动程序分为 ...