Access: [original] My ORM: Develop your own DataAccessApplicationBlock
Access: [original] My ORM: Develop your own Data Access Application Block-Part I 4. database: Most DataAccess operations are concentrated in this Abstract Database. This is a relatively large Class, so it has to be written in Partial Class mode.
Access: [original] My ORM: Develop your own Data Access Application Block-Part I
4. Database
Next we will introduce the most important thing: Database, the vast majority of Data Access operations are concentrated in this Abstract Database. This is a relatively large Class, so it has to be written in Partial Class mode.
Part I: Field and Property
These fields and properties basically correspond to our previous pipeline aiton. This defines three fields and properties: DbDataAdapter, Connection, and _ transaction. The Database implements the IDisposable interface considering garbage collection. It is worth noting that we have created a generic DbDataAdapter, DbConnection, and Transaction through Database DatabaseProviderFactory.
-
ConnectionString: string
-
DatabaseProviderFactory: DbProviderFactory
-
DefaultCommandType: CommandType
-
UseCommandBuilder: bool
-
DbParameterNameMapping: IDbParameterNameMapping
-
StoredProcedureNameMapping: IStoredProcedureNameMapping
-
DbDataAdapter: DbDataAdapter
-
Connection: DbConnection
-
Transaction: DbTransaction
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Data;
Using System. Data. Common;
Using Artech. ApplicationBlock. DataMapping;
Namespace Artech. ApplicationBlock. DataAccess
{
/**////
/// Database defines a series of database-based operations.
///
Public abstract partial class Database: IDisposable
{
Private bool _ isDisposed;
The five private fields possess the corressponding pubic properties, and they are only allowed to be evaluated by Database Factory. # region The five private fields possess the corressponding pubic properties, and they are only allowed to be evaluated by Database Factory.
Private DbProviderFactory _ dbProviderFactory;
Private string _ connectionString;
Private CommandType _ defacommandcommandtype;
Private bool _ useCommandBuilder;
Private IDbParameterNameMapping _ dbParameterNameMapping;
Private IStoredProcedureNameMapping _ storedProcedureNameMapping;
/**////
/// Database connection string which is specified by the database factory.
///
Public string ConnectionString
{
Get
{
Return this. _ connectionString;
}
Set
{
This. _ connectionString = value;
}
}
/**////
/// The concrete database specific provider factory.
///
Public DbProviderFactory DatabaseProviderFactory
{
Get
{
Return this. _ dbProviderFactory;
}
Set
{
This. _ dbProviderFactory = value;
}
}
/**////
/// The defaull command type to perform the database operations which do not specify the commanf type.
///
Public CommandType DefaultCommandType
{
Get
{
Return this. _ defacommandcommandtype;
}
Set
{
This. _ defacommandcommandtype = value;
}
}
/**////
/// Determine whether to use command builder or mapped stored procedures to execute database operations.
///
Public bool UseCommandBuilder
{
Get
{
Return this. _ useCommandBuilder;
}
Set
{
This. _ useCommandBuilder = value;
}
}
/**////
/// A string which indicates the type to perform mapping between stored procedure parameter and source column.
///
Public IDbParameterNameMapping DbParameterNameMapping
{
Get
{
Return this. _ dbParameterNameMapping;
}
Set
{
This. _ dbParameterNameMapping = value;
}
}
/**////
/// A string which indicates the type to perform mapping between table name and the related stored procedure names.
///
Public IStoredProcedureNameMapping StoredProcedureNameMapping
{
Get
{
Return this. _ storedProcedureNameMapping;
}
Set
{
This. _ storedProcedureNameMapping = value;
}
}
# Endregion
Connection & Database DataAdapter # region Connection & Database DataAdapter
Private DbDataAdapter _ dbDataAdapter;
Private DbConnection _ connection;
/**////
/// A generic database data adapter which is responsible for save the changed data into database.
///
Private DbDataAdapter DatabaseAdapter
{
Get
{
If (this. _ dbDataAdapter = null)
{
This. _ dbDataAdapter = this. _ dbProviderFactory. CreateDataAdapter ();
This. _ dbDataAdapter. AcceptChangesDuringUpdate = false;
This. _ dbDataAdapter. MissingSchemaAction = MissingSchemaAction. Add;
}
Return this. _ dbDataAdapter;
}
}
/**////
/// The database connection.
///
Private DbConnection Connection
{
Get
{
If (this. _ connection = null)
{
This. _ connection = this. _ dbProviderFactory. CreateConnection ();
This. _ connection. ConnectionString = this. _ connectionString;
}
Return this. _ connection;
}
}
# Endregion
Constructor # region Constructor
Public Database ()
&