1. The concept of reflection has been explained in the blog in the previous section. Next, we start to analyze the business requirements logic, and then use the reflection principle to realize it;
2. Description:
Usually, only one database can be connected to operate data in a project, but some complex businesses will use the interaction between multiple databases.
3. Look at the project structure:
4. First of all, we don't need reflection technology to realize it. Look at the code:
Let's take oracle and sqlserver as examples:
//Database provider class public interface IDBHelper { /// <summary> /// add /// </summary> void Create(); /// <summary> /// modification /// </summary> void Update(); /// <summary> /// delete /// </summary> void Delete(); /// <summary> /// query /// </summary> void Select(); } //Class oracle public class Oracle:IDBHelper { public void Create() { Console.WriteLine("Oracle ,Add data operation!"); } public void Update() { Console.WriteLine("Oracle ,Modify data operation!"); } public void Delete() { Console.WriteLine("Oracle ,Delete data operation!"); } public void Select() { Console.WriteLine("Oracle ,Query data operation!"); } } //Class sqlserver public void Create() { Console.WriteLine("Sql Server ,Add data operation!"); } public void Update() { Console.WriteLine("Sql Server ,Modify data operation!"); } public void Delete() { Console.WriteLine("Sql Server ,Delete data operation!"); } public void Select() { Console.WriteLine("Sql Server ,Query data operation!"); }
Console Code:
class Program { static void Main(string[] args) { IDBHelper db = new Sql_Server();//Or IDBHelper db = new Oracle(); db.Create();//Newly added db.Update();//modify db.Delete();//delete db.Select();//query Console.ReadKey(); } }
The operation results are as follows:
In this way, the business requirements are realized, but if you change the library, it will be more difficult. You need to modify the written code again. Let's use reflection to solve this small bug:
First, add new configuration information in app.config:
<appSettings> <!-- <add key="DataBase.Operation" value="DataBase.Operation,DataBase.Operation.DataBase_Oracle.Oracle"/>-->//The information here can be modified at will without compiling the program <add key="DataBase.Operation" value="DataBase.Operation,DataBase.Operation.DataBase_SqlServer.Sql_Server"/> </appSettings>
Here I go to the console program:
class Program { static void Main(string[] args) { //Get the assembly information name in the configuration file string[] configs = ConfigurationManager.AppSettings["DataBase.Operation"].ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); Assembly assembly = Assembly.Load(configs[0]);//Load assembly Type t = assembly.GetType(configs[1]);//Gets the program information of a module specified in the assembly. object obj_Dbhelper = Activator.CreateInstance(t);//Create examples IDBHelper db = obj_Dbhelper as IDBHelper;//Convert to instance information db.Create();//Newly added db.Update();//modify db.Delete();//delete db.Select();//query Console.ReadKey(); } }
Operation effect:
Today, we will take notes on the expansion and application of reflection technology, and do it here. see you later ~