Reflection + Abstract Factory mode switch DB data source (demo included)

Source: Internet
Author: User
Tags reflection switch case

First of all, the design pattern of the article originated in Geoscience "Big talk design mode" This book, this book personal feeling is suitable for me, watching not tired, can settle down reading study. Thanks to Geoscience's book here, the examples in my blog post are based on the examples in the book. In order not to infringe the copyright of this book, I will not write the specific code, only with their own code or ideas to describe, after all, this is to my own record, I write the article is to after I read the book myself to try to describe, deepen memory and understanding.

From this article, I will start my own methodical learning design patterns, an article of the record of my learning process!

The design pattern used in this blog post is abstract Factory mode, which is summed up in my own words: the different production forms of a product.

Here, first put the program structure diagram:

As can be seen, I have defined two entities, Department and User, here to simulate two tables in the database.

Now the situation is: I used SQL Server database to store the data, in response to customer needs, will be changed to Oracle database.

Take the user table as an example,

Here, I define a Iuser interface, which specifies two methods, the Insert method for inserting a single piece of data, and the GetUser method for reading a single piece of data.

The red box shows that I have created two new classes to execute the Oracle method separately and the SQL Server method.

All two of these classes implement the Iuser interface

Create a new DataAccess factory class that uses reflection to complete the automatic invocation of the class name in this class (I don't know if that's the right thing to say ...). )。

In the CreateUser method, use reflection to set up our pre-defined database. Here, I put the type configuration of the database in Web. config, which allows you to modify the database used by modifying the configuration file, just as the Access database uses the configuration file to store the connection characters.

1   <  >2     <!----3     <  key= "db"  value= "SQL Server"/>4   </appSettings>

In fact, this is the implementation of a variable to dynamically change the method name, here the db this string can be a different "class name", thus replacing the switch case.

Here, this program is basically completed, in addition to add Department related classes on it, so that the implementation of the switch db.

If we add a new table (product), such as project, then we just need to create a new model of the table and add the corresponding action method (for example: Insert ()) to the different database operation classes. Does not have any effect on other product methods.

If we need to add a new database, such as access, we just need to create a new access action class and change the value of db to access. You do not have to modify client code.

Specific about the use of reflection, here is unknown. After all, it is the main design pattern.

At last

I write in accordance with the procedure written by the process, may not be written clearly clear, if there is no written understanding, please give me a message, we come to discuss!

In addition, thanks to Geoscience's "Big Talk design mode" This excellent book, for me, perhaps the code concise Way of Enlightenment! Quote the word "no obsession, no success" in the book.

RELATED links:

Demo Download:http://download.csdn.net/detail/a406502972/7890969

Reflection + Abstract Factory mode switch DB data source (demo included)

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.