Support for switching multiple database types and splitting databases in the Winform development framework-

Source: Internet
Author: User
In many application systems, although a database is generally used for running, the business system may be deployed on different types of databases due to various situations, if the developed system can easily support switching between multiple databases, it can reduce a lot of troubles and improve the adaptability and robustness of the system. Another case is that the number of services

In many application systems, although a database is generally used for running, the business system may be deployed on different types of databases due to various situations, if the developed system can easily support switching between multiple databases, it can reduce a lot of troubles and improve the adaptability and robustness of the system. Another case is that the number of services

In many application systems, although a database is generally used for running, the business system may be deployed on different types of databases due to various situations, if the developed system can easily support switching between multiple databases, it can reduce a lot of troubles and improve the adaptability and robustness of the system. In another case, due to the constant expansion of the business database or the convenience of database cutting and isolation, different business databases are sometimes split, such as the database providing permissions and the customer relationship management database, workflow databases, Enterprise Operation databases, and so on. Therefore, two or more databases are used in one system. This article describes how to solve these two problems in my Winform development framework (also using my other frameworks.

In my various development frameworks, the bottom layer adopts the same database access mode, that is, the database access module of Enterprise Library. This is an open-source Enterprise application module of Microsoft, the various application modules are the best practices for development. Of course, the database access module is widely used. This database access module supports various database changes through configuration. Therefore, it is easy to implement this multi-database processing method when integrated into my Winform development framework.

The Winform development framework and common layered modes can be divided into the UI Layer, BLL layer, DAL layer, IDAL layer, Entity layer, and public class library layer, the B/S Web development framework also provides similar architecture models. They only differ in the Web interface layer. This provides us with a uniform development model, making development more efficient, uniformity is better. The architecture diagram below the framework interface layer is as follows.



<------------- <_ <Left-side view

1. Supports multi-database settings

1) Understanding of Database Access Base classes

To introduce the modes that support multiple databases, we need to first understand the hierarchy of the entire framework. <喎?http: www.2cto.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + primary + nP88HLy/nT0Mr9vt2/primary + cDgoaM8L3A + primary + 3b/isr + 31rX31fu7 + primary/b7dv + K1xLK/primary/hidden + primary/4 rXEsr + 31rX31fu7 + release/b7dv + K1xLK/release + 6OpoaM8L3A + release/nT0Lv5tKHK/b7dv + release/nA4KOsyOe7 + release/nA4KOsy/release + release + jqUFic3RyYWN0QmFzZURBTKGjy/vDx9auvOS1xLzMs9C52M + 1yOfPwsv5yr48L3A + CjxwPgo8aW1nIHNyYz0 = "http://www.2cto.com/uploadfile/Collfiles/20140613/2014061309052829.png" alt = "\">

As we can see in the project diagram just now, the class libraries BaseDALSQL, IBaseDAL, and AbstractBaseDAL have great versatility. To reduce the maintenance problems caused by Replication in different projects, therefore, we extract all these frequently used base classes or interfaces into an independent class library to distinguish them from common DotNET public class library names (WHC. framework. commons), we name it WHC. framework. controlUtil.

2) How can I fix the code in multiple databases? http://www.2cto.com/kf/yidong/wp/ "Target =" _ blank "class =" keylink "> encrypt/decrypt + wO/encrypt/decrypt/b7dv + decrypt + yu82stcSzzNDyvK/A4KOstNO2 + encrypt/4sd1_m21xmr9vt2/decrypt/K/cDvw + a1xMq1z9a0 + latest + CiNyZWdpb24guPm + latest/latest + Cn08L3A + latest + Cns8L3A + latest + Cn08L3A + latest + signature + Cns8L3A + signature/s8L3A + signature/PA4DwvcD4KPHA + CtTavt/Signature + 3b/itcS0psDtwOC9 + signature/Signature/avs2/Signature + ay47XEsr + 31rX308O0 + sLroaM8L3A + CjxwPgovL8m + s/252MGqtcS4vbz + signature + Cns8L3A + Signature = ", ');

