Home Database Mysql Tutorial Winform开发框架中实现多种数据库类型切换以及分拆数据库的支持-

Winform开发框架中实现多种数据库类型切换以及分拆数据库的支持-

Jun 07, 2016 pm 03:57 PM
switch accomplish develop database frame type

在很多应用系统里面,虽然一般采用一种数据库运行,但是由于各种情况的需要,可能业务系统会部署在不同类型的数据库上,如果开发的系统能够很方便支持多种数据库的切换,那可以为我们减少很多烦恼,同时提高系统的适应性和强壮型。还有一种情况,由于业务数

在很多应用系统里面,虽然一般采用一种数据库运行,但是由于各种情况的需要,可能业务系统会部署在不同类型的数据库上,如果开发的系统能够很方便支持多种数据库的切换,那可以为我们减少很多烦恼,同时提高系统的适应性和强壮型。还有一种情况,由于业务数据库的不断膨胀或者方便数据库的切割隔离,有时候也会把不同的业务数据库进行分拆,如权限提供数据库,客户关系管理数据库,工作流程数据库,企业营运数据库等等,因此在一个系统里面,同时使用2个或者以上的数据库的情况也是有的。针对这两种情况,本文介绍在我的Winform开发框架(也使用我的其他框架),如何具体处理这两个问题的。

在我的各种开发框架里面,底层都是采用同一种数据库的访问方式,就是采用了Enterprise Library的数据库访问模块,这个是微软开源的企业应用模块,里面各种应用模块,都堪称是开发的最佳实践。当然利用里面的数据库访问模块,是很广泛的,这个数据库访问模块,可以通过配置的方式支持多种数据库的变化。因此整合到我的Winform开发框架里面,也很容易实现这种多数据库的方式处理。

Winform开发框架,常见的分层模式,可以分为UI层、BLL层、DAL层、IDAL层、Entity层、公用类库层等等,B/S的Web开发框架也是提供类似的架构模式,它们只是Web界面层有所不同,这样就给我们提供了一个统一性的开发模式,使得开发起来更加高效,统一性更好。框架界面层以下的架构设计图如下所示。

\


1、支持多数据库的设置

1)数据库访问基类的了解

为了介绍支持多种数据库的模式,我们需要先来了解下整个框架的层次结构。喎?http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+CkFic3RyYWN0QmFzZURBTMrHs+nP88HLy/nT0Mr9vt2/4sq1z9a1xLOsvLa7+cDgoaM8L3A+CjxwPgpCYXNlREFMU1FMysfV67bUU3FsU2VydmVyyv2+3b/isr+31rX31fu7+cDgo6i63NChtcS199X7o6mhozwvcD4KPHA+CkJhc2VEQUxTUUxpdGXKx9XrttRTcWxpdGXK/b7dv+K1xLK/t9a199X7u/nA4KOoutzQobXEtffV+6OpoaM8L3A+CjxwPgpCYXNlREFMTXlTcWzKx9XrttRNeVNxbGl0Zcr9vt2/4rXEsr+31rX31fu7+cDgo6i63NChtcS199X7o6mhozwvcD4KPHA+CkJhc2VEQUxBY2Nlc3PKx9XrttRBY2Nlc3PK/b7dv+K1xLK/t9a199X7u/nA4KOoutzQobXEtffV+6OpoaM8L3A+CjxwPgpJQmFzZURBTMrHy/nT0Lv5tKHK/b7dv+K3w87KwOC1xL3Tv9qhozwvcD4KPHA+Csr9vt23w87KvdO/2sq1z9ay46OoyOdDdXN0b21lcqOpus2907/atqjS5bLjo6hJQ3VzdG9tZXKjqdK70fmjrLa809DSu7j2u/nA4KOsyOe7+dPaU3FsU2VydmVyyrXP1rXEu/nA4M6qQmFzZURBTFNRTKOs1eK49rv509pTcWxTZXJ2ZXK1xMr9vt23w87Ku/nA4KOsy/zSssrHvMyz0NfU0ru49rOsvLa7+cDgo6i087bgyv21xMq1z9bU2tXiwO+jqUFic3RyYWN0QmFzZURBTKGjy/vDx9auvOS1xLzMs9C52M+1yOfPwsv5yr48L3A+CjxwPgo8aW1nIHNyYz0="http://www.2cto.com/uploadfile/Collfiles/20140613/2014061309052829.png" alt="\">

而我们刚才在项目工程的图里面看到,BaseDALSQL、IBaseDAL、AbstractBaseDAL这些类库由于具有很大的通用性,为了减少在不同的项目中进行复制导致维护问题,因此我们全部把这些经常使用到的基类或者接口,抽取到一个独立的类库里面,为了和普通的DotNET公用类库命名进行区分(WHC.Framework.Commons),我们把它命名为WHC.Framework.ControlUtil。

2)多数据库的代码实馅喎?http://www.2cto.com/kf/yidong/wp/" target="_blank" class="keylink">WPC9wPgo8cD4KzqrBy8q1z9a24Mr9vt2/4rXE1qez1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.FindByID(id);

