Database abstract factory implemented with C # (1)
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 default connection /// summary> /// < Returns> Database connection returns> idbconnection createConnection ();
/// / / / Establish Connection object /// // // // // connection string param> /// /// Connection object < / RETURns> IDBConnection CreateConnection (String StrConn);
/// /// Establish Command object /// summary> /// Command object returns> idbcommand createcommand ();
/// /// Establish a DataAdapter object /// summary> /// DataAdapter object returnapter (iDBDataAdapter CreateDataAdapter);
/// /// According to Connection Transaction /// summary> /// Connection object param> /// Transaction object returns> iDBTRANSACTION CreateTransaction (idbconnection mydbconnection);
/// /// Established DataReader /// /////////////////////////////////// DataReader Object Returns> iDataAder CreateDataReader (idbcommand mydbcommand);
/// /// Get a connection string /// / summary> /// Connection string returns> string getConnectionstring ();}}
(2) Factory.cs
Using system; using system.configuration;
namespace DbService {/// /// Factory class /// summary> public sealed class Factory {private static volatile Factory singleFactory = null; private static object syncObj = new object (); /// /// Factory class constructor /// summary> private factory () {} /// /// Get an instance of the Factory class /// summary> //// Factory class instance returns> public static factory getinstance () {iffactory == null) {lock (syncobj) {if (SINGLACTORY == NULL) {SingleFactory = new factory ();}}} returnifact
/// /// Factory established class instance /// summary> /// Factory instance of the class returns> public AbstractDbFactory CreateInstance () {AbstractDbFactory abstractDbFactory = null; switch (ConfigurationSettings.AppSettings [ . "DatabaseType"] ToLower ()) {case "sqlserver": {abstractDbFactory = new SqlFactory (); break;} case "oledb": {abstractDbFactory = new OleDbFactory (); break;} case "odbc": {abstractDbFactory = NEW odbcfactory (); break;}} Return AbstractDbFactory;}}}
to be continued......