Foreach (string id in idArray)

{

InformationInfo info = BLLFactory. FindByID (id );

If (info! = Null &&! String. IsNullOrEmpty (info. Attachment_GUID ))

{

BLLFactory. Instance. DeleteByAttachGUID (info. Attachment_GUID );

}

}

}

In the specific configuration file, we can configure the relevant database as needed.

According to the configuration of the Enterprise Library, we only need to make it, then we will get the sqlServer node, and the code can construct the corresponding database access object by parsing the ComponentDbType configuration item. When the two are combined, the processing object can be correctly obtained and the database access can be processed successfully.

2. Support settings for splitting different databases

As described above, only one database can be accessed at a time. Therefore, the following code is used by default to construct database access objects.

Database db = DatabaseFactory. CreateDatabase ();

In this way, only the database set by defaultDatabase will be obtained each time for construction. If we support access to multiple databases in one system at the same time, what should we do.

In the Framework's base class AbstractBaseDAL, we encapsulate the code used to construct the database.

///

/// Generate a Database object based on the configuration name of the configuration Database

///

///

Protectedvirtual Database CreateDatabase ()

{

Database db = null;

If (string. IsNullOrEmpty (dbConfigName ))

{

Db = DatabaseFactory. CreateDatabase ();

}

Else

{

Db = DatabaseFactory. CreateDatabase (dbConfigName );

}

Return db;

}

Each DAL layer inherits from AbstractBaseDAL, so that you can use different databases by specifying dbConfigName In the DAL layer.

However, this problem occurs. If we have five DAL layers of different types of databases (SqlServer, Oracle, Mysql, Access, Sqlite, therefore, it is inconvenient to write some code for the Implementation classes of each DAL layer. Can you abstract the code to the BLL layer so that only one configuration can be written? This is a good idea, let's take a look at how to implement it.

1) define interfaces at the IBaseDAL Layer

///

/// Data access layer interface

///

Publicinterface IBaseDAL where T: BaseEntity

{

///

/// Set the database configuration item name

///

///

Database Configuration Item Name

Void SetDbConfigName (string dbConfigName );

.............

}

2. Add the default implementation code in AbstractBaseDAL.

///

/// Super base class at the data Access layer. The data Access base classes of all databases inherit from this super base class, including Oracle, SqlServer, Sqlite, MySql, and Access.

///

Publicpolicactclass AbstractBaseDAL where T: BaseEntity, new ()

{

///

/// Set the database configuration item name

///

///

Database Configuration Item Name

Publicvirtualvoid SetDbConfigName (string dbConfigName)

{

This. dbConfigName = dbConfigName;

}

....................

}

3. call and set the Init function in BaseBLL.

///

/// Initialize the object after the parameter value is assigned

///

///

The full name of the BLL service class (which must be implemented by the subclass). The subclass constructor transmits this. GetType (). FullName

///

The name of the configuration file of the data access layer assembly, excluding its extension. Set to NULL or the default value is Assembly. GetExecutingAssembly (). GetName (). Name.

///

Prefix of the BLL namespace (BLL .)

///

Database Configuration Item Name

Protectedvoid Init (string bllFullName, string dalAssemblyName = null, string bllPrefix = "BLL .")

{

.............

BaseDal. SetDbConfigName (dbConfigName); // you can specify the name of a database configuration item.

}

4. initialize the business class at the specific BLL layer.

///

/// Announcement of policies and regulations

///

Publicclass Information: BaseBLL

{

Public Information (): base ()

{

Base. Init (this. GetType (). FullName, System. Reflection. Assembly. GetExecutingAssembly (). GetName (). Name, "BLL.", "annotherConfig ");

}

After this setting, the code of the specific call remains unchanged, but the specific configuration item name is used for database access of the specified business. If the configuration item does not exist, the default configuration items will be obtained for processing.

Through this implementation step, we can implement a business system, split different databases for unified management, without increasing the call difficulty. For many of our business tables, the processing method of this framework should be good.

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.