WinForm Development framework for simultaneous compatibility with multiple database types

Source: Internet
Author: User
Tags sqlite database

In many applications, although the general use of a database operation, but due to the needs of various situations, the business system may be deployed in different types of databases, if the development of the system can easily support a variety of database switching, it can reduce the number of problems for us, while improving the adaptability and robustness of the system. There is also a situation, due to the expansion of the business database or the convenience of database cutting isolation, sometimes the different business databases are split, such as the rights to provide a database, customer relationship Management database, workflow database, enterprise operations database, etc., so in a system, There are also cases where 2 or more databases are used at the same time.

In my earlier essay, "Support for multiple database type switching and database splitting in the WinForm development Framework", describes how the framework supports split processing of multiple databases in a single project. In general, we are in the case of a database type, split multiple databases, but there are abnormal requirements exceptions, such as we may have some of the regular database stored in the local SQLite database, some other data on the LAN other types of database (such as SQL Server) inside , so this same time supports a variety of database types, and for users to create the BLL layer class, dynamically specify whether the database can be implemented?

Of course, we can make a slight adjustment to the creation method on the basis above. 1, simultaneously supports multiple types of database processing coexistence

Before we introduced the need to split the database, we need to add a Setconfigname method in the data access base class Abstractbasedal to specify the specific database configuration item, as shown below.

/// <summary>    ///The super base class of the data access layer, all data access base classes of the database inherit from this super base class, including Oracle, SQL Server, Sqlite, MYSQL, access, etc./// </summary>     Public Abstract classAbstractbasedal<t>whereT:baseentity,New()    {        /// <summary>        ///Set Database configuration item name/// </summary>        /// <param name= "Dbconfigname" >Database configuration Item name</param>         Public Virtual voidSetdbconfigname (stringdbconfigname) {             This. Dbconfigname =Dbconfigname; }        ....................}

So this time we just need to adjust on this basis can be implemented at the same time to transform different database support, because in the framework, we generally have implemented a variety of database access logic (as shown below), so switching access is certainly not a problem (guaranteed to have a database).

By adding a public method to the BLL layer, you can set the function of the configuration item and the database type, as shown below.

/// <summary>        ///Reinitialize The data access layer based on the parameter information (example: You can specify a different data access layer)/// </summary>        /// <param name= "Dbconfigname" >Database configuration Item name</param>        /// <param name= "Componentdbtype" >The database type, which is read from Componentdbtype by default, and if Dbconfigname specifies a different type of database connection, you need to specify Componentdbtype. </param>         Public voidSetconfigname (stringDbconfigname,stringComponentdbtype =NULL)        {            //Componentdbtype = NULL when Componentdbtype value is taken from the configuration item            stringDbType =Componentdbtype; if(string. IsNullOrEmpty (Componentdbtype)) {AppConfig config=NewAppConfig (); DbType= Config. Appconfigget ("Componentdbtype"); }            stringDalprefix =Getdalprefix (DbType);  This. Dalname = Bllfullname.replace (Bllprefix, Dalprefix);//Replace Intermediate BLL. is the DAL., which is the full name of the Dal class .Basedal = Reflect<ibasedal<t>>. Create ( This. Dalname, Dalassemblyname);//constructs an object class for the corresponding DAL data access layer            if(!string. IsNullOrEmpty (Dbconfigname)) {basedal.setdbconfigname (dbconfigname);//Set Database configuration item name            }        }

This allows us to specify the type of the database in addition to the configuration items that can be set for enterpriselibrary, and does not require the use of uniform componentdbtype values.

The following code processing, we can access other databases, switch the BLL layer of the object for other types of database (SQLite), so no matter how other classes change, this province data access is the data in the SQLite database.

Bllfactory<province>. Instance.setconfig ("sqlite" "sqlite");
2, support the base class Commondal of multi-database operation, realize different database access

Sometimes, we are in a small application, want to flexible to the database table to do some simple processing, do not want to use the code generation tool to generate the entire schema code, then this time, this commondal comes in handy, this can quickly access the database table, it is defined as follows.

Several of the constructors for this class are as follows, with the table name, primary key field, and database type, respectively.

/// <summary>        ///default constructor/// </summary>         PublicCommondal () {}/// <summary>        ///Specifies the table name and the primary key, and constructs the base class into the/// </summary>        /// <param name= "TableName" >Table name</param>        /// <param name= "PrimaryKey" >Table PRIMARY Key</param>        /// <param name= "DbType" >The database type, if empty, gets the Componentdbtype key value from the configuration file</param>         PublicCommondal (stringTableName): This(TableName,NULL,NULL)        {        }        /// <summary>        ///Specifies the table name and the primary key, and constructs the base class into the/// </summary>        /// <param name= "TableName" >Table name</param>        /// <param name= "PrimaryKey" >Table PRIMARY Key</param>        /// <param name= "DbType" >The database type, if empty, gets the Componentdbtype key value from the configuration file</param>         PublicCommondal (stringTableName,stringPrimaryKey,stringDbType =NULL)            :  This()        {        }

This is a streamlined version of the Abstractbasedal base class that provides most of the database manipulation methods we can use.

For example, I use this class to implement a fast database manipulation process in an example program that validates views and their formats.

/// <summary>    ///tasks that determine whether a view name exists/// </summary>     Public classViewnameexistjob:iexecutejob { Public BOOLExecute () {List<string> list =datahelper.getviewlist (); BOOLAllsuccess =true; foreach(stringViewinchlist) {commondb dal=NewCommondb (View,"F_guid"); Try{DataTable dt=dal.                Getreaderschema (view); }                Catch(Exception ex) {allsuccess=false; Logtexthelper.error (string. Format ("view: {0} does not exist. {1}", view, ex.                Message)); }            }            if(allsuccess) {Logtexthelper.info ("View all exists. "); }            returnallsuccess; }    }
or other database access processing.

Commondb dal =NewCommondb (View,"F_guid"); Try                {                    intCount =dal.                    GetRecordCount (); if(Count = =0) {Logtexthelper.info (string. Format ("view ' {0} ' data is empty, please check! ", view)); Allsuccess=false; }                }                Catch(Exception ex) {allsuccess=false; Logtexthelper.error (string. Format ("view: ' {0} ' does not exist. {1}", view, ex.                Message)); }
This can also realize the random switching of multiple databases, but this is for easy database access, for processing classes that require a variety of business packages, we also use the regular framework layering pattern to implement data processing operations.

WinForm Development framework for simultaneous compatibility with multiple database types

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.