Using a data factory in. Net for multi-database operations _ MySQL

Source: Internet
Author: User
In. in the development of the project, when the concept of the design pattern has not yet come out, when we compile the program, if the project database uses SQL Server and users want to change other databases, such as Oracle, we need to rewrite the code, especially in the productization of some software,

If our products allow users to select a variety of databases, it will undoubtedly provide users with great convenience.

Since the design concept of the factory model came out, all these implementations have become much easier. if you have research on Microsoft's PETSHOP, you will not be unfamiliar, starting from PETSHOP3.0, microsoft began to adopt applications of multi-database operating systems. The data pipeline mainly creates an abstract factory by connecting the database, such as the DALFactory name. all database connections in the program are generated through this factory class, it is used to dynamically create data access logic objects required by the system based on the configuration file.

Let's take PETSHOP as an example. during the installation of PETSHOP, we will prompt you to select a database. for example, you can obtain the Web page based on whether the database is SQL Server or Oracle. in the config node

  


Or

  


Then, call the database connection in the DataAccess class of the DALFactory project. the code is as follows:

Private static readonly string path = ConfigurationManager. deleettings ["WebDAL"];

Then let's look at the following code:

Public static PetShop. IDAL. ICategory CreateCategory (){
String className = path + ". Category ";
Return (PetShop. IDAL. ICategory) Assembly. Load (path). CreateInstance (className );
}


For example, if we use SQL Server, then string className = path + ". category "returns PetShop. SQLServerDAL. category, and then use Assembly. load to Load PetShop. SQLServerDAL. DLL and create PetShop. SQLServerDAL. category instance, and use the interface (PetShop. IDAL. ICategory) type. In this way, the business logic layer BLL uses the implementation code of the PetShop. SQLServerDAL. Category class to call the ICategory interface.

At this time, the user does not need to know which database is used in the background. it only needs to call the interface and define the method to be used in the interface, when an interface is called, the underlying data access operation is called based on the actual situation. Now this DALFactory is the key. when the business logic layer needs to operate the database, DALFactory will use one of the generated assembly SQLServerDAL or OracleDAL based on the actual situation, the advantage of this is that the program on the business logic layer and web page layer will not be affected by program changes in the underlying data access, because you only need to call the interface in the business logic layer.

Someone may mention that I also provide the following methods in the factory class to call the database:

Public static readonly DALFactory dalFa;
String webDal = ConfigurationManager. receivettings ["WebDAL"];
Switch (webDal)
{
Case "SQLServerDAL ":
DalFa = new SqlServerDALFactory ();
Break;
Case "incluledal ":
DalFa = new OracleDALFactory ();
Break;
Default:
DalFa = new SqlServerDALFactory ();
Break;
}


At this time, if we add a new database access method, we must modify this part of the program and then re-compile and deploy the program. Similarly, when we use the reflection mechanism to implement it, for example, if the MySQL database needs to be added in the system, let's take a look at its code scalability. we can compare the Category under SQLServerDAL in PETSHOP. cs file and Category under ledal. the codes of cs files are known because they all inherit the ICategory interface, so the class implementation methods are the same. in this case, we only need to add a MySqlDAL project under which the Category is located. the cs file also follows the ICategory interface method. at this time, we will change it

At this time, you do not need to re-compile the project, just add MySqlDAL. DLL files can be used. no matter how many databases are added, it is a very simple operation. the advantages of multi-data operations in the data factory are obvious.


Is

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.