I have recently learned the design model, and I tried to use the C # to use an abstract factory mode to implement the abstract factory. There is slightly different from the standard mode. I have added some my thoughts. I hope everyone criticizes the correct, the code is divided into 6. Category: (1) AbstractDbFactory.cs Using System; Using System.Data; Namespace DBService {/// /// Database Abstract Factory interface /// summary> public interface abstractdbFactory {/// /// Establish a default connection /// summary> /// Database connection returns> idbconnection createConnection (); /// / / / /> Creating the connection string /// < / summary> /// connection string param> /// connection object returns> iDBConnection CreateConnection (String StrConn); /// /// Establishment Command object /// summary> /// Command object returns> idbcommand createcommand (); /// /// Establish DataAdapter object /// summary> /// DataAdapter object returns> idbdataadapter createDataAdapter (); /// // / according to Connection TRANSAction /// summary> /// Connection object param> /// Transaction object returns> idbtransaction createTransaction (idbconnection mydbconnection); /// // // based on Command DataReader / // summary> ///
ODBC ": {AbstractDbFactory = New OdbcFactory (); break;}} Return AbstractDbFactory;}}} The following three classes are Factory for SQL Server dedicated connections, OLEDB connections, and ODBC connections: (3) SQLFactory.cs Using System; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace DbService {/// /// /// SqlServer plant for dedicated connection summary> public class SqlFactory: AbstractDbFactory { /// /// Constructor /// summary> public sqlfactory () {} /// /// Establish default Connection object /// summary> /// Connection object returns> public idbconnection createConnection () {return new summary> // / Establish Connection object /// Connection Object param> /// Transaction Object returns> public IDbTransaction CreateTransaction (IDbConnection myDbConnection) {Return mydbconnection.begintransaction ();} /// /// Established DataReader /// ///////// Command object parammand> //// < Returns> DataReader object returns> public iDataReader createDataReader (idbcommand mydbcommand) {return mydbcommand.executeReader ();} /// /// get connection string /// summary> /// The connection string returns> public string GetConnectionString () {string strServer = ConfigurationSettings.AppSettings [ "SqlServerServer"]; string strDatabase = ConfigurationSettings.AppSettings [ "SqlServerDatabase"]; string strUid = ConfigurationSettings.AppSettings [ "Sql ServerUid "]; string strPwd = ConfigurationSettings.AppSettings [" SqlServerPwd "]; string strConnectionString =" Server = " strServer "; Database = " strDatabase "; Uid = " strUid "; Pwd = " strPwd ";";}}} (4) OLEDBFACTORY.CS USING SYSTEM; use system.data; use system.data.oledb; use system.configuration; namespace dbservice {/// //> For OLEDB Connected factory /// summary>
Public class oledbfactory: AbstractDbFactory {/// /// Constructor /// summary> public oledbFactory () {} /// /// Establish default Connection object /// summary> /// Connection object returns> public idbconnection createConnection () {Return new oledbconnection ();} /// / / / / / /1 / Connection string param> /// Connection object returns> public idbconnection createconnection (string strconn) {return new oledbconnection (StrConn);} /// /// Establish a Command object /// ///// Command object returns> public idbcommand createcommand () {return new oledbcommand ();} /// /// Establish DataAPter object /// summary> /// DataAdapter object returns> pu BliciDbdataadapter createDataAdapter () {return new oledbdataadapter ();} /// ///> According to Connection Transaction /// summary> ///
myDbCommand "> Command Object param> /// DataReader Object returns> public IDataReader CreateDataReader (IDbCommand myDbCommand) {return myDbCommand.ExecuteReader ();} /// /// connection string obtained /// summary> /// connection string returns> public string GetConnectionString () {string strProvider = ConfigurationSettings.AppSettings [ "OleDbProvider"]; string strDataSource = ConfigurationSettings.AppSettings [ "OleDbDataSource"]; string strConnectionString = "Provider =" strProvider "; Data Source =" strDataSource ";"; return strConnectionString;}}} (5) OdbcFactory.cs using System; using System.Data; using System.Data.Odbc; Using system.configuration; namespace dbservice {/// // / /// summary> public class odbcfactory: AbstractDracfbFactory {//// /// Since / summary> public odbcf Actory () {} /// /// Establish a default Connection object /// summary> /// Connection object returns> public idbconnection createconnection () {return new odbcConnection ();} /// / / / Establish Connection object /// // // // // connection string param> /// /// Connection object < / RETURns> Public IDBConnection CreateConnection (String StrConn) {Return New OdbcConnection (STRCONN);
} /// /// Establish a Command object /// summary> /// Command object returns> public idbcommand createcommand () {return new odbccommand ();} // /// Establish DataAdapter object /// summary> /// DataAdapter object returns> public idbdataadapter createDataAdapter () {return new odbcdataadapter ();} /// / / / according to Connection establishing Transaction /// summary> /// Connection Object param> /// Transaction Object returns> public IDbTransaction CreateTransaction (IDbConnection myDbConnection) {return myDbConnection.BeginTransaction ();} /// /// Established DataReader /// // ///////////////////////////////////////// /// DataReader object < / returns> public iDataReader CreateDataReader (idbcommand mydbcomma) Nd) {return mydbcommand.executeReader ();} /// /// get connection string /// summary> /// returns> public string getConnectionstring () {string strir = ConfigurationSettings.AppSettings [ "OdbcDriver"]; string strDBQ = ConfigurationSettings.AppSettings [ "OdbcDBQ"]; string strConnectionString = "Driver = {" strDriver "}; DBQ =" strDBQ ";"; return strConnectionString;
}}} Database Abstract Factory Implementation Factory (3) The following is the class that is really called when applying: (6) dbaccess.cs using system; use system.data; namespace dbservice {/// // / DBACCESS class, that is, the class /// summary> public sealed class dbaccess {/// /// dbaccess constructor //// DBACCESS constructor /////////////////////////////////////////////////////////////////////////////////////////////////////>> /// /// unconditional query operation, ie all records in the query table //// summary> /// table name param> /// column group group param> /// unconditional query results returns> public static dataset selectall (string stratlename, string [] strcolumn) {dataset ds = new dataset (); factory factory = factory .GetInstance (); abstractDbFactory abstractDbFactory = factory.CreateInstance (); IDbConnection concreteDbConn = abstractDbFactory.CreateConnection (); concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString (); concreteDbConn.Open (); IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand (); IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction (concreteDbConn); concreteDbCommand.Connection = concreteDbConn; concreteDbCommand.Transaction = concreteDbTrans; IDbDataAdapter concreteDbAdapter = abstractDbFactory.CreateDataAdapter (); try {string strSql = "SELECT"; for (INT i = 0; i
(StrColumn [i] ",");} strSql = (strColumn [strColumn.Length - 1] "FROM" strTableName); concreteDbCommand.CommandText = strSql; concreteDbAdapter.SelectCommand = concreteDbCommand; concreteDbAdapter.Fill (ds) ConcretedbTrans.commit ();} catch {concretedbtrans.rollback (); ds.clear (); throw;} finally {contRededbconn.close ();} Return DS;} /// /// condition query operation /// summary> /// table name param> /// column name group param> /// Condition param> /// Condition Query Results returns> public static dataset select (string stratlename, string [] strcolumn, string strcondition) {dataset ds = new dataset (); Factory factory = Factory.GetInstance (); AbstractDbFactory abstractDbFactory = factory.CreateInstance (); IDbConnection concreteDbConn = abstractDbFactory.CreateConnection (); concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString (); concreteDbConn.Open (); IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand ( ); Idbtransaction concretedbtrans = AbstractDbFactory.createTransaction (ConcretedbConn); concretedbcommand.connection = concretedbconn;
concreteDbCommand.Transaction = concreteDbTrans; IDbDataAdapter concreteDbAdapter = abstractDbFactory.CreateDataAdapter (); try {string strSql = "SELECT"; for (int i = 0; i /// single record Insert operation /// summar Y> /// Table name param> /// column name group param> /// Value group param> public static void Insert (string strTableName, string [] strColumn, object [] strValue) {Factory factory = Factory.GetInstance (); abstractDbFactory abstractDbFactory = factory.CreateInstance (); IDbConnection concreteDbConn = abstractDbFactory.CreateConnection (); Concretedbconn.connectionstring =
abstractDbFactory.GetConnectionString (); concreteDbConn.Open (); IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand (); IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction (concreteDbConn); concreteDbCommand.Connection = concreteDbConn; concreteDbCommand.Transaction = concreteDbTrans; try {string strSql = "INSERT INTO " STRTABLENAME " ("; for (int i = 0; I /// Batch record is inserted once Insert different batch records /// summary> /// of the DataSet, the DataTable name in DataSet, the Datacolumn name, the DataTable, the Datacolumn name Name param>
public static void InsertSet (ref DataSet ds) {Factory factory = Factory.GetInstance (); AbstractDbFactory abstractDbFactory = factory.CreateInstance (); IDbConnection concreteDbConn = abstractDbFactory.CreateConnection (); concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString (); concreteDbConn.Open (); IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand (); IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction (concreteDbConn); concreteDbCommand.Connection = concreteDbConn; concreteDbCommand.Transaction = concreteDbTrans; try {foreach (DataTable dt in ds.Tables) {foreach (DataRow dr In dt.rows) {string strsql = "INSERT INTO" DT.TABLENAME "("; for (INT i = 0; i
Concretedbcommand.executenonquery ();}} contRedbtrans.commit ();} catch};} finally {contRetedbconn.close ();}} /// /// unconditional delete operation, ie delete all records in the table /// summary> /// table param> public static void DeleteAll (string strTableName) {Factory factory = Factory.GetInstance (); abstractDbFactory abstractDbFactory = factory.CreateInstance (); IDbConnection concreteDbConn = abstractDbFactory.CreateConnection (); concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString (); concreteDbConn.Open (); IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand (); IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransa ction (concreteDbConn); concreteDbCommand.Connection = concreteDbConn; concreteDbCommand.Transaction = concreteDbTrans; try {string strSql = "DELETE FROM" strTableName; concreteDbCommand.CommandText = strSql; concreteDbCommand.ExecuteNonQuery (); concreteDbTrans.Commit ();} catch { Concretedbtrans.rollback (); throw;} finally {contRedbconn.close ();}} ///
/// Condition Delete Operation /// summary> /// Table Name param> /// condition param> public static void delete (string strTableName, string strCondition) {Factory factory = Factory.GetInstance (); abstractDbFactory abstractDbFactory = factory.CreateInstance (); IDbConnection concreteDbConn = abstractDbFactory.CreateConnection (); concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString (); concreteDbConn.Open () ; IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand (); IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction (concreteDbConn); concreteDbCommand.Connection = concreteDbConn; concreteDbCommand.Transaction = concreteDbTrans; try {string strSql = "DELETE FROM" strTableName "WHERE" strCondition; Concretedbcommand.commandtext = ST RSQL; contRedbcommand.executenonquery (); contRedbTrans.commit ();} catch {concretedbtrans.rollback (); throw;} finally {contretedbconn.close ();}} /// /// unconditional update operation, ie Update all records /// summary> /// table name param> /// column name group param> ///// Value group param>
public static void UpdateAll (string strTableName, string [] strColumn, object [] strValue) {Factory factory = Factory.GetInstance (); AbstractDbFactory abstractDbFactory = factory.CreateInstance (); IDbConnection concreteDbConn = abstractDbFactory.CreateConnection (); concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString (); concreteDbConn.Open (); IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand (); IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction (concreteDbConn); concreteDbCommand.Connection = concreteDbConn; concreteDbCommand.Transaction = concreteDbTrans; try {string strSql = "UPDATE " STRTABLENAME " set "; for (int i = 0; i /// condition update operation /// summary> /// Table name param> /// column name group param> /// Value group param> /// conditions param> public static void Update (string strTableName, string [] strColumn, object [] strValue, string strCondition) {Factory factory = Factory.GetInstance (); abstractDbFactory abstractDbFactory = factory.CreateInstance (); IDbConnection concreteDbConn = abstractDbFactory.CreateConnection (); concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString (); concreteDbConn.Open (); IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand (); IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction (concreteDbConn); concreteDbCommand.Connection = concreteDbConn; Concretedbcommand.transaction = concretedbtrans; try {string strsql = "Update" strtablename "s ET "; for (int i = 0; I