抽象工厂模式:实现ASP.NET访问不同数据库
?
1、抽象工廠模式(Abstract Factory):提供一個(gè)創(chuàng)建一系列相互依賴對(duì)象的接 口,而無(wú)需指定他們的具體類。
2、一般組成:抽象 產(chǎn)品接口,具體產(chǎn)品類,抽象工廠接口,具體工廠類,客戶端代碼
3、作用:易于交換 產(chǎn)品系列,在一個(gè)應(yīng)用中只需在初始化的時(shí)候出現(xiàn)一次,這就使得改變一個(gè)應(yīng)用的具體工廠的具體工廠變得非常容易,只需改變具體工廠即可使用不同的產(chǎn)品配置
讓具體的創(chuàng)建實(shí)例過(guò)程與客戶端分離,客戶端是通過(guò)他們的抽象 類接口操縱實(shí)例,產(chǎn)品的具體類名也被具體工廠是實(shí)現(xiàn)分離,不會(huì)出現(xiàn)在客戶代碼中。
?
?
網(wǎng)上提供的代碼基本是為了講解說(shuō) 明C#設(shè)計(jì)模式,由C#控制臺(tái)程序代碼實(shí)現(xiàn),并沒(méi)有真正實(shí)現(xiàn)訪問(wèn)數(shù)據(jù)庫(kù)。我這里通過(guò)建立一個(gè)ASP.NET網(wǎng)站,實(shí)現(xiàn)訪問(wèn)Access與SQL Server數(shù)據(jù)庫(kù)。主要參考《大話設(shè)計(jì)模式》
具體步驟代碼如下:
1、?分別在Access和SQL Server建立數(shù)據(jù)庫(kù)DaHua與數(shù)據(jù)庫(kù)表User(ID,Name),Department(ID,DepartName)
2、?啟動(dòng)Visual Studio 2005/2008新建網(wǎng)站
3、?到web.config添加數(shù)據(jù)庫(kù)連接
4、?在網(wǎng)站解決方案右鍵新建類庫(kù)ClassLibrary1,新建類文件,包 括:
AbstractUser抽象類,SqlserverUser類,AccessUser類
AbstractDepartment抽象類,SqlserverDepartment類,AccessDepartment 類
DataAccess類
生成dll
5、到項(xiàng)目添加引用,項(xiàng)目-右鍵-添加引用-項(xiàng)目-ClassLibrary1
6、前臺(tái)代碼:添加一個(gè)GridView控件,三個(gè)Button控件,2個(gè)TextBox控件
用于查詢表User和Department表內(nèi)容,插入U(xiǎn)ser表數(shù)據(jù)
?
?
using?System;
using?System.Configuration;
using?System.Data;
using?System.Linq;
using?System.Web;
using?System.Xml.Linq;
using?System.Data.SqlClient;
using?System.Data.OleDb;
using?System.Reflection;
?
namespace?ClassLibrary1
{
????///?<summary>
????///AbstractUser抽象類
????///?</summary>
????public?abstract?class?AbstractUser
????{
????????public?abstract?DataSet?GetUser();
????????public?abstract?void?InsertUser(string?userID,?string?userName);
????}
?
????///?<summary>
????///?SqlserverUser類
????///?</summary>
????public?class?SqlserverUser?:?AbstractUser
????{
????????string?constr?=?ConfigurationManager.ConnectionStrings["constrSQLDahua"].ConnectionString;
?
????????public?override?DataSet?GetUser()
????????{
????????????SqlConnection?conn?=?new?SqlConnection(constr);
????????????if?(conn.State?==?ConnectionState.Closed)
????????????????conn.Open();
?
????????????SqlCommand?cmd?=?new?SqlCommand("select?*?from?[User]",?conn);
????????????SqlDataAdapter?sda?=?new?SqlDataAdapter();
????????????sda.SelectCommand?=?cmd;
????????????DataSet?ds?=?new?DataSet();
????????????sda.Fill(ds);
????????????return?ds;
????????}
?
????????public?override?void?InsertUser(string?userID,?string?userName)
????????{
????????????SqlConnection?conn?=?new?SqlConnection(constr);
????????????if?(conn.State?==?ConnectionState.Closed)
????????????????conn.Open();
????????????SqlCommand?cmd?=?new?SqlCommand();
????????????cmd.Connection?=?conn;
????????????cmd.CommandText?=?"insert?into?[User]?values('"?+?Int32.Parse(userID)?+?"','"?+?userName?+?"')?";
????????????cmd.ExecuteNonQuery();
????????????conn.Close();
?
????????}
????}
?
????///?<summary>
????///?AccessUser類
????///?</summary>
????public?class?AccessUser?:?AbstractUser
????{
????????string?constr?=?ConfigurationManager.ConnectionStrings["constrAccessDaHua"].ConnectionString;
?
????????public?override?DataSet?GetUser()
????????{
????????????OleDbConnection?conn?=?new?OleDbConnection(constr);
????????????if?(conn.State?==?ConnectionState.Closed)
????????????????conn.Open();
?
????????????OleDbCommand?cmd?=?new?OleDbCommand("select?*?from?[User]",?conn);
????????????OleDbDataAdapter?sda?=?new?OleDbDataAdapter();
????????????sda.SelectCommand?=?cmd;
????????????DataSet?ds?=?new?DataSet();
????????????sda.Fill(ds);
????????????return?ds;
????????}
?
????????public?override?void?InsertUser(string?userID,?string?userName)
????????{
????????????OleDbConnection?conn?=?new?OleDbConnection(constr);
????????????if?(conn.State?==?ConnectionState.Closed)
????????????????conn.Open();
????????????OleDbCommand?cmd?=?new?OleDbCommand();
????????????cmd.Connection?=?conn;
????????????cmd.CommandText?=?"insert?into?[User]?values('"?+?Int32.Parse(userID)?+?"','"?+?userName?+?"')?";
????????????cmd.ExecuteNonQuery();
????????????conn.Close();
????????}
}
?
///?<summary>
????///AbstractDepartment抽象類
????///?</summary>
????public?abstract?class?AbstractDepartment
????{
????????public?abstract?DataSet?GetDepartment();
????????//?void?InsertDepartment(string?depID,?string?depName);
????}
?
????///?<summary>
????///?SqlserverDepartment類
????///?</summary>
????public?class?SqlserverDepartment?:?AbstractDepartment
????{
????????string?constr?=?ConfigurationManager.ConnectionStrings["constrSQLDahua"].ConnectionString;
?
????????public?override?DataSet?GetDepartment()
????????{
????????????SqlConnection?conn?=?new?SqlConnection(constr);
????????????if?(conn.State?==?ConnectionState.Closed)
????????????????conn.Open();
?
????????????SqlCommand?cmd?=?new?SqlCommand("select?*?from?[Department]",?conn);
????????????SqlDataAdapter?sda?=?new?SqlDataAdapter();
????????????sda.SelectCommand?=?cmd;
????????????DataSet?ds?=?new?DataSet();
????????????sda.Fill(ds);
????????????return?ds;
?
????????}
????}
?
????///?<summary>
????///?AccessDepartment?類
????///?</summary>
????public?class?AccessDepartment?:?AbstractDepartment
????{
????????string?constr?=?ConfigurationManager.ConnectionStrings["constrAccessDaHua"].ConnectionString;
?
????????public?override?DataSet?GetDepartment()
????????{
????????????OleDbConnection?conn?=?new?OleDbConnection(constr);
????????????if?(conn.State?==?ConnectionState.Closed)
????????????????conn.Open();
?
????????????OleDbCommand?cmd?=?new?OleDbCommand("select?*?from?[Department]",?conn);
????????????OleDbDataAdapter?sda?=?new?OleDbDataAdapter();
????????????sda.SelectCommand?=?cmd;
????????????DataSet?ds?=?new?DataSet();
????????????sda.Fill(ds);
????????????return?ds;
????????}
?}
?
///?<summary>
????///DataAccess類
????///使用反射
????///?</summary>
????public?class?DataAccess
????{
????????private?static?readonly?string?AssemblyName?=?"ClassLibrary1";?//程序集名稱
????????private?static?readonly?string?db?=?"Sqlserver";?//數(shù)據(jù)庫(kù)類型名,要更改訪問(wèn)的數(shù)據(jù)?庫(kù),只需更改這里
?
????????public?static?AbstractUser?CreateUser()
????????{
????????????string?className?=?AssemblyName?+?"."?+?db?+?"User";?//命名空間.要實(shí)例化的類名
????????????return?(AbstractUser)Assembly.Load(AssemblyName).CreateInstance(className);
????????}
?
????????public?static?AbstractDepartment?CreateDepartment()
????????{
????????????string?className?=?AssemblyName?+?"."?+?db?+?"Department";
????????????return?(AbstractDepartment)Assembly.Load(AssemblyName).CreateInstance(className);
????????}
?}
}
?
前臺(tái)代碼:
public?partial?class?_Default?:?System.Web.UI.Page?
{
????ClassLibrary1.AbstractUser?au?=?null;
????ClassLibrary1.AbstractDepartment?ad?=?null;
?
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????????
???????
????}
????protected?void?Button1_Click(object?sender,?EventArgs?e)
????{
????????au?=ClassLibrary1.DataAccess.CreateUser();
????????DataSet?ds?=?au.GetUser();
????????GridView1.DataSource?=?ds;
????????GridView1.DataBind();
?
????}
????protected?void?Button2_Click(object?sender,?EventArgs?e)
????{????????
????????string?userID?=?txtID.Text;
????????string?userName?=?txtName.Text?;
????????au?=?ClassLibrary1.DataAccess.CreateUser();
????????au.InsertUser(userID,userName);
????}
?
????//部門查詢
????protected?void?Button3_Click(object?sender,?EventArgs?e)
????{
????????ad?=?ClassLibrary1.DataAccess.CreateDepartment();
????????DataSet?ds?=?ad.GetDepartment();
????????GridView1.DataSource?=?ds;
????????GridView1.DataBind();
????}
}
?
轉(zhuǎn)自:http://blog.csdn.net/canhui87/archive/2009/10/20/4704781.aspx
總結(jié)
以上是生活随笔為你收集整理的抽象工厂模式:实现ASP.NET访问不同数据库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 如何利用抽象工厂更换数据库
- 下一篇: Java之美之设计模式