.NET对存储过程的调用抽象封装
最近一边参与公司的项目开发,一边还肩负着基础库的创建和维护。真真切切的体会到写框架的不容易,写出好的,方便使用的框架更不容易,需要考虑的东西太多,需要掌握的东西太多。不过不要紧我们正在前进的道路上。同志们一起加油!
经过与DBA的沟通,他认为对存储过程的封装是有必要的,以他十几年的经验看,存储过程后期的移植是必不可少的。现在的项目是用SQLSERVER2008开发的,后期可能会移植到ORACLE上去,那么对存储过程的编写DBA考虑很周全。但是对于程序员来说,经验稍微丰富点的可能会通过某种工厂将具体对象脱耦,或者使用依赖倒置的原则来解决更换数据源问题。但是考虑到统一的使用方法,这里还是真的有必要进行封装的。那么如何封装?
Dictionary< string , object > parameter = new Dictionary< string , object >(); parameter.Add( "PurchaseID" , Purchase.TempSerialNo); //单据流水号 parameter.Add( "WarehouseId" , Purchase.InWarehouseID); //仓库ID parameter.Add( "UserID" , Purchase.UserID); //操作ID parameter.Add( "UserName" , Purchase.UserName); //操作人名称 parameter.Add( "PurchaseDate" , DBNull.Value); //采购日期 parameter.Add( "BuyUserID" , DBNull.Value); //采购人编号 parameter.Add( "BuyUserName" , DBNull.Value); //采购人名称 parameter.Add( "BuyDate" , DBNull.Value); //采购日期 parameter.Add( "Memo" , Purchase.Memo); //备注说明 IDataParameter[] parameterDic = IDataParameterFactory.CreateDbDataParameter(parameter); List<IDataParameter> listparameter = IDataParameterHelper.IDataParameterArrayToList(parameterDic); listparameter.Add(WL.DAL.DAL_TB_WLPurchase.GetErrIDParametere()); using (Fast.Orm.IDataSourceOperation operation = Fast.Orm.IDataSourceOperationFactory.Create()) { operation.ExecuteNonQuery(CommandType.StoredProcedure, "prc_WLPurchaseTmpAdd" , listparameter.ToArray()); if (listparameter[listparameter.Count - 1].Value.ToString() == "0" ) return true ; return false ; } |
一般性的封装基本都这样或者在IDataParameterFactory.CreateDbDataParameter(Entity)中加入根据实体的属性动态的创建IDataParameter[]对象,如果你的创建始终是使用反射的话那么将是不可取的。有兴趣的朋友可以参见本人的另一篇文章“利用抽象、多态实现无反射的绿色环保ORM框架”对实体的使用如果不能摆脱反射,那么在以后的基础库扩展中将面临着很多性能问题,这里需三思。
/// <summary> /// 存储过程实体(参数信息类)基类 /// </summary> public abstract class BaseStoredprocedureObject : DictionaryBase { /// <summary> /// 受保护的字段-存储过程名称 /// </summary> protected string procedurename = string .Empty; /// <summary> /// 受保护的字段-命令参数集合 /// </summary> protected List<IDataParameter> parameterlist = new List<IDataParameter>(); /// <summary> /// 获取命令参数集合 /// </summary> public List<IDataParameter> ParameterList { get { return parameterlist; } } /// <summary> /// 添加IDataParameter对象到基类parameterlist对象 /// </summary> public abstract void AddParameterToBaseParameterObject(); /// <summary> /// 获取存储过程名称 /// </summary> public string ProcedureName { get { return procedurename; } } /// <summary> /// 获取对应参数名称的值 /// </summary> /// <param name="keyname">参数名称</param> /// <returns>object:参数值</returns> public object this [ string keyname] { get { return this .Dictionary[keyname]; } internal set { this .Dictionary[keyname] = value; } } /// <summary> /// 获取所有参数信息 /// </summary> public IDictionary GetProcedureEntity { get { return this .Dictionary; } } /// <summary> /// 存储过程返回的数据集 /// </summary> public DataTable Source { get ; internal set ; } } |
using System; using System.Collections.Generic; using System.Text; using System.Data; using Fast.Orm; namespace Fast.WL.Parmeter { [Serializable()] public class Init_prc_WLOrderTmpAdd : BaseStoredprocedureObject { public Init_prc_WLOrderTmpAdd() { this .procedurename = "prc_WLOrderTmpAdd" ; this .Dictionary.Add( "OrderID" , null ); this .Dictionary.Add( "StationID" , null ); this .Dictionary.Add( "UserID" , null ); this .Dictionary.Add( "UserName" , null ); this .Dictionary.Add( "OrderDate" , null ); this .Dictionary.Add( "DeliveryAddress" , null ); this .Dictionary.Add( "OrderType" , null ); this .Dictionary.Add( "APNumber" , null ); this .Dictionary.Add( "Memo" , null ); this .Dictionary.Add( "ErrID" , DBNull.Value); } public override void AddParameterToBaseParameterObject() { base .parameterlist.Add(IDataParameterFactory.CreateDbDataParameter( "OrderID" , base .Dictionary[ "OrderID" ], ParameterDirection.Input, DbType.String, 14)); base .parameterlist.Add(IDataParameterFactory.CreateDbDataParameter( "StationID" , base .Dictionary[ "StationID" ], ParameterDirection.Input, DbType.String, 36)); base .parameterlist.Add(IDataParameterFactory.CreateDbDataParameter( "UserID" , base .Dictionary[ "UserID" ], ParameterDirection.Input, DbType.String, 36)); base .parameterlist.Add(IDataParameterFactory.CreateDbDataParameter( "UserName" , base .Dictionary[ "UserName" ], ParameterDirection.Input, DbType.String, 10)); base .parameterlist.Add(IDataParameterFactory.CreateDbDataParameter( "OrderDate" , base .Dictionary[ "OrderDate" ], ParameterDirection.Input, DbType.DateTime, 8)); base .parameterlist.Add(IDataParameterFactory.CreateDbDataParameter( "DeliveryAddress" , base .Dictionary[ "DeliveryAddress" ], ParameterDirection.Input, DbType.String, 50)); base .parameterlist.Add(IDataParameterFactory.CreateDbDataParameter( "OrderType" , base .Dictionary[ "OrderType" ], ParameterDirection.Input, DbType.Int16, 2)); base .parameterlist.Add(IDataParameterFactory.CreateDbDataParameter( "APNumber" , base .Dictionary[ "APNumber" ], ParameterDirection.Input, DbType.String, 20)); base .parameterlist.Add(IDataParameterFactory.CreateDbDataParameter( "Memo" , base .Dictionary[ "Memo" ], ParameterDirection.Input, DbType.String, 220)); base .parameterlist.Add(IDataParameterFactory.CreateDbDataParameter( "ErrID" , base .Dictionary[ "ErrID" ], ParameterDirection.Output, DbType.Int32, 4)); } } public class prc_WLOrderTmpAdd : Init_prc_WLOrderTmpAdd { public object OrderID { get { return this .Dictionary[ "OrderID" ] as object ; } set { this .Dictionary[ "OrderID" ] = value; } } public object StationID { get { return this .Dictionary[ "StationID" ] as object ; } set { this .Dictionary[ "StationID" ] = value; } } public object UserID { get { return this .Dictionary[ "UserID" ] as object ; } set { this .Dictionary[ "UserID" ] = value; } } public object UserName { get { return this .Dictionary[ "UserName" ] as object ; } set { this .Dictionary[ "UserName" ] = value; } } public object OrderDate { get { return this .Dictionary[ "OrderDate" ] as object ; } set { this .Dictionary[ "OrderDate" ] = value; } } public object DeliveryAddress { get { return this .Dictionary[ "DeliveryAddress" ] as object ; } set { this .Dictionary[ "DeliveryAddress" ] = value; } } public object OrderType { get { return this .Dictionary[ "OrderType" ] as object ; } set { this .Dictionary[ "OrderType" ] = value; } } public object APNumber { get { return this .Dictionary[ "APNumber" ] as object ; } set { this .Dictionary[ "APNumber" ] = value; } } public object Memo { get { return this .Dictionary[ "Memo" ] as object ; } set { this .Dictionary[ "Memo" ] = value; } } public object ErrID { get { return this .Dictionary[ "ErrID" ] as object ; } set { this .Dictionary[ "ErrID" ] = value; } } } } |
在Init_prc_WLOrderTmpAdd构造函数中我们设置所有的参数名称和默认的值,这里可以会是DbNull.Value。[王清培版权所有,转载请给出署名]
prc_WLOrderTmpAdd ordertmp = new prc_WLOrderTmpAdd(); ordertmp.OrderID = order.OrderID; //订单流水号 ordertmp.StationID = order.StationID; //站点ID ordertmp.UserID = order.UserID; ordertmp.UserName = order.UserName; ordertmp.OrderDate = DBNull.Value; ordertmp.DeliveryAddress = order.DeliveryAddress; ordertmp.OrderType = order.OrderType; ordertmp.APNumber = string .IsNullOrEmpty(order.APNumber) ? DBNull.Value : ( object )order.APNumber; ordertmp.Memo = DBNull.Value; //备注 Fast.Orm.ProcedureHelper.ProcedureOperation<prc_WLOrderTmpAdd>(ordertmp); return int .Parse(ordertmp.ErrID.ToString()) == 0 ? true : false ; |
这样保证我们写的代码都围绕着数据实体来进行数据库的操作。
有了专业的代码生成器之后,一切就变的简单多了,我们按照自己的要求设计开发代码生成器来配合基础框架的使用,那么我们的开发效率将大大提高了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!