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

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

 2008-12-06 10:15:45 来源:WEB开发网   
核心提示: 这两个Mapping主要用在通过Dataset跟新数据库的场景,利用IDbParameterNameMapping,ORM: 开发自己的Data Access Application Block - Part I(6),我们通过Dataset中各个Table name获得对它进行Inser

这两个Mapping主要用在通过Dataset跟新数据库的场景,利用IDbParameterNameMapping,我们通过Dataset中各个Table name获得对它进行Insert,Update,Delete操作的Stored procedure的name。利用IDbParameterNameMapping,我们可以为Stored procedure的Parameter指定对应的Source field. 

注:GetParameterName方法实际上是不需要的,我把使用在另一个AppBlock中。

接下来我们来写两个实现了上面连个Interface的默认的mapping:SimpleStoredProcedureNameMapping和SimpleDbParameterNameMapping。他实际上实现了这样的Mapping:比如Table name为T_ABC_DEF(我经常用的命名方式:以T开头代表Table,名称大写并一下划线连接),那么对应的Stored procedure name分别为:sp_abc_def_s(Select), sp_abc_def_i(Insert), sp_abc_def_u(Update), sp_abc_def_d(delete)。如果Field name为ABC_123,那么对于Original version的Parameter name为o_abc_123(o代表Original),Current version的Parameter name为p_abc_123(p代表一般意义的Parameter)。

using System;
using System.Collections.Generic;
using System.Text;
namespace Artech.ApplicationBlock.DataMapping
{
  /**//// <summary>
  /// IStoredProcedureNameMapping defines the mapping between the data table name and the name of stored procedures to perform insertion, modification and deletion operation.
  /// </summary>
  public class SimpleStoredProcedureNameMapping:IStoredProcedureNameMapping
  {
    IStoredProcedureNameMapping Members#region IStoredProcedureNameMapping Members
    /**//// <summary>
    /// Get the name of stored procedure to perform selection operation.
    /// </summary>
    /// <param name="tableName">The name of the database table.</param>
    /// <returns>The name of stored procedure to perform selection operation</returns>
    public string GetSelectStoredProcedureName(string tableName)
    {
      //T_ABC_DEF=>sp_abc_def_s
      return string.Format("sp_{0}_s", tableName.Substring(2, tableName.Length - 2).ToLower());
    }
    /**//// <summary>
    /// Get the name of stored procedure to perform insert operation.
    /// </summary>
    /// <param name="tableName">The name of the database table.</param>
    /// <returns>The name of stored procedure to perform insertion operation</returns>
    public string GetInsertStoredProcedureName(string tableName)
    {
      //T_ABC_DEF=>sp_abc_def_i
      return string.Format("sp_{0}_i", tableName.Substring(2, tableName.Length - 2).ToLower());
    }
    /**//// <summary>
    /// Get the name of stored procedure to perform modification operation.
    /// </summary>
    /// <param name="tableName">The name of the database table.</param>
    /// <returns>The name of stored procedure to perform modification operation</returns>
    public string GetModifyStoredProcedureName(string tableName)
    {
      //T_ABC_DEF=>sp_abc_def_u
      return string.Format("sp_{0}_u", tableName.Substring(2, tableName.Length - 2).ToLower());
    }
    /**//// <summary>
    /// Get the name of stored procedure to perform deletion operation.
    /// </summary>
    /// <param name="tableName">The name of the database table.</param>
    /// <returns>The name of stored procedure to perform deletion operation</returns>
    public string GetDeleteStoredProcedureName(string tableName)
    {
      //T_ABC_DEF=>sp_abc_def_d
      return string.Format("sp_{0}_d", tableName.Substring(2, tableName.Length - 2).ToLower());
    }
    #endregion
  }
}

上一页  1 2 3 4 5 6 7 8 9 10  下一页

Tags:ORM 开发 自己

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接