Using data factories to implement multiple database operations in. Net

Source: Internet
Author: User
Tags mysql database oracle database

During the development of the project, when the concept of design patterns has not yet come out, we are writing programs, often if the project's database is SQL Server, and then users want to exchange other databases such as Oracle, we need to rewrite their code, especially in some software of the product path , we make out products if users can have a choice to choose a variety of databases, it will undoubtedly provide users with great convenience.

Since the factory model design concept came out, all this has become much easier, if you have a study of Microsoft's PetShop, it will not be unfamiliar, starting from PETSHOP3.0, Microsoft began to use a multiple database operating system applications. The data factory is mainly through the connection of the database into an abstract factory, such as named Dalfactory, all of the database connections in the program are generated through this factory class, which is responsible for the dynamic creation of the system based on the configuration of the data Access logic objects required.

Let's take PetShop to illustrate, petshop in the installation, will prompt us to choose what database, such as according to the display of SQL Server database or Oracle database, you can get Web.config node

<add key=" WebDAL " value=" PetShop.SQLServerDAL "/>
<add key=" OrdersDAL " value=" PetShop.SQLServerDAL "/>

Or is

<add key=" WebDAL " value=" PetShop.OracleDAL "/>
<add key=" OrdersDAL " value=" PetShop. OracleDAL "/>

Then call the database connection in the DataAccess class of the Dalfactory project, as follows:

private static readonly string path = configurationmanager.appsettings["Webdal"];

And then look at the following code:

public static PetShop.IDAL.ICategory CreateCategory() {
      string className = path + ".Category";
      return (PetShop.IDAL.ICategory)Assembly.Load(path).CreateInstance(className);
    }

If we are using SQL Server, then string className = Path +. Category "Return is the Petshop.sqlserverdal. Category, and then load the PetShop.SQLServerDAL.DLL with Assembly.Load, and create an instance of PetShop.SQLServerDAL.Category with an interface ( PetShop.IDAL.ICategory) type returns. This way the business logic layer BLL The implementation code of the PetShop.SQLServerDAL.Category class when calling the ICategory interface.

At this time the user does not need to know what kind of database is used in the background, it just calls the interface on the line, in the interface defined the method to use, when the call interface will be based on the specific circumstances of the call to the underlying data access operations. And now this dalfactory is the key, when the business logic layer to operate the database, Dalfactory will be based on the specific situation to use the generated assembly Sqlserverdal or Oracledal, The advantage of this is that the business logic layer and the Web page layer program will not be affected by the changes in the underlying data access program, as it is only necessary to invoke the interface in the business logic layer.

It may be mentioned that I also provide the following method in the factory class to implement the call database:

public static readonly DALFactory dalFa;
string webDal = ConfigurationManager.AppSettings["WebDAL"];
switch (webDal)
{
  case "SQLServerDAL":
    dalFa = new SqlServerDALFactory();
    break;
  case "OracleDAL":
    dalFa = new OracleDALFactory();
    break;
  default:
    dalFa = new SqlServerDALFactory();
    break;
}

And this time, if we add a new way of accessing the database, you have to modify this part of the program, and then recompile the deployment, but also using the reflection mechanism to implement, we give an example if the system now need to increase the MySQL database, we look at its code scalability, We can compare the Category.cs files below the Sqlserverdal in PetShop and the Category.cs files below Oracledal to know that because they all inherit the ICategory interface, so the class implements the same method, at which point we only need to add one m Ysqldal Project, the following Category.cs file also follows the ICategory interface method, at which point we will change to

This time does not need to compile the project, only need to add MySqlDAL.DLL file can, no matter how many database, is a very simple operation, data Factory operation of the advantages of multiple data is visible.

<add key=" WebDAL " value=" PetShop.SQLServerDAL "/>为<add key=" WebDAL " value=" PetShop.MySqlDAL "/>

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.