搜索
您的当前位置:首页正文

[EntLib]微软企业库6基于DataAccessApplicationBlock的Repos

2020-11-09 来源:品趣旅游知识分享网

名字起得有点夸张了,其实就是实现 基于 Data Access Application Block的DAL基类和约束 首先Repository部分没什么好描述的,如果有不了解的可以直接百度或者谷歌相关内容,直接上具体代码 注意此部分没有写批量查询的方法(比如FindAll,这跟后面的基类设定

名字起得有点夸张了,其实就是实现基于Data Access Application Block的DAL基类和约束

首先Repository部分没什么好描述的,如果有不了解的可以直接百度或者谷歌相关内容,直接上具体代码

注意此部分没有写批量查询的方法(比如FindAll,这跟后面的基类设定有关)

 /// 
 /// DataAccess Repository
 /// 
 /// 
 /// 
 public interface IRepository
 {
 /// 
 /// 根据主键获取对应的实体对象
 /// 
 /// 
 /// 
 T1 GetEntityByKey(T2 key);
 /// 
 /// 单个新增
 /// 
 /// 
 /// 
 bool Insert(T1 entity);
 /// 
 /// 单个编辑
 /// 
 /// 
 /// 
 bool Update(T1 entity);
 /// 
 /// 单个删除
 /// 
 /// 
 /// 
 bool Delete(T2 key);
 }
然后是DAL抽象基类,该基类设计上实现读写分离,而且每个子类只应有一个数据库连接,这样做的隐藏目的是每个DAL类都只应该执行自己最基本的功能:只关心数据库交互,不关心业务逻辑
 /// 
 /// DAL基类,基于EntLib,读写分离
 /// 
 public abstract class DataAccessBase
 {
 private Database _readDB;
 private Database _writeDB;
 /// 
 /// 要使用的读数据库配置节,如果配置为null或者空则会调用默认配置节
 /// 
 protected abstract string ReadDBName { get; }
 /// 
 /// 要使用的写数据库配置节,如果配置为null或者空则会调用默认配置节
 /// 
 protected abstract string WriteDBName { get; }
 /// 
 /// 读库
 /// 
 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);
 }
 }
 /// 
 /// 写库
 /// 
 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;
 /// 
 /// 单表DataAccess基类,所有单表DataAccess应当继承此类,建议非共用部分同样实现接口
 /// 多表但单数据库操作的DataAccess不能继承此类,而应继承DataAccessBase
 /// 
 /// 
 /// 
 public abstract class SingleDataAccessBase : DataAccessBase, IRepository
 where T1 : new()
 {
 #region SqlString
 /// 
 /// GetEntityByKey方法对应的Sql语句
 /// 
 protected abstract string SelectSql { get; }
 /// 
 /// Insert方法对应的Sql语句
 /// 
 protected abstract string InsertSql { get; }
 /// 
 /// Update方法对应的Sql语句
 /// 
 protected abstract string UpdateSql { get; }
 /// 
 /// Delete方法对应的Sql语句
 /// 
 protected abstract string DeleteSql { get; }
 #endregion

 #region IRepository 成员
 /// 
 /// 根据主键获取对应的实体对象
 /// 
 /// 
 /// 
 public virtual T1 GetEntityByKey(T2 key)
 {
 return this.ReadDB.ExecuteBySqlString(this.SelectSql, null, (IRowMapper)null, this.GetKeyParameters(key).ToArray()).FirstOrDefault();
 }
 /// 
 /// 单个新增,如果是自增主键,则需要override
 /// 
 /// 
 /// 
 public virtual bool Insert(T1 entity)
 {
 return this.WriteDB.ExecuteNonQueryBySqlString(this.InsertSql, (cmd) => { this.SetParametersByEntity(entity, cmd); }) > 0;
 }
 /// 
 /// 单个编辑
 /// 
 /// 
 /// 
 public virtual bool Update(T1 entity)
 {
 return this.WriteDB.ExecuteNonQueryBySqlString(this.UpdateSql, (cmd) => { this.SetParametersByEntity(entity, cmd); }) > 0;
 }
 /// 
 /// 单个删除
 /// 
 /// 
 /// 
 public virtual bool Delete(T2 key)
 {
 return this.WriteDB.ExecuteNonQueryBySqlString(this.DeleteSql, (cmd) => { this.SetParametersByKey(key, cmd); }) > 0;
 }

 #endregion

 #region Methods
 /// 
 /// 通过Entity填充DbParameter
 /// 
 /// 
 /// 
 protected abstract void SetParametersByEntity(T1 entity, DbCommand cmd);
 /// 
 /// 通过Key填充DbParameter
 /// 
 /// 
 /// 
 protected virtual void SetParametersByKey(T2 key, DbCommand cmd)
 {
 var paramters = this.GetKeyParameters(key);
 cmd.Parameters.AddRange(paramters.ToArray());
 }
 /// 
 /// 通过协变的方式根据Key获取对应的DbParameter
 /// 
 /// 
 /// 
 protected abstract IEnumerable GetKeyParameters(T2 key);
 #endregion
 }

这里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 GetKeyParameters(T2 key)
 {
 throw new NotImplementedException();
 }
 #endregion

可能很多人觉得区分多表操作的DAL和单表操作的DAL没有必要或者没有意义,的确,在功能上,写在一个里面和写在多个里面没有区别,系统较小时还没有什么问题,如果系统较大,经由的开发人员较多时,很可能会出现某个开发人员想要用到某个表的某些字段,但因为系统较大又没有相关约束,导致该开发人员不知道去哪里找是否该方法已经存在,为了简易起见,最常见的做法是直接在自己需要的地方新写这部分代码,而最终的后果就是,代码分布越来越混乱,当表结构发生变化时,此部分尤其变成了灾难,因为如果不通过查表名,你根本无法预见到底哪些地方用到了这张表!而区分单表及多表操作,就是要解决这样的问题,当表结构发生变化时,也只需要找该表对应的单表DAL以及以数据库对应的多表DAL两个地方

最后吐槽下,企业库5真心伤不起,为了用DataAccess模块,连连千千添加了5个相关dll引用,真心怪不得人家不愿意用,还好6没了这个问题……

Top