PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

开发自己的Data Access Application Block[下篇]

原创
2016-06-07 15:48:45 698浏览

上接: [原创] 我的ORM: 开发自己的Data Access Application Block - Part I 4. Database 下面来介绍重中之重:Database,绝大部分的DataAccess 操作都集中在这个Abstract Database中。这是一个相对庞大的Class,所以不得不采用Partial Class的方式来编写。

上接:[原创] 我的ORM: 开发自己的Data Access Application Block - Part I
4. Database

下面来介绍重中之重:Database,绝大部分的Data Access 操作都集中在这个Abstract Database中。这是一个相对庞大的Class,所以不得不采用Partial Class的方式来编写。

Part I:Field 和Property

这些Field 和Property基本上对应我们前面的Configuraiton。此为我们定义了三个Field 和Property:DbDataAdapter,Connection,_transaction。考虑到垃圾回收,使Database实现IDisposable接口。值得说明一点的是,我们通过Database的DatabaseProviderFactory创建了泛型的DbDataAdapter,DbConnection和Transaction。

  • 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 _defaultCommandType;
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._defaultCommandType;
}

set
{
this._defaultCommandType = 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()
&

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。