Abstract Factory plus reflection----Three-tier architecture for database conversion

Source: Internet
Author: User

Project requires the use of SQL Server databases to switch to an Access database. The requirement is simply to change the configuration in Webconfig to enable the data source to switch between SQL Server and Access . After thinking, think that can use abstract factory to achieve this function, behind the careful thought, feel a bit of trouble, behind the thought of using reflection to achieve.

The idea is this: the data access layer extracted from the interface Domeidal class library, and then different databases to establish different data access layer implementation interface, such as the use ofSQL ServerThen there is a correspondingSQL ServerThe Data Access Class library projectSQL ServerDAL, in which the class implements the interface in the Demoidal accordingly,AccessYesAccessThe DAL data Access class library Accordingly, so that different databases of access to the extraction. Then build a reflection factory demofactory, which is used to create different data access objects, to implement this, to configure the name of the assembly to use in webconfig, such as using the data access layerAccess, it's aAccessDAL, and then read the Webconfig configuration in the launch factory, so that it can load the assembly with a launch, and then pass the corresponding class name in the BLL layer, allowing the factory to produce objects of that class. This enables you to configure the name of the assembly that you want to use with the data access layer only in webconfig, so that you can use that data access layer to interview the database.


For example, in the data access layer there are two interfaces, Iuser and iproduct, that are implemented in SQL Server and access, as seen in the following ways:


Now through the business logic layer the BLL needs to instantiate an instance that implements Iuser to access the DAL layer

Assuming the direct use of Iuser userrep=new sqlserveruser (), there is a strong coupling relationship between the BLL layer and the DAL layer. Not perfect.

When the need to change the DAL layer is implemented for access, a large number of the instance code of the BLL layer new is required to be modified.

The ability to use abstract factory plus reflection to obtain information from a configuration file through a reflection mechanism to determine which method of the DAL layer is implemented.

Access to the BLL layer and DAL through the abstract factory is dependent on the factory to achieve the purpose of decoupling


PS: The configuration file must be in the interface layer

The notation of reflection:

Objtype=assembly.load (assemblypath). CreateInstance (ClassName);

Middle: AssemblyPath refers to the assembly name. ClassName refers to a namespace. Class name.

A principle of reflection:

Everything is centered on the DLL name in the bin directory of the UI layer. (The reason very easy:.net class loading mechanism is to find from the bin file of this assembly by default, so the bin directory must have the DLL for the assembly to be loaded). What is the name of the DLL in the bin directory in the UI layer, assemblypath, and what is the full name of the inner class of the bin, and what is the full name of classname? References in. NET: Adding a reference to an assembly will enable you to copy the DLL yourself when the assembly changes.



Abstract Factory plus reflection----Three-tier architecture for database conversion

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.