Factory Method来实现数据库操作的类 (转) 原文:冷风.NET

今天看了看设计模式中的工场模式,感觉还不错,一时兴起,便将我原来利用简单工场模式写的一个操作数据库的类大至改成了工场模式,算是加深我对工场模式的理解吧。下面来看看实现过程:

一。采用工场模式实现对Connection对象的操作

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Data.Odbc;
None.gif
using  System.Data.OleDb;
None.gif
using  System.Data.SqlClient;
None.gif
None.gif
namespace  DBFactory
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>抽象Connection工場類</summary>
InBlock.gif    public abstract class ConnectionFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected string connectionString;
InBlock.gif
InBlock.gif        
public ConnectionFactory(string connString)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.connectionString = connString;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public abstract IDbConnection GetConnection();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>實現SqlConnection對象的具體工作類</summary>
InBlock.gif    public class SqlConnection : ConnectionFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public SqlConnection() : this(null)dot.gif{}
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public SqlConnection(string connString) : base(connString)dot.gif{}
InBlock.gif
InBlock.gif        
public override IDbConnection GetConnection()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(connectionString!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return new System.Data.SqlClient.SqlConnection(connectionString);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return new System.Data.SqlClient.SqlConnection();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>實現OleDbConnection對象的具體工場類</summary>
InBlock.gif    public class AccessConnection :ConnectionFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public AccessConnection() : this(null)dot.gif{}
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public AccessConnection(string connString) :base(connString)dot.gif{}
InBlock.gif
InBlock.gif        
public override IDbConnection GetConnection()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(connectionString!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return new OleDbConnection(connectionString);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return new OleDbConnection();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

connection.jpg
二。采用工场模式实现对Command对象的操作

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Data.Common;
None.gif
using  System.Data.OleDb;
None.gif
using  System.Data.SqlClient;
None.gif
None.gif
namespace  DBFactory
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>抽象CommandFactory類</summary>
InBlock.gif    public abstract class CommandFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected string commandText;
InBlock.gif        
protected IDbConnection iConn;
InBlock.gif
InBlock.gif        
public CommandFactory(IDbConnection conn,string commText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.iConn = conn;
InBlock.gif            
this.commandText = commText;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public abstract IDbCommand GetCommand();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>實現SqlCommand對象的具體類</summary>
InBlock.gif    public class SqlCommand : CommandFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public SqlCommand(IDbConnection conn,string commText) : base(conn,commText)dot.gif{}
InBlock.gif
InBlock.gif        
public override IDbCommand GetCommand()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new System.Data.SqlClient.SqlCommand(commandText,(System.Data.SqlClient.SqlConnection)iConn);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>實現OleDbCommand對象的具體類</summary>
InBlock.gif    public class AccessCommand : CommandFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public AccessCommand(IDbConnection conn,string commText) : base(conn,commText)dot.gif{}
InBlock.gif
InBlock.gif        
public override IDbCommand GetCommand()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new OleDbCommand(commandText,(OleDbConnection)iConn);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

command.jpg
三。采用工场模式实现对DataAdapter对象的操作
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Data.Common;
None.gif
using  System.Data.OleDb;
None.gif
using  System.Data.SqlClient;
None.gif
None.gif
namespace  DBFactory
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>抽象DataAdapterFactory工場類</summary>
InBlock.gif    public abstract class DataAdapterFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected IDbCommand iComm;
InBlock.gif
InBlock.gif        
public DataAdapterFactory(IDbCommand comm)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.iComm = comm;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public abstract DbDataAdapter GetDataAdapter();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>實現SqlDataAdapter對象的具體類</summary>
InBlock.gif    public class SqlDataAdapter : DataAdapterFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public SqlDataAdapter(IDbCommand comm) : base(comm)dot.gif{}
InBlock.gif
InBlock.gif        
public override DbDataAdapter GetDataAdapter()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new System.Data.SqlClient.SqlDataAdapter((System.Data.SqlClient.SqlCommand)iComm);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>實現OleDbDataAdapter對象的具體類</summary>
InBlock.gif    public class AccessDataAdapter : DataAdapterFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public AccessDataAdapter(IDbCommand comm) : base(comm)dot.gif{}
InBlock.gif
InBlock.gif        
public override DbDataAdapter GetDataAdapter()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new OleDbDataAdapter((OleDbCommand)iComm);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

DataAdapter.jpg

四。这里利用简单工场模式来返回以上的抽象工场对象

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Data.Common;
None.gif
using  System.Data.OleDb;
None.gif
using  System.Data.SqlClient;
None.gif
None.gif
namespace  DBFactory
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class SimpleFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public SimpleFactory()dot.gif{}
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>返回抽象的ConnectionFactory工場對象</summary>
InBlock.gif        public static ConnectionFactory GetConnFactory(string connString,string dbType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ConnectionFactory factory;
InBlock.gif            
switch(dbType.ToUpper())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case "SQL":
InBlock.gif                    factory 
= new DBFactory.SqlConnection(connString);
InBlock.gif                    
break;
InBlock.gif                
case "ACCESS":
InBlock.gif                    factory 
= new DBFactory.AccessConnection(connString);
InBlock.gif                    
break;
InBlock.gif                
default:
InBlock.gif                    factory 
= null;
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return factory;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>返回抽象的CommandFactory工場對象</summary>
InBlock.gif        public static CommandFactory GetCommFactory(IDbConnection conn,string commText,string dbType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CommandFactory factory;
InBlock.gif            
switch(dbType.ToUpper())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case "SQL":
InBlock.gif                    factory 
= new DBFactory.SqlCommand(conn,commText);
InBlock.gif                    
break;
InBlock.gif                
case "ACCESS":
InBlock.gif                    factory 
= new DBFactory.AccessCommand(conn,commText);
InBlock.gif                    
break;
InBlock.gif                
default:
InBlock.gif                    factory 
= null;
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return factory;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>返回抽象的DataAdapterFactory工場對象</summary>
InBlock.gif        public static DataAdapterFactory GetDataAdapterFactory(IDbCommand comm,string dbType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataAdapterFactory factory;
InBlock.gif            
switch(dbType.ToUpper())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case "SQL":
InBlock.gif                    factory 
= new DBFactory.SqlDataAdapter(comm);
InBlock.gif                    
break;
InBlock.gif                
case "ACCESS":
InBlock.gif                    factory 
= new DBFactory.AccessDataAdapter(comm);
InBlock.gif                    
break;
InBlock.gif                
default:
InBlock.gif                    factory 
= null;
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return factory;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
simplefactory.jpg
五。封装的操作数据库存的类
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Data.Common;
None.gif
using  System.Data.OleDb;
None.gif
using  System.Data.SqlClient;
None.gif
using  System.Configuration;
None.gif
None.gif
namespace  DBFactory
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class ExecuteDB
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private static string connectionString;
InBlock.gif        
private static string dbType;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public ExecuteDB()dot.gif{}
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>數據庫連接字符串</summary>
InBlock.gif        public static string ConnectionString
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(connectionString==null) connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
InBlock.gif                
return connectionString;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{connectionString = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>數據庫類型</summary>
InBlock.gif        public static string DBType
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(dbType==null) dbType = ConfigurationSettings.AppSettings["DataBaseType"];
InBlock.gif                
return dbType;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{dbType=value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>執行SQL語句返回DataSet</summary>
InBlock.gif        public static DataSet ExcuteSql(string sqlString)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet ds 
= new DataSet();
InBlock.gif            ConnectionFactory objConn 
= SimpleFactory.GetConnFactory(ConnectionString,DBType);
InBlock.gif            IDbConnection iConn 
= objConn.GetConnection();
InBlock.gif            iConn.Open();
InBlock.gif            CommandFactory objComm 
= SimpleFactory.GetCommFactory(iConn,sqlString,DBType);
InBlock.gif            IDbCommand iComm 
= objComm.GetCommand();
InBlock.gif            DataAdapterFactory objAdapter 
= SimpleFactory.GetDataAdapterFactory(iComm,DBType);
InBlock.gif            DbDataAdapter dataAdaper 
= objAdapter.GetDataAdapter();
InBlock.gif            dataAdaper.Fill(ds);
InBlock.gif            iComm.Dispose();
InBlock.gif            iConn.Close();
InBlock.gif            iConn.Dispose();
InBlock.gif            
return ds;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

上面對具體的數據庫的選擇采用的是 簡單工場模式來實現的(因工場模式中的具體類只能實現具體的對象,感覺不好實現)

實現方法:
None.gif string  connString  =   " data source=192.168.1.9;initial catalog=sqldll;persist security info=False;user id=sa;password=123456;workstation id=Server;packet size=4096 " ;
None.gif            
string  commString  =   " select * from tbl_Vip " ;
None.gif
None.gif            ExecuteDB.ConnectionString 
=  connString;
None.gif            ExecuteDB.DBType 
=   " sql " ;
None.gif            DataGrid1.DataSource
= ExecuteDB.ExcuteSql(commString);
None.gif            DataGrid1.DataBind();

原文: http://www.cnblogs.com/helimin19/archive/2005/06/23/109535.html#179803

转载于:https://www.cnblogs.com/dagon007/archive/2005/07/18/195102.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值