大家帮忙分析一下接口继承的最佳的方案! ( 积分: 100 )

  • 主题发起人 主题发起人 china_ttw
  • 开始时间 开始时间
C

china_ttw

Unregistered / Unconfirmed
GUEST, unregistred user!
大家帮忙分析一下接口继承的最佳的方案!
大至的UML关系图如上:(强烈要求CSDN充许进行贴图)
DBConn ------> (interface)IDBConn
> |
| |
| |
MsSQLConn ODBCConn OLEConn |
|
|
DBConnFoctory <------ AbsBasicExecSQL
|
|
|
BasicExecSQL
大至的思路如下:
首先定义一个接口:IDBConn
using System;
using System.Data;
namespace DBConn
{
public interface IDBConn : IDisposable
{
IDbConnection DataBaseConnection
{get;set;}
string GetConnStr
{get;set;}
bool GetConnState();
void OpenDBCon();
void CloseDBCon();
void begin
Tran();
void CommitTran();
void RollbackTran();
}
}
然后定义一个实现IDBConn接口的虚函数类
在该类中主要实现数据库连接的基本操作
注意该处不涉及实际的数据库连接方式
using System;
using System.Data;
namespace DBConn
{
public abstract class DBConn: IDBConn
{
private IDbConnection _DbConn = null;//数据库连接对象
private IDbTransaction _DBTran = null;//数据库事务处理对象
private int _ConnCount = 0;
//连接计数器
private int _DbTransactionCount = 0;
//事务计数器
private IsolationLevel _DbTranLevel = IsolationLevel.ReadCommitted;//数据库事务处理级别;
private string _DBTranLabel = "Application" + DateTime.Now.Ticks.ToString();//当前执行的数据库事务的实例名称;
protected internal abstract IDbDataAdapter InitDataAdapter(IDbCommand DbCommand);//初始化一个DataAdapter
protected internal abstract IDbDataParameter InitDataParamter(int dbType , int paramDirection , int paramSize , string paramName , string paramValue);
//初始化参数

public virtual IDbConnection DataBaseConnection //数据库连接属性
{
get{return _DbConn;}
set{_DbConn = value;}
}
public string GetConnStr //数据库连接字符串属性
{
get{return DataBaseConnection.ConnectionString;}
set{DataBaseConnection.ConnectionString = value;}
}
public virtual bool GetConnState() //数据库连接状态
{
return DataBaseConnection.State == ConnectionState.Open;
}

public virtual void OpenDBCon() //用计数器打开数据库连接
{
if (!GetConnState())
{
DataBaseConnection.Open();
}
_ConnCount ++;
}

public virtual void CloseDBCon()//关闭数据库连接
{
_ConnCount--;
if (_ConnCount == 0)
if (GetConnState())
DataBaseConnection.Close();
}

public virtual void begin
Tran() //开始事务
{
if (_DBTran == null)
{
_DBTran = DataBaseConnection.begin
Transaction((_DbTranLevel));
}
_DbTransactionCount++;
}

public virtual void CommitTran() //提交事务
{
_DbTransactionCount--;
if (_DbTransactionCount == 0)
{
if ((_DBTran != null) &amp;&amp;
(_DBTran.Connection != null))
_DBTran.Commit();
}
}

public virtual void RollbackTran()//回滚事务
{
if ((_DBTran != null) &amp;&amp;
(_DBTran.Connection != null))
{
_DBTran .Rollback();
_DbTransactionCount = 0;
}
}

protected internal virtual IDbCommand InitSqlCommand(string sqlstr , IDbConnection sqlconn , IDbDataParameter[] paramList)//初始化DBCommand
{
IDbCommand sqlcmd = sqlconn.CreateCommand();
sqlcmd.CommandType = CommandType.Text;
if (paramList != null)
{
sqlcmd.CommandType = CommandType.StoredProcedure;
foreach(IDbDataParameter sp in paramList)
sqlcmd.Parameters.Add(sp);
}
sqlcmd.CommandText = sqlstr;
if (_DBTran != null)
sqlcmd.Transaction = _DBTran;
return sqlcmd;
}
protected internal virtual IDbDataParameter SetParameterDirectionAndValue(IDbDataParameter Parameter , int ParamDirection , object ParamValue) //设置一下参数的传递方向及参数值
{
switch(ParamDirection)
{
case 1 :
Parameter.Direction = ParameterDirection.Input;
break;
case 2:
Parameter.Direction = ParameterDirection.InputOutput;
break;
case 3 :
Parameter.Direction = ParameterDirection.Output;
break;
case 4 :
Parameter.Direction = ParameterDirection.ReturnValue;
break;
default:
Parameter.Direction = ParameterDirection.Input;
break;
}
Parameter.Value = ParamValue;
return Parameter;
}
public virtual void Dispose()
{
if (DataBaseConnection != null)
DataBaseConnection.Dispose();
if (_DBTran != null)
_DBTran.Dispose();
}
}
}
实现一个针对MS-SQLSERVER连接的操作的特殊类:
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DBConn
{
public sealed class MsSQLConn : DBConn
{
public MsSQLConn(string DBConnStr)
{
DataBaseConnection = new SqlConnection();
DataBaseConnection.ConnectionString = DBConnStr;
}
protected internal override IDbDataAdapter InitDataAdapter(IDbCommand DbCommand)
{
return new SqlDataAdapter(DbCommand as SqlCommand);
}
protected internal override IDbDataParameter InitDataParamter(int dbType , int paramDirection , int paramSize , string paramName , string paramValue)
{
SqlDbType dbtype = SqlDbType.VarChar;
switch (dbType)
{
case 1 :
dbtype = SqlDbType.VarChar;
break;
case 2 :
dbtype = SqlDbType.Int;
break;
case 3 :
dbtype = SqlDbType.DateTime;
break;
case 4 :
dbtype = SqlDbType.Decimal;
break;
case 5 :
dbtype = SqlDbType.Bit;
break;
}
return SetParameterDirectionAndValue(new SqlParameter(paramName , dbtype , paramSize),paramDirection,paramValue);
}
}
}
用实现MS-SQLSER的方式实现另外几种连接方式(包括ODBCConn OLEConn),在此一并贴出代码
/*ODBCConn*/
using System;
using System.Data;
using System.Data.Odbc;
namespace DBConn
{
public sealed class ODBCConn : DBConn
{
public ODBCConn(string DBConnStr)
{
DataBaseConnection = new OdbcConnection();
DataBaseConnection.ConnectionString = DBConnStr;
}
protected internal override IDbDataAdapter InitDataAdapter(IDbCommand DbCommand)
{
return new OdbcDataAdapter(DbCommand as OdbcCommand);
}
protected internal override IDbDataParameter InitDataParamter(int dbType , int paramDirection , int paramSize , string paramName , string paramValue)
{
OdbcType dbtype = OdbcType.VarChar;
switch (dbType)
{
case 1 :
dbtype = OdbcType.VarChar;
break;
case 2 :
dbtype = OdbcType.Int;
break;
case 3 :
dbtype = OdbcType.Date;
break;
case 4 :
dbtype = OdbcType.Decimal;
break;
case 5 :
dbtype = OdbcType.Bit;
break;
}
return SetParameterDirectionAndValue(new OdbcParameter(paramName , dbtype , paramSize),paramDirection,paramValue);
}
}
}
/*OLEConn*/
using System;
using System.Data.OleDb;
using System.Data;
namespace DBConn
{
public sealed class OLEConn : DBConn
{
public OLEConn(string DBConnStr)
{
DataBaseConnection = new OleDbConnection();
DataBaseConnection.ConnectionString = DBConnStr;
}
protected internal override IDbDataAdapter InitDataAdapter(IDbCommand DbCommand)
{
return new OleDbDataAdapter(DbCommand as OleDbCommand);
}
protected internal override IDbDataParameter InitDataParamter(int dbType , int paramDirection , int paramSize , string paramName , string paramValue)
{
OleDbType dbtype = OleDbType.VarChar;
switch (dbType)
{
case 1 :
dbtype = OleDbType.VarChar;
break;
case 2 :
dbtype = OleDbType.Integer;
break;
case 3 :
dbtype = OleDbType.Date;
break;
case 4 :
dbtype = OleDbType.Decimal;
break;
case 5 :
dbtype = OleDbType.Boolean;
break;
}
return SetParameterDirectionAndValue(new OleDbParameter(paramName , dbtype , paramSize),paramDirection,paramValue);
}
}
}
到此为止整个程序用最少的代码最实现了各种数据库的连接
下面就通过一个工厂模式实现数据库连接的创建,该工厂只创建一个数据库的连接实例
using System;
namespace DBConn
{
public class DBConnFoctory
{
public enum DBConnType{MsSQLConn,ODBCConn,OLEConn,OracleConn};
private DBConn _DBConn;
protected internal DBConn DBConnClass
{
get
{
return _DBConn;
}
}
public DBConnFoctory(string DBConnStr):this(DBConnType.MsSQLConn,DBConnStr)
{
}
public DBConnFoctory(DBConnType ConnType , string DBConnStr)
{
switch(ConnType)
{
case DBConnType.MsSQLConn:
_DBConn = new MsSQLConn(DBConnStr);
break;
case DBConnType.ODBCConn:
_DBConn = new ODBCConn(DBConnStr);
break;
case DBConnType.OLEConn:
_DBConn = new OLEConn(DBConnStr);
break;
case DBConnType.OracleConn:
_DBConn = new OracleConn(DBConnStr);
break;
}
}
}
}
然后实现一个纯虚函数,
这儿本来是通过一个接口的来实现的,但后来改成了纯虚类
用户就通过这个类对数据库连接的所有操作进行访问
using System;
using System.Data;
namespace DBConn
{
public abstract class AbsBasicExecSQL:DBConnFoctory,IDBConn
{
public AbsBasicExecSQL(string DBConnStr):base(DBConnStr)
{
}
public AbsBasicExecSQL(DBConnType ConnType , string DBConnStr):base(ConnType, DBConnStr)
{
}
#region 执行基本SQL语句接口
public abstract DataTable ExecToDataTable(string sqlstr);
public abstract DataTable ExecToDataTable(string sqlstr, DataSet dataset);
public abstract DataRow ExecToDataRow(string sqlstr);
public abstract IDataReader ExecToDataReader(string sqlstr);
public abstract object ExecToScalar(string sqlstr);
public abstract int ExecToNoQuery(string sqlstr);
#endregion
#region 执行存储过程接口
public abstract void GetParamValue(IDbCommand command , IDbDataParameter[] paramList);
public abstract DataTable ExecToDataTable(string sqlstr , IDbDataParameter[] paramList);
public abstract DataTable ExecToDataTable(string sqlstr, DataSet dataset , IDbDataParameter[] paramList);
public abstract IDataReader ExecToDataReader(string sqlstr , IDbDataParameter[] paramList);
public abstract object ExecToScalar(string sqlstr , IDbDataParameter[] paramList);
public abstract int ExecToNoQuery(string sqlstr , IDbDataParameter[] paramList);
#endregion
#region IDBConn 成员
public IDbConnection DataBaseConnection
{
get
{
return DBConnClass.DataBaseConnection;
}
set
{
DBConnClass.DataBaseConnection = value;
}
}
public string GetConnStr
{
get
{
return DBConnClass.GetConnStr;
}
set
{
DBConnClass.GetConnStr = value;
}
}
public bool GetConnState()
{
return DBConnClass.GetConnState();
}
public void OpenDBCon()
{
DBConnClass.OpenDBCon();
}
public void CloseDBCon()
{
DBConnClass.CloseDBCon();
}
public void begin
Tran()
{
DBConnClass.begin
Tran();
}
public void CommitTran()
{
DBConnClass.CommitTran();
}
public void RollbackTran()
{
DBConnClass.RollbackTran();
}
#endregion
#region IDisposable 成员
public void Dispose()
{
DBConnClass.Dispose();
}
#endregion
}
}
然后从AbsBasicExecSQL派生一个类,实现AbsBasicExecSQL类所定义的所有纯虚函数
using System;
using System.Data;
namespace DBConn
{
public class BasicExecSQL : AbsBasicExecSQL
{
public BasicExecSQL(string DBConnStr):base(DBConnStr)
{
}
public BasicExecSQL(DBConnType ConnType , string DBConnStr):base(ConnType,DBConnStr)
{
}
#region 执行已经构造好的IDbCommand
protected virtual DataTable ExecToDataTable(IDbCommand dbcommand , DataSet dataset)
{
if (dataset == null) dataset = new DataSet();
DBConnClass.InitDataAdapter(dbcommand).Fill(dataset);
return dataset.Tables[0];
}
protected virtual IDataReader ExecToDataReader(IDbCommand dbcommand)
{
DBConnClass.OpenDBCon();
IDataReader dr = dbcommand.ExecuteReader();
DBConnClass.CloseDBCon();
return dr;
}
protected virtual object ExecToScalar(IDbCommand dbcommand)
{
return dbcommand.ExecuteScalar();
}
protected virtual int ExecToNoQuery(IDbCommand dbcommand)
{
return dbcommand.ExecuteNonQuery();
}
#endregion
#region 执行一个SQL字符串
public override DataTable ExecToDataTable(string sqlstr)
{
using (DataSet ds = new DataSet())
{
return ExecToDataTable(sqlstr,ds);
}
}
public override DataTable ExecToDataTable(string sqlstr, DataSet dataset)
{
return ExecToDataTable(sqlstr , dataset , null);
}
public override DataRow ExecToDataRow(string sqlstr)
{
DataTable dt = ExecToDataTable(sqlstr);
return dt.Rows.Count> 0 ? dt.Rows[0] : null;
}
public override IDataReader ExecToDataReader(string sqlstr)
{
return ExecToDataReader(sqlstr , null);
}
public override object ExecToScalar(string sqlstr)
{
return ExecToScalar(sqlstr, null);
}
public override int ExecToNoQuery(string sqlstr)
{
return ExecToNoQuery(sqlstr,null);
}
#endregion
#region 执行一个带参数的存储过程
public override void GetParamValue(IDbCommand command , IDbDataParameter[] paramList)
{
if (paramList == null) return;
foreach(IDbDataParameter idp in paramList)
{
if (idp.Direction != ParameterDirection.Input)
idp.Value = (command.Parameters[idp.ParameterName] as IDbDataParameter).Value;;
}
}
public override DataTable ExecToDataTable(string sqlstr , IDbDataParameter[] paramList)
{
return ExecToDataTable(sqlstr,null,paramList);
}
public override DataTable ExecToDataTable(string sqlstr, DataSet dataset , IDbDataParameter[] paramList)
{
IDbCommand command = DBConnClass.InitSqlCommand(sqlstr , DBConnClass.DataBaseConnection , paramList);
DataTable dt = ExecToDataTable(command , dataset);
GetParamValue(command , paramList);
return dt;
}
public override IDataReader ExecToDataReader(string sqlstr , IDbDataParameter[] paramList)
{
IDbCommand command = DBConnClass.InitSqlCommand(sqlstr , DBConnClass.DataBaseConnection , paramList);
IDataReader ida = ExecToDataReader(command);
GetParamValue(command , paramList);
return ida;
}
public override object ExecToScalar(string sqlstr , IDbDataParameter[] paramList)
{
IDbCommand command = DBConnClass.InitSqlCommand(sqlstr , DBConnClass.DataBaseConnection , paramList);
object o = ExecToScalar(command);
GetParamValue(command , paramList);
return o;
}
public override int ExecToNoQuery(string sqlstr , IDbDataParameter[] paramList)
{
IDbCommand command = DBConnClass.InitSqlCommand(sqlstr , DBConnClass.DataBaseConnection , paramList);
int i = ExecToNoQuery(command);
GetParamValue(command , paramList);
return i;
}
#endregion
}
}
所有代码到此为止就贴完了,问题也就出来了,
就AbsBasicExecSQL类而言
不管是一个类还是一个接口,该不会只让用户执行
public abstract DataTable ExecToDataTable(string sqlstr);
public abstract DataTable ExecToDataTable(string sqlstr, DataSet dataset);
public abstract DataRow ExecToDataRow(string sqlstr);
public abstract IDataReader ExecToDataReader(string sqlstr);
public abstract object ExecToScalar(string sqlstr);
public abstract int ExecToNoQuery(string sqlstr);
这几种操作吧,用户在执行这几种操作的同时肯定还需要对数据库连接进行其它处理,
比如打开,关闭数据库连接,执行数据库事务等等操作(这些操作都在IDBConn接口中进行约定过)
所以该类在从DBConnFoctory类派生的同时也实现了IDBConn接口
但该接口实现的代码就显得非常冗余,差不多完全是在调用DBConnClass类的操作;
用现有的方式确实实现了!
但代码显得太过于冗余,该类在实现IDBConn接口的时候完全是在照搬DBConnClass类的实现结果
如果现在将IDBConn接口的定义作一小改动,AbsBasicExecSQL类的调用过程也要作相应更改,很烦!
现在的问题就是有没有办法让用户在通过AbsBasicExecSQL类进行操作(这儿是指该类定义的虚函数)的同时
用更好的方式把IDBConn接口实现的操作包含进来?
 
