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