WEB开发网
开发学院数据库MSSQL Server ORM: 开发自己的Data Access Application Block -... 阅读

ORM: 开发自己的Data Access Application Block - Part II

 2008-12-06 10:15:42 来源:WEB开发网   
核心提示:4. Database下面来介绍重中之重:Database,绝大部分的DataAccess 操作都集中在这个Abstract Database中,ORM: 开发自己的Data Access Application Block - Part II,这是一个相对庞大的Class,所以不得不采用Partial Class的方

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   
  }
}

1 2 3  下一页

Tags:ORM 开发 自己

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接