if (info != null&& !string.IsNullOrEmpty(info.Attachment_GUID))

{

BLLFactory.Instance.DeleteByAttachGUID(info.Attachment_GUID);

}

}

}

在具体的配置文件里面,我们就可以根据需要配置好相关的数据库了。

根据Enterprise Library的配置,我们只要制定了,那么就会获取sqlServer的节点,而代码通过解析ComponentDbType配置项,就可以构造对应的数据库访问对象了。两者合一就可以正确获取到处理对象,并成功处理数据库的访问。

2、支持分拆不同数据库的设置

上面介绍的方式,一次性只能访问一个数据库,因此默认在代码构造数据库访问对象的时候,是通过下面的代码进行的。

Database db = DatabaseFactory.CreateDatabase();

这样每次只会获取defaultDatabase设置的数据库进行构造,如果我们在一个系统里面,同时支持多个数据库的访问,那么我们应该如何处理呢。

在框架的基类AbstractBaseDAL里面,我们对构造数据库的代码进行了封装。

///

/// 根据配置数据库配置名称生成Database对象

///

///

protectedvirtual Database CreateDatabase()

{

Database db = null;

if (string.IsNullOrEmpty(dbConfigName))

{

db = DatabaseFactory.CreateDatabase();

}

else

{

db = DatabaseFactory.CreateDatabase(dbConfigName);

}

return db;

}

而每个DAL层都会继承自AbstractBaseDAL,这样也就可以通过在DAL层指定dbConfigName进行使用不同的数据库的了。

但是这样问题出现了,假如我们有5个不同类型(SqlServer、Oracle、Mysql、Access、Sqlite)的数据库的DAL层,那么每个DAL层的实现类都要写一些代码,这样挺不方便,那么是否可以把它抽象到BLL层,这样只写一次配置就可以了,这个思路很好,我们具体来看看如何实现。

1)在IBaseDAL层定义接口

///

/// 数据访问层的接口

///

publicinterface IBaseDAL where T : BaseEntity

{

///

/// 设置数据库配置项名称

///

///

数据库配置项名称

void SetDbConfigName(string dbConfigName);

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

}

2、在AbstractBaseDAL添加默认实现代码

///

/// 数据访问层的超级基类,所有数据库的数据访问基类都继承自这个超级基类,包括Oracle、SqlServer、Sqlite、MySql、Access等

///

publicabstractclass AbstractBaseDAL where T : BaseEntity, new()

{

///

/// 设置数据库配置项名称

///

///

数据库配置项名称

publicvirtualvoid SetDbConfigName(string dbConfigName)

{

this.dbConfigName = dbConfigName;

}

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

}

3、在BaseBLL里面的Init函数进行调用设置处理

///

/// 参数赋值后,初始化相关对象

///

///

BLL业务类的全名(子类必须实现),子类构造函数传入this.GetType().FullName

///

数据访问层程序集的清单文件的文件名,不包括其扩展名。设置为NULL或默认为Assembly.GetExecutingAssembly().GetName().Name

///

BLL命名空间的前缀(BLL.)

///

数据库配置项名称

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

{

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

baseDal.SetDbConfigName(dbConfigName); //设置数据库配置项名称

}

4、在具体BLL层的业务类进行初始化处理。

///

/// 政策法规公告动态

///

publicclass Information : BaseBLL

{

public Information() : base()

{

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

}

这样设置后,我们具体调用的代码不变,但是指定业务的数据库访问已经使用了特定的配置项名称了,如果配置项不存在,那么还是会获取默认的配置项进行处理了。

通过这样的实现步骤,我们就能实现在一个业务系统里面,分拆不同的数据库,进行统一管理,而且又不会增加额外的调用难度,对于我们很多业务表,这种框架的处理方式 ,应该是不错的。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How does the learning curve of PHP frameworks compare to other language frameworks? How does the learning curve of PHP frameworks compare to other language frameworks? Jun 06, 2024 pm 12:41 PM

The learning curve of a PHP framework depends on language proficiency, framework complexity, documentation quality, and community support. The learning curve of PHP frameworks is higher when compared to Python frameworks and lower when compared to Ruby frameworks. Compared to Java frameworks, PHP frameworks have a moderate learning curve but a shorter time to get started.

How do the lightweight options of PHP frameworks affect application performance? How do the lightweight options of PHP frameworks affect application performance? Jun 06, 2024 am 10:53 AM

The lightweight PHP framework improves application performance through small size and low resource consumption. Its features include: small size, fast startup, low memory usage, improved response speed and throughput, and reduced resource consumption. Practical case: SlimFramework creates REST API, only 500KB, high responsiveness and high throughput

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

PHP Database Connection Pitfalls: Avoid Common Mistakes and Misunderstandings PHP Database Connection Pitfalls: Avoid Common Mistakes and Misunderstandings Jun 05, 2024 pm 10:21 PM

To avoid PHP database connection errors, follow best practices: check for connection errors and match variable names with credentials. Use secure storage or environment variables to avoid hardcoding credentials. Close the connection after use to prevent SQL injection and use prepared statements or bound parameters.

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

See all articles