ORM: 开发自己的Data Access Application Block - Part I
2008-12-06 10:15:45 来源:WEB开发网这两个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
}
}
- ››开发Android 日历教程
- ››开发学院总结 Win 8实用技巧大全
- ››开发学院原创教程:把win8的IE10放桌面上方法(非...
- ››开发者眼中的Windows Phone和Android
- ››开发学院教你用SQL 语句最快速清空MySQL 数据表的...
- ››自己动手写iPhone wap浏览器之界面架构篇
- ››自己也能DIY个性真人QQ表情
- ››自己动手!巧法让酷狗动感歌词更完美
- ››自己编译Google Android内核的Linux源码
- ››自己写的一个jquery模板引擎(json比较好用)
- ››开发一个自己的HTML在线编辑器(一)
- ››开发一个自己的HTML在线编辑器(二)
更多精彩
赞助商链接