名字起得有点夸张了,其实就是实现 基于 Data Access Application Block的DAL基类和约束 首先Repository部分没什么好描述的,如果有不了解的可以直接百度或者谷歌相关内容,直接上具体代码 注意此部分没有写批量查询的方法(比如FindAll,这跟后面的基类设定
名字起得有点夸张了,其实就是实现基于Data Access Application Block的DAL基类和约束
首先Repository部分没什么好描述的,如果有不了解的可以直接百度或者谷歌相关内容,直接上具体代码
注意此部分没有写批量查询的方法(比如FindAll,这跟后面的基类设定有关)
/// <summary> /// DataAccess Repository /// </summary> /// <typeparam name="T1"></typeparam> /// <typeparam name="T2"></typeparam> public interface IRepository<t1 t2> { /// <summary> /// 根据主键获取对应的实体对象 /// </summary> /// <param name="key"> /// <returns></returns> T1 GetEntityByKey(T2 key); /// <summary> /// 单个新增 /// </summary> /// <param name="entity"> /// <returns></returns> bool Insert(T1 entity); /// <summary> /// 单个编辑 /// </summary> /// <param name="entity"> /// <returns></returns> bool Update(T1 entity); /// <summary> /// 单个删除 /// </summary> /// <param name="key"> /// <returns></returns> bool Delete(T2 key); }</t1>
/// <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); } } }
using System.Collections.Generic; using System.Linq; using Microsoft.Practices.EnterpriseLibrary.Data; using System.Data.Common; /// <summary> /// 单表DataAccess基类,所有单表DataAccess应当继承此类,建议非共用部分同样实现接口 /// 多表但单数据库操作的DataAccess不能继承此类,而应继承DataAccessBase /// </summary> /// <typeparam name="T1"></typeparam> /// <typeparam name="T2"></typeparam> public abstract class SingleDataAccessBase<t1 t2> : DataAccessBase, IRepository<t1 t2> 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<t1> 成员 /// <summary> /// 根据主键获取对应的实体对象 /// </summary> /// <param name="key"> /// <returns></returns> public virtual T1 GetEntityByKey(T2 key) { return this.ReadDB.ExecuteBySqlString(this.SelectSql, null, (IRowMapper<t1>)null, this.GetKeyParameters(key).ToArray()).FirstOrDefault(); } /// <summary> /// 单个新增,如果是自增主键,则需要override /// </summary> /// <param name="entity"> /// <returns></returns> public virtual bool Insert(T1 entity) { return this.WriteDB.ExecuteNonQueryBySqlString(this.InsertSql, (cmd) => { this.SetParametersByEntity(entity, cmd); }) > 0; } /// <summary> /// 单个编辑 /// </summary> /// <param name="entity"> /// <returns></returns> public virtual bool Update(T1 entity) { return this.WriteDB.ExecuteNonQueryBySqlString(this.UpdateSql, (cmd) => { this.SetParametersByEntity(entity, cmd); }) > 0; } /// <summary> /// 单个删除 /// </summary> /// <param name="key"> /// <returns></returns> 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> /// <param name="entity"> /// <param name="cmd"> protected abstract void SetParametersByEntity(T1 entity, DbCommand cmd); /// <summary> /// 通过Key填充DbParameter /// </summary> /// <param name="key"> /// <param name="cmd"> protected virtual void SetParametersByKey(T2 key, DbCommand cmd) { var paramters = this.GetKeyParameters(key); cmd.Parameters.AddRange(paramters.ToArray()); } /// <summary> /// 通过协变的方式根据Key获取对应的DbParameter /// </summary> /// <param name="key"> /// <returns></returns> protected abstract IEnumerable<dbparameter> GetKeyParameters(T2 key); #endregion }</dbparameter></t1></t1></t1></t1>
这里SingleDataAccessBase其实并不能算实现了IRepository,只是进行了相关的约束规范而已,实际子类需要提供相应的SqlString以及Parameter实现(注意此处用到了前面博客中写的微软企业库扩展,具体见[EntLib]微软企业库6 Data Access Application Block 扩展),因为Data Access Application Block其实并不是一个ORM,如果采用这个类库,其实已经有比较大的概率可以确定系统对性能的要求较高,当然你也可以通过反射之类的实现真正的Repository(前提是你的POCO与关系型数据库中字段能对应上)
SingleDataAccessBase子类建议划分区域块,对应的代码写入对应区域,如
#region ConnectionSetting protected override string ReadDBName { get { throw new NotImplementedException(); } } protected override string WriteDBName { get { throw new NotImplementedException(); } } #endregion #region SqlString protected override string SelectSql { get { throw new NotImplementedException(); } } protected override string InsertSql { get { throw new NotImplementedException(); } } protected override string UpdateSql { get { throw new NotImplementedException(); } } protected override string DeleteSql { get { throw new NotImplementedException(); } } #endregion #region Read #endregion #region Write #endregion #region DbParameter protected override void SetParametersByEntity(T1 entity, DbCommand cmd) { throw new NotImplementedException(); } protected override IEnumerable<dbparameter> GetKeyParameters(T2 key) { throw new NotImplementedException(); } #endregion</dbparameter>
可能很多人觉得区分多表操作的DAL和单表操作的DAL没有必要或者没有意义,的确,在功能上,写在一个里面和写在多个里面没有区别,系统较小时还没有什么问题,如果系统较大,经由的开发人员较多时,很可能会出现某个开发人员想要用到某个表的某些字段,但因为系统较大又没有相关约束,导致该开发人员不知道去哪里找是否该方法已经存在,为了简易起见,最常见的做法是直接在自己需要的地方新写这部分代码,而最终的后果就是,代码分布越来越混乱,当表结构发生变化时,此部分尤其变成了灾难,因为如果不通过查表名,你根本无法预见到底哪些地方用到了这张表!而区分单表及多表操作,就是要解决这样的问题,当表结构发生变化时,也只需要找该表对应的单表DAL以及以数据库对应的多表DAL两个地方
最后吐槽下,企业库5真心伤不起,为了用DataAccess模块,连连千千添加了5个相关dll引用,真心怪不得人家不愿意用,还好6没了这个问题……