在上一篇文章中講解了什么是反射,以及利用反射可以獲取程序集里面的哪些內(nèi)容。在平時的項目中,可能會遇到項目需要使用多種數(shù)據(jù)庫,這篇文章中將會講解如何利用反射實現(xiàn)訪問多種數(shù)據(jù)庫。 項目整體結(jié)構(gòu)如下圖所示: 1、Database.Instance是一個類庫文件,IDBHelper是一個接口,封裝的訪問數(shù)據(jù)庫數(shù)據(jù)的CURD方法,OracleDBHelper和SQLServerDBHelper類實現(xiàn)IDBHelper接口,分別用來訪問Oracle數(shù)據(jù)庫和SQL Server數(shù)據(jù)庫,接口和類的定義如下: IDBHelper接口定義 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Database.Instance.Interface 8 { 9 public interface IDBHelper 10 { 11 /// <summary> 12 /// 創(chuàng)建數(shù)據(jù) 13 /// </summary> 14 void Create(); 15 16 /// <summary> 17 /// 更新數(shù)據(jù) 18 /// </summary> 19 void Update(); 20 21 /// <summary> 22 /// 讀取數(shù)據(jù) 23 /// </summary> 24 void Retrieve(); 25 26 /// <summary> 27 /// 刪除數(shù)據(jù) 28 /// </summary> 29 void Delete(); 30 } 31 } OracleDBHelper類定義如下 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using Database.Instance.Interface; 7 8 namespace Database.Instance.Oracle 9 { 10 public class OracleDBHelper :IDBHelper 11 { 12 public void Create() 13 { 14 Console.WriteLine("這是Oracle數(shù)據(jù)庫執(zhí)行創(chuàng)建操作"); 15 } 16 17 public void Update() 18 { 19 Console.WriteLine("這是Oracle數(shù)據(jù)庫執(zhí)行更新操作"); 20 } 21 22 public void Retrieve() 23 { 24 Console.WriteLine("這是Oracle數(shù)據(jù)庫執(zhí)行讀取操作"); 25 } 26 27 public void Delete() 28 { 29 Console.WriteLine("這是Oracle數(shù)據(jù)庫執(zhí)行刪除操作"); 30 } 31 } 32 } SQLServerDBHelper類定義如下 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using Database.Instance.Interface; 7 8 namespace Database.Instance.SQL_Server 9 { 10 public class SQLServerDBHelper:IDBHelper 11 { 12 public void Create() 13 { 14 Console.WriteLine("這是SQL Server數(shù)據(jù)庫執(zhí)行創(chuàng)建操作"); 15 } 16 17 public void Update() 18 { 19 Console.WriteLine("這是SQL Server數(shù)據(jù)庫執(zhí)行更新操作"); 20 } 21 22 public void Retrieve() 23 { 24 Console.WriteLine("這是SQL Server數(shù)據(jù)庫執(zhí)行讀取操作"); 25 } 26 27 public void Delete() 28 { 29 Console.WriteLine("這是SQL Server數(shù)據(jù)庫執(zhí)行刪除操作"); 30 } 31 } 32 }
2、MyReflection是一個控制臺程序,用來測試 一、使用原始方法實現(xiàn) 使用原始的方法實現(xiàn)代碼如下: 1 using Database.Instance.Interface; 2 using Database.Instance.Oracle; 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 using System.Reflection; 9 using System.Configuration; 10 11 namespace MyReflection 12 { 13 class Program 14 { 15 static void Main(string[] args) 16 { 17 // 實例化(調(diào)用Oracle數(shù)據(jù)庫) 18 IDBHelper dbHelper = new OracleDBHelper(); 19 // 調(diào)用方法 20 dbHelper.Create(); 21 dbHelper.Update(); 22 dbHelper.Retrieve(); 23 dbHelper.Delete(); 24 25 Console.ReadKey(); 26 } 27 } 28 } 程序運行結(jié)果: 存在的問題:如果換一種數(shù)據(jù)庫,那么就需要修改實例化的代碼,例如更換SQL Server數(shù)據(jù)庫,那么代碼修改如下: IDBHelper dbHelper = new SQLServerDBHelper();
這樣很不方便,每次更換數(shù)據(jù)庫的時候,都需要修改實例化的代碼,有沒有什么方便的方法可以做到不需要修改代碼就可以實現(xiàn)更換數(shù)據(jù)庫呢?辦法就是使用反射加配置文件實現(xiàn)。 二、使用反射加配置文件實現(xiàn) 配置文件結(jié)構(gòu)如下: 1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <appSettings> 4 <!--key表示定義的接口 value格式 要加載的程序集名稱,要實例化的類 value值中間以','分割--> 5 <add key="Database.Instance.Interface.IDBHelper" value="Database.Instance,Database.Instance.Oracle.OracleDBHelper"/> 6 </appSettings> 7 <startup> 8 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" /> 9 </startup> 10 </configuration> Program類定義如下: 1 using Database.Instance.Interface; 2 using Database.Instance.Oracle; 3 using Database.Instance.SQL_Server; 4 using System; 5 using System.Collections.Generic; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Reflection; 10 using System.Configuration; 11 12 namespace MyReflection 13 { 14 class Program 15 { 16 static void Main(string[] args) 17 { 18 // 根據(jù)key值讀取對應的value值 19 string[] config = ConfigurationManager.AppSettings["Database.Instance.Interface.IDBHelper"].Split(','); 20 // 加載程序集 config[0]=Database.Instance 21 Assembly assembly = Assembly.Load(config[0]); 22 23 // 根據(jù)類的完全限定名找出類型 config[1]= Database.Instance.Oracle.OracleDBHelper 24 Type type = assembly.GetType(config[1]); 25 // 根據(jù)類型創(chuàng)建對象 26 object obj = Activator.CreateInstance(type); 27 //實例化 28 IDBHelper dbHelper = obj as IDBHelper; 29 dbHelper.Create(); 30 dbHelper.Update(); 31 dbHelper.Retrieve(); 32 dbHelper.Delete(); 33 Console.ReadKey(); 34 } 35 } 36 } 運行結(jié)果如下: 如果更新數(shù)據(jù)庫,只需要更新配置文件中value的值即可,例如要更換SQL Server數(shù)據(jù)庫,配置文件修改如下: 1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <appSettings> 4 <!--key表示定義的接口 value格式 要加載的程序集名稱,要實例化的類 value值中間以','分割--> 5 <add key="Database.Instance.Interface.IDBHelper" value="Database.Instance,Database.Instance.SQL_Server.SQLServerDBHelper"/> 6 </appSettings> 7 <startup> 8 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" /> 9 </startup> 10 </configuration> Program類不需要修改,運行結(jié)果如下: 示例代碼下載地址:https://pan.baidu.com/s/1mkf20WC |
|