名字起得有点夸张了,其实就是实现 基于 Data Access Application Block的DAL基类和约束 首先Repository部分没什么好描述的,如果有不了解的可以直接百度或者谷歌相关内容,直接上具体代码 注意此部分没有写批量查询的方法(比如FindAll,这跟后面的基类设定
名字起得有点夸张了,其实就是实现基于Data Access Application Block的DAL基类和约束
首先Repository部分没什么好描述的,如果有不了解的可以直接百度或者谷歌相关内容,直接上具体代码
注意此部分没有写批量查询的方法(比如FindAll,这跟后面的基类设定有关)
/// <summary> /// DataAccess Repository /// </summary> /// /// public interface IRepository { /// <summary> /// 根据主键获取对应的实体对象 /// </summary> /// /// T1 GetEntityByKey(T2 key); /// <summary> /// 单个新增 /// </summary> /// /// bool Insert(T1 entity); /// <summary> /// 单个编辑 /// </summary> /// /// bool Update(T1 entity); /// <summary> /// 单个删除 /// </summary> /// /// bool Delete(T2 key); }
然后是DAL抽象基类,该基类设计上实现读写分离,而且每个子类只应有一个数据库连接,这样做的隐藏目的是每个DAL类都只应该执行自己最基本的功能:只关心数据库交互,不关心业务逻辑
/// <summary> /// DAL基类,<strong>基于</strong>EntLib,读写分离 /// </summary> public abstract class DataAccessBase { private Database _readDB; private Database _writeDB; /// <summary> /// 要使用的读数据库配置节,如果配置为null或者空则会调用默认配置节 /// </summary> protected abstract string ReadDBName { get; } /// <summary> /// 要使用的写数据库配置节,如果配置为null或者空则会调用默认配置节 /// </summary> protected abstract string WriteDBName { get; } /// <summary> /// 读库 /// </summary> protected Database ReadDB { get { if (this._readDB == null) { this._readDB = this.GetDatabase(this._writeDB, this.ReadDBName); } return this._readDB; } } private Database GetDatabase(Database db, string dbName) { if (this.ReadDBName == this.WriteDBName && db != null) { return db; } else { return this.CreateDatabase(dbName); } } /// <summary> /// 写库 /// </summary> protected Database WriteDB { get { if (this._writeDB == null) { this._writeDB = this.GetDatabase(this._readDB, this.WriteDBName); } return this._writeDB; } } private Database CreateDatabase(string dbName) { DatabaseProviderFactory factory = new DatabaseProviderFactory(); if (string.IsNullOrWhiteSpace(dbName)) { return factory.CreateDefault(); } else { return factory.Create(dbName); } } }
最后就是IRepository接口与DataAccessBase的组合实现SingleDataAccessBase,为什么不在DataAccessBase时就实现IRepository呢?因为设计上DataAccessBase是可以同时运用于多表操作及单表操作的,多表操作时,IRepository不存在任何意义,只有单表操作时,IRepository才有意义,而SingleDataAccessBase就是单表DAL基类
using System.Collections.Generic; using System.Linq; using Microsoft.Practices.EnterpriseLibrary.Data; using System.Data.Common; /// <summary> /// 单表DataAccess基类,所有单表DataAccess应当继承此类,建议非共用部分同样实现接口 /// 多表但单数据库操作的DataAccess不能继承此类,而应继承DataAccessBase /// </summary> /// /// public abstract class SingleDataAccessBase : DataAccessBase, IRepository where T1 : new() { #region SqlString /// <summary> /// GetEntityByKey方法对应的Sql语句 /// </summary> protected abstract string SelectSql { get; } /// <summary> /// Insert方法对应的Sql语句 /// </summary> protected abstract string InsertSql { get; } /// <summary> /// Update方法对应的Sql语句 /// </summary> protected abstract string UpdateSql { get; } /// <summary> /// Delete方法对应的Sql语句 /// </summary> protected abstract string DeleteSql { get; } #endregion #region IRepository 成员 /// <summary> /// 根据主键获取对应的实体对象 /// </summary> /// /// public vir<p style="color:transparent">本文来源gao!%daima.com搞$代*!码网1</p>tual T1 GetEntityByKey(T2 key) { return this.ReadDB.ExecuteBySqlString(this.SelectSql, null, (IRowMapper)null, this.GetKeyParameters(key).ToArray()).FirstOrDefault(); } /// <summary> /// 单个新增,如果是自增主键,则需要override /// </summary> /// /// public virtual bool Insert(T1 entity) { return this.WriteDB.ExecuteNonQueryBySqlString(this.InsertSql, (cmd) => { this.SetParametersByEntity(entity, cmd); }) > 0; } /// <summary> /// 单个编辑 /// </summary> /// /// public virtual bool Update(T1 entity) { return this.WriteDB.ExecuteNonQueryBySqlString(this.UpdateSql, (cmd) => { this.SetParametersByEntity(entity, cmd); }) > 0; } /// <summary> /// 单个删除 /// </summary> /// /// public virtual bool Delete(T2 key) { return this.WriteDB.ExecuteNonQueryBySqlString(this.DeleteSql, (cmd) => { this.SetParametersByKey(key, cmd); }) > 0; } #endregion #region Methods /// <summary> /// 通过Entity填充DbParameter /// </summary> /// /// protected abstract void SetParametersByEntity(T1 entity, DbCommand cmd); /// <summary> /// 通过Key填充DbParameter /// </summary> /// /// protected virtual void SetParametersByKey(T2 key, DbCommand cmd) { var paramters = this.GetKeyParameters(key); cmd.Parameters.AddRange(paramters.ToArray()); } /// <summary> /// 通过协变的方式根据Key获取对应的DbParameter /// </summary> /// /// protected abstract IEnumerable GetKeyParameters(T2 key); #endregion }