大家帮忙分析一下接口继承的最佳的方案!
大至的UML关系图如上:(强烈要求CSDN充许进行贴图)
DBConn ------> (interface)IDBConn
> |
| |
| |
MsSQLConn ODBCConn OLEConn |
|
|
DBConnFoctory <------ AbsBasicExecSQL
|
|
|
BasicExecSQL
大至的思路如下:
首先定义一个接口:IDBConn
using System;
using System.Data;
namespace DBConn
{
public interface IDBConn : IDisposable
{
IDbConnection DataBaseConnection
{get;set;}
string GetConnStr
{get;set;}
bool GetConnState();
void OpenDBCon();
void CloseDBCon();
void begin
Tran();
void CommitTran();
void RollbackTran();
}
}
然后定义一个实现IDBConn接口的虚函数类
在该类中主要实现数据库连接的基本操作
注意该处不涉及实际的数据库连接方式
using System;
using System.Data;
namespace DBConn
{
public abstract class DBConn: IDBConn
{
private IDbConnection _DbConn = null;//数据库连接对象
private IDbTransaction _DBTran = null;//数据库事务处理对象
private int _ConnCount = 0;
//连接计数器
private int _DbTransactionCount = 0;
//事务计数器
private IsolationLevel _DbTranLevel = IsolationLevel.ReadCommitted;//数据库事务处理级别;
private string _DBTranLabel = "Application" + DateTime.Now.Ticks.ToString();//当前执行的数据库事务的实例名称;
protected internal abstract IDbDataAdapter InitDataAdapter(IDbCommand DbCommand);//初始化一个DataAdapter
protected internal abstract IDbDataParameter InitDataParamter(int dbType , int paramDirection , int paramSize , string paramName , string paramValue);
//初始化参数

public virtual IDbConnection DataBaseConnection //数据库连接属性
{
get{return _DbConn;}
set{_DbConn = value;}
}
public string GetConnStr //数据库连接字符串属性
{
get{return DataBaseConnection.ConnectionString;}
set{DataBaseConnection.ConnectionString = value;}
}
public virtual bool GetConnState() //数据库连接状态
{
return DataBaseConnection.State == ConnectionState.Open;
}

public virtual void OpenDBCon() //用计数器打开数据库连接
{
if (!GetConnState())
{
DataBaseConnection.Open();
}
_ConnCount ++;
}

public virtual void CloseDBCon()//关闭数据库连接
{
_ConnCount--;
if (_ConnCount == 0)
if (GetConnState())
DataBaseConnection.Close();
}

public virtual void begin
Tran() //开始事务
{
if (_DBTran == null)
{
_DBTran = DataBaseConnection.begin
Transaction((_DbTranLevel));
}
_DbTransactionCount++;
}

public virtual void CommitTran() //提交事务
{
_DbTransactionCount--;
if (_DbTransactionCount == 0)
{
if ((_DBTran != null) &amp;&amp;
(_DBTran.Connection != null))
_DBTran.Commit();
}
}

public virtual void RollbackTran()//回滚事务
{
if ((_DBTran != null) &amp;&amp;
(_DBTran.Connection != null))
{
_DBTran .Rollback();
_DbTransactionCount = 0;
}
}

protected internal virtual IDbCommand InitSqlCommand(string sqlstr , IDbConnection sqlconn , IDbDataParameter[] paramList)//初始化DBCommand
{
IDbCommand sqlcmd = sqlconn.CreateCommand();
sqlcmd.CommandType = CommandType.Text;
if (paramList != null)
{
sqlcmd.CommandType = CommandType.StoredProcedure;
foreach(IDbDataParameter sp in paramList)
sqlcmd.Parameters.Add(sp);
}
sqlcmd.CommandText = sqlstr;
if (_DBTran != null)
sqlcmd.Transaction = _DBTran;
return sqlcmd;
}
protected internal virtual IDbDataParameter SetParameterDirectionAndValue(IDbDataParameter Parameter , int ParamDirection , object ParamValue) //设置一下参数的传递方向及参数值
{
switch(ParamDirection)
{
case 1 :
Parameter.Direction = ParameterDirection.Input;
break;
case 2:
Parameter.Direction = ParameterDirection.InputOutput;
break;
case 3 :
Parameter.Direction = ParameterDirection.Output;
break;
case 4 :
Parameter.Direction = ParameterDirection.ReturnValue;
break;
default:
Parameter.Direction = ParameterDirection.Input;
break;
}
Parameter.Value = ParamValue;
return Parameter;
}
public virtual void Dispose()
{
if (DataBaseConnection != null)
DataBaseConnection.Dispose();
if (_DBTran != null)
_DBTran.Dispose();
}
}
}
实现一个针对MS-SQLSERVER连接的操作的特殊类:
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DBConn
{
public sealed class MsSQLConn : DBConn
{
public MsSQLConn(string DBConnStr)
{
DataBaseConnection = new SqlConnection();
DataBaseConnection.ConnectionString = DBConnStr;
}
protected internal override IDbDataAdapter InitDataAdapter(IDbCommand DbCommand)
{
return new SqlDataAdapter(DbCommand as SqlCommand);
}
protected internal override IDbDataParameter InitDataParamter(int dbType , int paramDirection , int paramSize , string paramName , string paramValue)
{
SqlDbType dbtype = SqlDbType.VarChar;
switch (dbType)
{
case 1 :
dbtype = SqlDbType.VarChar;
break;
case 2 :
dbtype = SqlDbType.Int;
break;
case 3 :
dbtype = SqlDbType.DateTime;
break;
case 4 :
dbtype = SqlDbType.Decimal;
break;
case 5 :
dbtype = SqlDbType.Bit;
break;
}
return SetParameterDirectionAndValue(new SqlParameter(paramName , dbtype , paramSize),paramDirection,paramValue);
}
}
}
用实现MS-SQLSER的方式实现另外几种连接方式(包括ODBCConn OLEConn),在此一并贴出代码
/*ODBCConn*/
using System;
using System.Data;
using System.Data.Odbc;
namespace DBConn
{
public sealed class ODBCConn : DBConn
{
public ODBCConn(string DBConnStr)
{
DataBaseConnection = new OdbcConnection();
DataBaseConnection.ConnectionString = DBConnStr;
}
protected internal override IDbDataAdapter InitDataAdapter(IDbCommand DbCommand)
{
return new OdbcDataAdapter(DbCommand as OdbcCommand);
}
protected internal override IDbDataParameter InitDataParamter(int dbType , int paramDirection , int paramSize , string paramName , string paramValue)
{
OdbcType dbtype = OdbcType.VarChar;
switch (dbType)
{
case 1 :
dbtype = OdbcType.VarChar;
break;
case 2 :
dbtype = OdbcType.Int;
break;
case 3 :
dbtype = OdbcType.Date;
break;
case 4 :
dbtype = OdbcType.Decimal;
break;
case 5 :
dbtype = OdbcType.Bit;
break;
}
return SetParameterDirectionAndValue(new OdbcParameter(paramName , dbtype , paramSize),paramDirection,paramValue);
}
}
}
/*OLEConn*/
using System;
using System.Data.OleDb;
using System.Data;
namespace DBConn
{
public sealed class OLEConn : DBConn
{
public OLEConn(string DBConnStr)
{
DataBaseConnection = new OleDbConnection();
DataBaseConnection.ConnectionString = DBConnStr;
}
protected internal override IDbDataAdapter InitDataAdapter(IDbCommand DbCommand)
{
return new OleDbDataAdapter(DbCommand as OleDbCommand);
}
protected internal override IDbDataParameter InitDataParamter(int dbType , int paramDirection , int paramSize , string paramName , string paramValue)
{
OleDbType dbtype = OleDbType.VarChar;
switch (dbType)
{
case 1 :
dbtype = OleDbType.VarChar;
break;
case 2 :
dbtype = OleDbType.Integer;
break;
case 3 :
dbtype = OleDbType.Date;
break;
case 4 :
dbtype = OleDbType.Decimal;
break;
case 5 :
dbtype = OleDbType.Boolean;
break;
}
return SetParameterDirectionAndValue(new OleDbParameter(paramName , dbtype , paramSize),paramDirection,paramValue);
}
}
}
到此为止整个程序用最少的代码最实现了各种数据库的连接
下面就通过一个工厂模式实现数据库连接的创建,该工厂只创建一个数据库的连接实例
using System;
namespace DBConn
{
public class DBConnFoctory
{
public enum DBConnType{MsSQLConn,ODBCConn,OLEConn,OracleConn};
private DBConn _DBConn;
protected internal DBConn DBConnClass
{
get
{
return _DBConn;
}
}
public DBConnFoctory(string DBConnStr):this(DBConnType.MsSQLConn,DBConnStr)
{
}
public DBConnFoctory(DBConnType ConnType , string DBConnStr)
{
switch(ConnType)
{
case DBConnType.MsSQLConn:
_DBConn = new MsSQLConn(DBConnStr);
break;
case DBConnType.ODBCConn:
_DBConn = new ODBCConn(DBConnStr);
break;
case DBConnType.OLEConn:
_DBConn = new OLEConn(DBConnStr);
break;
case DBConnType.OracleConn:
_DBConn = new OracleConn(DBConnStr);
break;
}
}
}
}
然后实现一个纯虚函数,
这儿本来是通过一个接口的来实现的,但后来改成了纯虚类
用户就通过这个类对数据库连接的所有操作进行访问
using System;
using System.Data;
namespace DBConn
{
public abstract class AbsBasicExecSQL:DBConnFoctory,IDBConn
{
public AbsBasicExecSQL(string DBConnStr):base(DBConnStr)
{
}
public AbsBasicExecSQL(DBConnType ConnType , string DBConnStr):base(ConnType, DBConnStr)
{
}
#region 执行基本SQL语句接口
public abstract DataTable ExecToDataTable(string sqlstr);
public abstract DataTable ExecToDataTable(string sqlstr, DataSet dataset);
public abstract DataRow ExecToDataRow(string sqlstr);
public abstract IDataReader ExecToDataReader(string sqlstr);
public abstract object ExecToScalar(string sqlstr);
public abstract int ExecToNoQuery(string sqlstr);
#endregion
#region 执行存储过程接口
public abstract void GetParamValue(IDbCommand command , IDbDataParameter[] paramList);
public abstract DataTable ExecToDataTable(string sqlstr , IDbDataParameter[] paramList);
public abstract DataTable ExecToDataTable(string sqlstr, DataSet dataset , IDbDataParameter[] paramList);
public abstract IDataReader ExecToDataReader(string sqlstr , IDbDataParameter[] paramList);
public abstract object ExecToScalar(string sqlstr , IDbDataParameter[] paramList);
public abstract int ExecToNoQuery(string sqlstr , IDbDataParameter[] paramList);
#endregion
#region IDBConn 成员
public IDbConnection DataBaseConnection
{
get
{
return DBConnClass.DataBaseConnection;
}
set
{
DBConnClass.DataBaseConnection = value;
}
}
public string GetConnStr
{
get
{
return DBConnClass.GetConnStr;
}
set
{
DBConnClass.GetConnStr = value;
}
}
public bool GetConnState()
{
return DBConnClass.GetConnState();
}
public void OpenDBCon()
{
DBConnClass.OpenDBCon();
}
public void CloseDBCon()
{
DBConnClass.CloseDBCon();
}
public void begin
Tran()
{
DBConnClass.begin
Tran();
}
public void CommitTran()
{
DBConnClass.CommitTran();
}
public void RollbackTran()
{
DBConnClass.RollbackTran();
}
#endregion
#region IDisposable 成员
public void Dispose()
{
DBConnClass.Dispose();
}
#endregion
}
}
然后从AbsBasicExecSQL派生一个类,实现AbsBasicExecSQL类所定义的所有纯虚函数
using System;
using System.Data;
namespace DBConn
{
public class BasicExecSQL : AbsBasicExecSQL
{
public BasicExecSQL(string DBConnStr):base(DBConnStr)
{
}
public BasicExecSQL(DBConnType ConnType , string DBConnStr):base(ConnType,DBConnStr)
{
}
#region 执行已经构造好的IDbCommand
protected virtual DataTable ExecToDataTable(IDbCommand dbcommand , DataSet dataset)
{
if (dataset == null) dataset = new DataSet();
DBConnClass.InitDataAdapter(dbcommand).Fill(dataset);
return dataset.Tables[0];
}
protected virtual IDataReader ExecToDataReader(IDbCommand dbcommand)
{
DBConnClass.OpenDBCon();
IDataReader dr = dbcommand.ExecuteReader();
DBConnClass.CloseDBCon();
return dr;
}
protected virtual object ExecToScalar(IDbCommand dbcommand)
{
return dbcommand.ExecuteScalar();
}
protected virtual int ExecToNoQuery(IDbCommand dbcommand)
{
return dbcommand.ExecuteNonQuery();
}
#endregion
#region 执行一个SQL字符串
public override DataTable ExecToDataTable(string sqlstr)
{
using (DataSet ds = new DataSet())
{
return ExecToDataTable(sqlstr,ds);
}
}
public override DataTable ExecToDataTable(string sqlstr, DataSet dataset)
{
return ExecToDataTable(sqlstr , dataset , null);
}
public override DataRow ExecToDataRow(string sqlstr)
{
DataTable dt = ExecToDataTable(sqlstr);
return dt.Rows.Count> 0 ? dt.Rows[0] : null;
}
public override IDataReader ExecToDataReader(string sqlstr)
{
return ExecToDataReader(sqlstr , null);
}
public override object ExecToScalar(string sqlstr)
{
return ExecToScalar(sqlstr, null);
}
public override int ExecToNoQuery(string sqlstr)
{
return ExecToNoQuery(sqlstr,null);
}
#endregion
#region 执行一个带参数的存储过程
public override void GetParamValue(IDbCommand command , IDbDataParameter[] paramList)
{
if (paramList == null) return;
foreach(IDbDataParameter idp in paramList)
{
if (idp.Direction != ParameterDirection.Input)
idp.Value = (command.Parameters[idp.ParameterName] as IDbDataParameter).Value;;
}
}
public override DataTable ExecToDataTable(string sqlstr , IDbDataParameter[] paramList)
{
return ExecToDataTable(sqlstr,null,paramList);
}
public override DataTable ExecToDataTable(string sqlstr, DataSet dataset , IDbDataParameter[] paramList)
{
IDbCommand command = DBConnClass.InitSqlCommand(sqlstr , DBConnClass.DataBaseConnection , paramList);
DataTable dt = ExecToDataTable(command , dataset);
GetParamValue(command , paramList);
return dt;
}
public override IDataReader ExecToDataReader(string sqlstr , IDbDataParameter[] paramList)
{
IDbCommand command = DBConnClass.InitSqlCommand(sqlstr , DBConnClass.DataBaseConnection , paramList);
IDataReader ida = ExecToDataReader(command);
GetParamValue(command , paramList);
return ida;
}
public override object ExecToScalar(string sqlstr , IDbDataParameter[] paramList)
{
IDbCommand command = DBConnClass.InitSqlCommand(sqlstr , DBConnClass.DataBaseConnection , paramList);
object o = ExecToScalar(command);
GetParamValue(command , paramList);
return o;
}
public override int ExecToNoQuery(string sqlstr , IDbDataParameter[] paramList)
{
IDbCommand command = DBConnClass.InitSqlCommand(sqlstr , DBConnClass.DataBaseConnection , paramList);
int i = ExecToNoQuery(command);
GetParamValue(command , paramList);
return i;
}
#endregion
}
}
所有代码到此为止就贴完了,问题也就出来了,
就AbsBasicExecSQL类而言
不管是一个类还是一个接口,该不会只让用户执行
public abstract DataTable ExecToDataTable(string sqlstr);
public abstract DataTable ExecToDataTable(string sqlstr, DataSet dataset);
public abstract DataRow ExecToDataRow(string sqlstr);
public abstract IDataReader ExecToDataReader(string sqlstr);
public abstract object ExecToScalar(string sqlstr);
public abstract int ExecToNoQuery(string sqlstr);
这几种操作吧,用户在执行这几种操作的同时肯定还需要对数据库连接进行其它处理,
比如打开,关闭数据库连接,执行数据库事务等等操作(这些操作都在IDBConn接口中进行约定过)
所以该类在从DBConnFoctory类派生的同时也实现了IDBConn接口
但该接口实现的代码就显得非常冗余,差不多完全是在调用DBConnClass类的操作;
用现有的方式确实实现了!
但代码显得太过于冗余,该类在实现IDBConn接口的时候完全是在照搬DBConnClass类的实现结果
如果现在将IDBConn接口的定义作一小改动,AbsBasicExecSQL类的调用过程也要作相应更改,很烦!
现在的问题就是有没有办法让用户在通过AbsBasicExecSQL类进行操作(这儿是指该类定义的虚函数)的同时
用更好的方式把IDBConn接口实现的操作包含进来?
 
这其实就是一个典型的adapter的设计模式的应用的例子.
可以将DBConnClass的方法adapter到你的类中间去.
 
后退
顶部