SqlHelperは簡単に実現する(Expressionと反射によって).Providorモード(ファクトリ+ポリシー)データベース選択を構成できます...

21615 ワード

基本思想は、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         /// 
 12         ///   Sql    
 13         /// 
 14         /// 
 15         /// 
 16         SqlSession GetSqlExpression() where T : class;
 17         /// 
 18         ///       
 19         /// 
 20         /// 
 21         /// 
 22         /// 
 23         List GetList(SqlSession exp) where T : class;
 24         /// 
 25         ///       ,   DTO 
 26         /// 
 27         /// 
 28         /// 
 29         /// 
 30         /// 
 31         List GetList(SqlSession exp) where T : class where Target : class;
 32         /// 
 33         ///   
 34         /// 
 35         /// 
 36         /// OrderBy   
 37         /// 
 38         /// 
 39         /// 
 40         List Paged(Expressionobject>> By, int pageIndex, int pageSize = 1) where T : class;
 41         /// 
 42         ///       
 43         /// 
 44         /// 
 45         /// linq   ,    
 46         /// 
 47         T GetSingle(Expressionbool>> func) where T : class;
 48         /// 
 49         ///       
 50         /// 
 51         /// 
 52         /// linq   ,    
 53         /// 
 54         int Delete(Expressionbool>> func) where T : class;
 55         /// 
 56         ///       
 57         /// 
 58         /// 
 59         /// 
 60         /// 
 61         int Add(T obj) where T : class;
 62         /// 
 63         ///     Sql
 64         /// 
 65         /// 
 66         /// 
 67         /// 
 68         int RunSingleSql(string sql) where T : class;
 69         /// 
 70         ///   Sql  DataTable,      
 71         /// 
 72         /// 
 73         /// 
 74         /// 
 75         DataTable GetDataBySql(string sql) where T : class;
 76         /// 
 77         ///       
 78         /// 
 79         ///      
 80         ///     
 81         /// linq   ,    
 82         /// 
 83         int Update(T obj, Expressionbool>> func) where T : class;
 84         /// 
 85         ///       
 86         /// 
 87         /// 
 88         ///       
 89         /// 
 90         int AddList(List objs) where T : class;
 91         /// 
 92         ///   
 93         /// 
 94         /// 
 95         /// linq   ,    
 96         /// 
 97         int Count(Expressionbool>> func = null) where T : class;
 98         /// 
 99         ///         
100         /// 
101         /// 
102         /// 
103         /// linq   ,    
104         /// linq   ,    
105         /// 
106         Target Scala(Expression> field, Expressionbool>> 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 }

 

:https://www.cnblogs.com/kakura/p/6108987.html