SqlHelper简单实现(通过Expression和反射)6.Providor模式(工厂+策略)可配置数据库选择
基本思想,将MsSqlDbUtility和MySqlDbUtility设计成单例模式,通过App.config或Web.config进行默认的数据库设置,然后通过DbUtilityFactory获取DbUtility。
1.接口:
1 using System; 2 using System.Collections.Generic; 3 using System.Data; 4 using System.Linq.Expressions; 5 using RA.DataAccess.MsSqlDbUtility; 6 7 namespace RA.DataAccess 8 { 9 public interface IDbUtility 10 { 11 /// <summary> 12 /// 获取Sql描述对象 13 /// </summary> 14 /// <typeparam name="T"></typeparam> 15 /// <returns></returns> 16 SqlSession<T> GetSqlExpression<T>() where T : class; 17 /// <summary> 18 /// 获取多条数据 19 /// </summary> 20 /// <typeparam name="T"></typeparam> 21 /// <param name="exp"></param> 22 /// <returns></returns> 23 List<T> GetList<T>(SqlSession<T> exp) where T : class; 24 /// <summary> 25 /// 获取多条数据,并填入DTO中 26 /// </summary> 27 /// <typeparam name="Target"></typeparam> 28 /// <typeparam name="T"></typeparam> 29 /// <param name="exp"></param> 30 /// <returns></returns> 31 List<Target> GetList<Target, T>(SqlSession<T> exp) where T : class where Target : class; 32 /// <summary> 33 /// 分页 34 /// </summary> 35 /// <typeparam name="T"></typeparam> 36 /// <param name="By">OrderBy表达式</param> 37 /// <param name="pageIndex"></param> 38 /// <param name="pageSize"></param> 39 /// <returns></returns> 40 List<T> Paged<T>(Expression<Func<T, object>> By, int pageIndex, int pageSize = 1) where T : class; 41 /// <summary> 42 /// 获取单条数据 43 /// </summary> 44 /// <typeparam name="T"></typeparam> 45 /// <param name="func">linq表达式,代表条件</param> 46 /// <returns></returns> 47 T GetSingle<T>(Expression<Func<T, bool>> func) where T : class; 48 /// <summary> 49 /// 删除一条数据 50 /// </summary> 51 /// <typeparam name="T"></typeparam> 52 /// <param name="func">linq表达式,代表条件</param> 53 /// <returns></returns> 54 int Delete<T>(Expression<Func<T, bool>> func) where T : class; 55 /// <summary> 56 /// 添加一条数据 57 /// </summary> 58 /// <typeparam name="T"></typeparam> 59 /// <param name="obj"></param> 60 /// <returns></returns> 61 int Add<T>(T obj) where T : class; 62 /// <summary> 63 /// 直接执行Sql 64 /// </summary> 65 /// <typeparam name="T"></typeparam> 66 /// <param name="sql"></param> 67 /// <returns></returns> 68 int RunSingleSql<T>(string sql) where T : class; 69 /// <summary> 70 /// 通过Sql获取DataTable,业务层不使用 71 /// </summary> 72 /// <typeparam name="T"></typeparam> 73 /// <param name="sql"></param> 74 /// <returns></returns> 75 DataTable GetDataBySql<T>(string sql) where T : class; 76 /// <summary> 77 /// 更新一条数据 78 /// </summary> 79 /// <typeparam name="T">数据表实体</typeparam> 80 /// <param name="obj">实体对象</param> 81 /// <param name="func">linq表达式,代表条件</param> 82 /// <returns></returns> 83 int Update<T>(T obj, Expression<Func<T, bool>> func) where T : class; 84 /// <summary> 85 /// 添加多条数据 86 /// </summary> 87 /// <typeparam name="T"></typeparam> 88 /// <param name="objs">实体对象列表</param> 89 /// <returns></returns> 90 int AddList<T>(List<T> objs) where T : class; 91 /// <summary> 92 /// 计数 93 /// </summary> 94 /// <typeparam name="T"></typeparam> 95 /// <param name="func">linq表达式,代表条件</param> 96 /// <returns></returns> 97 int Count<T>(Expression<Func<T, bool>> func = null) where T : class; 98 /// <summary> 99 /// 获取一个字段数据 100 /// </summary> 101 /// <typeparam name="T"></typeparam> 102 /// <typeparam name="Target"></typeparam> 103 /// <param name="field">linq表达式,代表字段</param> 104 /// <param name="func">linq表达式,代表条件</param> 105 /// <returns></returns> 106 Target Scala<T, Target>(Expression<Func<T, Target>> field, Expression<Func<T, bool>> func); 107 } 108 }
2.工厂类:
1 using System; 2 using System.Configuration; 3 4 namespace RA.DataAccess 5 { 6 public class DbUtilityFactory 7 { 8 public static IDbUtility GetDbUtility() 9 { 10 var getDbType = ConfigurationManager.AppSettings["DbType"]; 11 if (getDbType == "MySql") 12 { 13 return MySqlDbUtility.DbUtility.GetInstance(); 14 } 15 else if (getDbType == "MsSql") 16 { 17 return MsSqlDbUtility.DbUtility.GetInstance(); 18 } 19 else 20 { 21 throw new NotSupportedException("不支持的数据库"); 22 } 23 } 24 } 25 26 27 }