冷风.NET

    ---默默無聞
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Factory Method来实现数据库操作的类

Posted on 2005-02-26 13:57  冷风.net  阅读(2710)  评论(10编辑  收藏  举报

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

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

using System;
using System.Data;
using System.Data.Odbc;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace DBFactory
{
    
/// <summary>抽象Connection工場類</summary>
    public abstract class ConnectionFactory
    
{
        
protected string connectionString;

        
public ConnectionFactory(string connString)
        
{
            
this.connectionString = connString;
        }


        
public abstract IDbConnection GetConnection();
    }


    
/// <summary>實現SqlConnection對象的具體工作類</summary>
    public class SqlConnection : ConnectionFactory
    
{
        
public SqlConnection() : this(null){}
        
public SqlConnection(string connString) : base(connString){}

        
public override IDbConnection GetConnection()
        
{
            
if(connectionString!=null)
            
{
                
return new System.Data.SqlClient.SqlConnection(connectionString);
            }

            
else
            
{
                
return new System.Data.SqlClient.SqlConnection();
            }

        }

    }


    
/// <summary>實現OleDbConnection對象的具體工場類</summary>
    public class AccessConnection :ConnectionFactory
    
{
        
public AccessConnection() : this(null){}
        
public AccessConnection(string connString) :base(connString){}

        
public override IDbConnection GetConnection()
        
{
            
if(connectionString!=null)
            
{
                
return new OleDbConnection(connectionString);
            }

            
else
            
{
                
return new OleDbConnection();
            }

        }

    }

}


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

using System;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace DBFactory
{
    
/// <summary>抽象CommandFactory類</summary>
    public abstract class CommandFactory
    
{
        
protected string commandText;
        
protected IDbConnection iConn;

        
public CommandFactory(IDbConnection conn,string commText)
        
{
            
this.iConn = conn;
            
this.commandText = commText;
        }


        
public abstract IDbCommand GetCommand();
    }


    
/// <summary>實現SqlCommand對象的具體類</summary>
    public class SqlCommand : CommandFactory
    
{
        
public SqlCommand(IDbConnection conn,string commText) : base(conn,commText){}

        
public override IDbCommand GetCommand()
        
{
            
return new System.Data.SqlClient.SqlCommand(commandText,(System.Data.SqlClient.SqlConnection)iConn);
        }

    }


    
/// <summary>實現OleDbCommand對象的具體類</summary>
    public class AccessCommand : CommandFactory
    
{
        
public AccessCommand(IDbConnection conn,string commText) : base(conn,commText){}

        
public override IDbCommand GetCommand()
        
{
            
return new OleDbCommand(commandText,(OleDbConnection)iConn);
        }

    }

}


三。采用工场模式实现对DataAdapter对象的操作
using System;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace DBFactory
{
    
/// <summary>抽象DataAdapterFactory工場類</summary>
    public abstract class DataAdapterFactory
    
{
        
protected IDbCommand iComm;

        
public DataAdapterFactory(IDbCommand comm)
        
{
            
this.iComm = comm;
        }


        
public abstract DbDataAdapter GetDataAdapter();
    }


    
/// <summary>實現SqlDataAdapter對象的具體類</summary>
    public class SqlDataAdapter : DataAdapterFactory
    
{
        
public SqlDataAdapter(IDbCommand comm) : base(comm){}

        
public override DbDataAdapter GetDataAdapter()
        
{
            
return new System.Data.SqlClient.SqlDataAdapter((System.Data.SqlClient.SqlCommand)iComm);
        }

    }


    
/// <summary>實現OleDbDataAdapter對象的具體類</summary>
    public class AccessDataAdapter : DataAdapterFactory
    
{
        
public AccessDataAdapter(IDbCommand comm) : base(comm){}

        
public override DbDataAdapter GetDataAdapter()
        
{
            
return new OleDbDataAdapter((OleDbCommand)iComm);
        }


    }

}

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

using System;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace DBFactory
{
    
public class SimpleFactory
    
{
        
public SimpleFactory(){}

        
/// <summary>返回抽象的ConnectionFactory工場對象</summary>
        public static ConnectionFactory GetConnFactory(string connString,string dbType)
        
{
            ConnectionFactory factory;
            
switch(dbType.ToUpper())
            
{
                
case "SQL":
                    factory 
= new DBFactory.SqlConnection(connString);
                    
break;
                
case "ACCESS":
                    factory 
= new DBFactory.AccessConnection(connString);
                    
break;
                
default:
                    factory 
= null;
                    
break;
            }

            
return factory;
        }


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

            
return factory;
        }


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

            
return factory;
        }

    }

}


五。封装的操作数据库存的类
using System;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Configuration;

namespace DBFactory
{
    
public class ExecuteDB
    
{
        
private static string connectionString;
        
private static string dbType;

        
public ExecuteDB(){}

        
/// <summary>數據庫連接字符串</summary>
        public static string ConnectionString
        
{
            
get
            
{
                
if(connectionString==null) connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
                
return connectionString;
            }

            
set{connectionString = value;}
        }


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

            
set{dbType=value;}
        }


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

    }

}


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

實現方法:
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";
            
string commString = "select * from tbl_Vip";

            ExecuteDB.ConnectionString 
= connString;
            ExecuteDB.DBType 
= "sql";
            DataGrid1.DataSource
=ExecuteDB.ExcuteSql(commString);
            DataGrid1.DataBind();