Data access layer design (3)

Source: Internet
Author: User

3. Configuration Design and factory design

1) Configuration

Using system;
Using system. Data;
Using system. xml;
Using system. configuration;
Using hkh. database;
Using hkh. database. type;

Namespace hkh. database. config
{
/// <Summary>
/// Configuration class of the data access layer, Singleton Mode
/// </Summary>
/// <Remarks>
/// Create by liwt on 2006-06-21
/// </Remarks>
Public class clsdbaccessconfig
{
// Keep static references to yourself
Private Static clsdbaccessconfig m_clsdbaccessconfig = new clsdbaccessconfig ();

# Region private variable

Private connectiontype m_conntype = NULL;

# Endregion

# Region attributes

Public connectiontype conntype
{
Get
{
Return m_conntype;
}
}

# Endregion

/// <Summary>
/// Private structure
/// </Summary>
Private clsdbaccessconfig ()
{
Try
{
M_conntype = getconntype ();
}
Catch
{
Throw new exception ("database configuration error, check the configuration and restart the program ");
}
}

/// <Summary>
/// Provide external instances
/// </Summary>
/// <Returns> </returns>
Public static clsdbaccessconfig getinstance ()
{
Return m_clsdbaccessconfig;
}

# Region private Method
/// <Summary>
/// Read the configuration file
/// </Summary>
/// <Returns> </returns>
Private connectiontype getconntype ()
{
Try
{
Connectiontype m_conntype = new connectiontype ();
Appsettingsreader appreader = new appsettingsreader ();

M_conntype.dbtype = (databasetype) system. enum. parse (typeof (database. type. databasetype), (string) appreader. getvalue ("databasetype", typeof (string), true );
M_conntype.provider = appreader. getvalue ("provider", typeof (string) as string;
M_conntype.databasename = appreader. getvalue ("databasename", typeof (string) as string;
M_conntype.userid = appreader. getvalue ("userid", typeof (string) as string;
M_conntype.password = appreader. getvalue ("password", typeof (string) as string;
M_conntype.integratedsecurity = convert. toboolean (appreader. getvalue ("integratedsecurity", typeof (string )));
M_conntype.servername = appreader. getvalue ("servername", typeof (string) as string;
M_conntype.connecttimeout = convert. toint32 (appreader. getvalue ("connecttimeout", typeof (string )));
M_conntype.mode = appreader. getvalue ("Mode", typeof (string) as string;
M_conntype.extendedproperties = appreader. getvalue ("extendedproperties", typeof (string) as string;

Return m_conntype;
}
Catch (exception ex)
{
Throw ex;
}
}

# Endregion

}
}

2) Bridge Design

Using system;
Using system. xml;
Using system. Data;
Using system. configuration;
Using system. reflection;
Using hkh. database. interface;
Using hkh. database. config;
Using hkh. database. type;
Using hkh. database. SQL;
Using hkh. database. ODBC;
Using hkh. database. oledb;

Namespace hkh. Database
{
/// <Summary>
/// Summary of dbfactory.
/// </Summary>
Public sealed class dbfactory
{
Private Static databasetype dbtype = hkh. database. config. clsdbaccessconfig. getinstance (). conntype. dbtype;

Private dbfactory ()
{
}

# Region obtains the idataaccess instance

/// <Summary>
/// Obtain the database metadata instance
/// </Summary>
/// <Returns> </returns>
Public static idbaccess getdataaccess ()
{
Try
{
Idbaccess m_idbaccess = NULL;

Switch (dbtype)
{
Case databasetype. sqlserver:
{
M_idbaccess = new clssqldbaccess ();
Break;
}
Case databasetype. ODBC:
{
M_idbaccess = new clsodbcdbaccess ();
Break;
}
Case databasetype. oledb:
{
M_idbaccess = new clsoledbdbaccess ();
Break;
}
Default:
Break;
}

Return m_idbaccess;
}
Catch (exception ex)
{
Throw ex;
}
}

/// <Summary>
/// Obtain the database metadata instance
/// </Summary>
/// <Param name = "myds"> </param>
/// <Returns> </returns>
Public static idbaccess getdataaccess (Dataset myds)
{
Try
{
Idbaccess m_idbaccess = NULL;

Switch (dbtype)
{
Case databasetype. sqlserver:
{
M_idbaccess = new clssqldbaccess (myds );
Break;
}
Case databasetype. ODBC:
{
M_idbaccess = new clsodbcdbaccess (myds );
Break;
}
Case databasetype. oledb:
{
M_idbaccess = new clsoledbdbaccess (myds );
Break;
}
Default:
Break;
}

Return m_idbaccess;
}
Catch (exception ex)
{
Throw ex;
}
}

/// <Summary>
/// Obtain the database metadata instance
/// </Summary>
/// <Param name = "myds"> </param>
/// <Param name = "conn"> </param>
/// <Param name = "trans"> </param>
/// <Returns> </returns>
Public static idbaccess getdataaccess (Dataset myds, idbconnection Conn, idbtransaction trans)
{
Try
{
Idbaccess m_idbaccess = NULL;

Switch (dbtype)
{
Case databasetype. sqlserver:
{
M_idbaccess = new clssqldbaccess (myds, Conn, trans );
Break;
}
Case databasetype. ODBC:
{
M_idbaccess = new clsodbcdbaccess (myds, Conn, trans );
Break;
}
Case databasetype. oledb:
{
M_idbaccess = new clsoledbdbaccess (myds, Conn, trans );
Break;
}
Default:
Break;
}

Return m_idbaccess;
}
Catch (exception ex)
{
Throw ex;
}
}

/// <Summary>
/// Obtain the database metadata instance
/// </Summary>
/// <Param name = "conn"> </param>
/// <Param name = "trans"> </param>
/// <Returns> </returns>
Public static idbaccess getdataaccess (idbconnection Conn, idbtransaction trans)
{
Try
{
Idbaccess m_idbaccess = NULL;

Switch (dbtype)
{
Case databasetype. sqlserver:
{
M_idbaccess = new clssqldbaccess (Conn, trans );
Break;
}
Case databasetype. ODBC:
{
M_idbaccess = new clsodbcdbaccess (Conn, trans );
Break;
}
Case databasetype. oledb:
{
M_idbaccess = new clsoledbdbaccess (Conn, trans );
Break;
}
Default:
Break;
}

Return m_idbaccess;
}
Catch (exception ex)
{
Throw ex;
}
}

# Endregion

# Region obtaining idaobuilder instances

/// <Summary>
/// Obtain the instance of the Database Access Object
/// </Summary>
/// <Returns> </returns>
Public static idaobuilder getdaobuilder ()
{
Try
{
Idaobuilder m_idaobuilder = NULL;
Switch (dbtype)
{
Case databasetype. sqlserver:
{
M_idaobuilder = new clssqldaobuilder ();
Break;
}
Case databasetype. ODBC:
{
M_idaobuilder = new clsodbcdaobuilder ();
Break;
}
Case databasetype. oledb:
{
M_idaobuilder = new clsoledbdaobuilder ();
Break;
}
Default:
Break;
}

Return m_idaobuilder;
}
Catch (exception ex)
{
Throw ex;
}
}

/// <Summary>
/// Obtain the instance of the Database Access Object
/// </Summary>
/// <Param name = "myds"> </param>
/// <Returns> </returns>
Public static idaobuilder getdaobuilder (Dataset myds)
{
Try
{
Idaobuilder m_idaobuilder = NULL;
Switch (dbtype)
{
Case databasetype. sqlserver:
{
M_idaobuilder = new clssqldaobuilder (myds );
Break;
}
Case databasetype. ODBC:
{
M_idaobuilder = new clsodbcdaobuilder (myds );
Break;
}
Case databasetype. oledb:
{
M_idaobuilder = new clsoledbdaobuilder (myds );
Break;
}
Default:
Break;
}

Return m_idaobuilder;
}
Catch (exception ex)
{
Throw ex;
}
}

/// <Summary>
/// Obtain the instance of the Database Access Object
/// </Summary>
/// <Param name = "myds"> </param>
/// <Param name = "conn"> </param>
/// <Param name = "trans"> </param>
/// <Returns> </returns>
Public static idaobuilder getdaobuilder (Dataset myds, idbconnection Conn, idbtransaction trans)
{
Try
{
Idaobuilder m_idaobuilder = NULL;
Switch (dbtype)
{
Case databasetype. sqlserver:
{
M_idaobuilder = new clssqldaobuilder (myds, Conn, trans );
Break;
}
Case databasetype. ODBC:
{
M_idaobuilder = new clsodbcdaobuilder (myds, Conn, trans );
Break;
}
Case databasetype. oledb:
{
M_idaobuilder = new clsoledbdaobuilder (myds, Conn, trans );
Break;
}
Default:
Break;
}

Return m_idaobuilder;
}
Catch (exception ex)
{
Throw ex;
}
}

/// <Summary>
/// Obtain the instance of the Database Access Object
/// </Summary>
/// <Param name = "conn"> </param>
/// <Param name = "trans"> </param>
/// <Returns> </returns>
Public static idaobuilder getdaobuilder (idbconnection Conn, idbtransaction trans)
{
Try
{
Idaobuilder m_idaobuilder = NULL;
Switch (dbtype)
{
Case databasetype. sqlserver:
{
M_idaobuilder = new clssqldaobuilder (Conn, trans );
Break;
}
Case databasetype. ODBC:
{
M_idaobuilder = new clsodbcdaobuilder (Conn, trans );
Break;
}
Case databasetype. oledb:
{
M_idaobuilder = new clsoledbdaobuilder (Conn, trans );
Break;
}
Default:
Break;
}

Return m_idaobuilder;
}
Catch (exception ex)
{
Throw ex;
}
}

# Endregion

# Region private Method

# Endregion

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.