Database abstract factory implemented with C # (1)

zhaozj2021-02-16  59

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 /// public interface AbstractDbFactory {/// /// Establish default connection /// /// < Returns> Database connection idbconnection createConnection ();

///

/ / / Establish Connection object /// connection string /// /// Connection object < / RETURns> IDBConnection CreateConnection (String StrConn);

///

/// Establish Command object /// /// Command object idbcommand createcommand ();

///

/// Establish a DataAdapter object /// /// DataAdapter object

///

/// According to Connection Transaction /// /// Connection object /// Transaction object iDBTRANSACTION CreateTransaction (idbconnection mydbconnection);

///

/// Established DataReader /// DataReader Object iDataAder CreateDataReader (idbcommand mydbcommand);

///

/// Get a connection string /// /// Connection string string getConnectionstring ();}}

(2) Factory.cs

Using system; using system.configuration;

namespace DbService {///

/// Factory class /// public sealed class Factory {private static volatile Factory singleFactory = null; private static object syncObj = new object (); /// /// Factory class constructor /// private factory () {} /// /// Get an instance of the Factory class /// //// Factory class instance public static factory getinstance () {iffactory == null) {lock (syncobj) {if (SINGLACTORY == NULL) {SingleFactory = new factory ();}}} returnifact

///

/// Factory established class instance /// /// Factory instance of the class 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......

转载请注明原文地址:https://www.9cbs.com/read-24825.html

New Post(0)