ORM: 开发自己的Data Access Application Block - Part II
2008-12-06 10:15:42 来源:WEB开发网4. Database
下面来介绍重中之重:Database,绝大部分的DataAccess 操作都集中在这个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
{
/**//// <summary>
/// Database defines a series of database-based operations.
/// </summary>
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;
/**//// <summary>
/// Database connection string which is specified by the database factory.
/// </summary>
public string ConnectionString
{
get
{
return this._connectionString;
}
set
{
this._connectionString = value;
}
}
/**//// <summary>
/// The concrete database specific provider factory.
/// </summary>
public DbProviderFactory DatabaseProviderFactory
{
get
{
return this._dbProviderFactory;
}
set
{
this._dbProviderFactory = value;
}
}
/**//// <summary>
/// The defaull command type to perform the database operations which do not specify the commanf type.
/// </summary>
public CommandType DefaultCommandType
{
get
{
return this._defaultCommandType;
}
set
{
this._defaultCommandType = value;
}
}
/**//// <summary>
///Determine whether to use command builder or mapped stored procedures to execute database operations.
/// </summary>
public bool UseCommandBuilder
{
get
{
return this._useCommandBuilder;
}
set
{
this._useCommandBuilder = value;
}
}
/**//// <summary>
/// A string which indicates the type to perform mapping between stored procedure parameter and source column.
/// </summary>
public IDbParameterNameMapping DbParameterNameMapping
{
get
{
return this._dbParameterNameMapping;
}
set
{
this._dbParameterNameMapping = value;
}
}
/**//// <summary>
/// A string which indicates the type to perform mapping between table name and the related stored procedure names.
/// </summary>
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;
/**//// <summary>
/// A generic database data adapter which is responsible for save the changed data into database.
/// </summary>
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;
}
}
/**//// <summary>
/// The database connection.
/// </summary>
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()
{
}
#endregion
IDisposable Members#region IDisposable Members
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!this._isDisposed)
{
if (disposing)
{
if (this._connection != null)
{
if (this._connection.State == ConnectionState.Open)
{
this._connection.Close();
}
}
if (this._transaction != null)
{
this._transaction.Dispose();
}
}
}
this._isDisposed = true;
}
#endregion
}
}
- ››自己写的一个php基于phpQuery的通用采集类
- ››开发Android 日历教程
- ››开发学院总结 Win 8实用技巧大全
- ››开发学院原创教程:把win8的IE10放桌面上方法(非...
- ››开发者眼中的Windows Phone和Android
- ››开发学院教你用SQL 语句最快速清空MySQL 数据表的...
- ››自己动手写iPhone wap浏览器之界面架构篇
- ››自己也能DIY个性真人QQ表情
- ››自己动手!巧法让酷狗动感歌词更完美
- ››自己编译Google Android内核的Linux源码
- ››自己写的一个jquery模板引擎(json比较好用)
- ››开发一个自己的HTML在线编辑器(一)
更多精彩
赞助商链接