程序员的自我救赎---1.4.1: 核心框架讲解(DAL)
Posted 程序员的自我救赎
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了程序员的自我救赎---1.4.1: 核心框架讲解(DAL)相关的知识,希望对你有一定的参考价值。
《核心框架讲解》
之前在《Winner2.0框架解决方案命分层规范》 有讲到过Winner框架最重要的三个程序集分别是:
DataAcces: 继承 “DataAccessBase”
Entities: 没有单独可基础的核心框架类,可直接引用工具类
Facade: 继承:“FacadeBase”
APP(MVC): 继承:“TopControllerBase”
我们重点讲一下,“DataAccessBase”,“FacadeBase”,“TopControllerBase” 这三个基类。
其实前面在 《代码生成器的使用》和《事务的使用》两篇文章中就分别讲到过“DataAccessBase” 和 "FacadeBase" 。
我们先来分析DataAccessBase我们看一下在我们真实开发中关于数据库访问代码的的使用。建议复制到VS中去看,不然在博客中确实不好看懂。
/***********************代码生成器生成的 充值表数据操作************ * * Data Access Layer Of Winner Framework * FileName : Tdct_Recharge.generate.cs * Version : V 1.1.0 * Author:架构师 曾杰(Jie) * E_Mail : 6e9e@163.com * Tencent QQ:554044818 * Blog : http://www.cnblogs.com/fineblog/ * CreateTime : 2017-10-11 18:06:04 * ***************************************************/ using System; using System.Collections.Generic; using System.Data; using System.Text; using Winner.Framework.Core; using Winner.Framework.Core.DataAccess; using Winner.Framework.Core.DataAccess.Oracle; using Winner.Framework.Utils; using Winner.DCTS.Entities; namespace Winner.DCTS.DataAccess { /// <summary> /// Data Access Layer Object Of Tdct_Recharge /// </summary> public partial class Tdct_Recharge : DataAccessBase { #region 默认构造 public Tdct_Recharge() : base() { } public Tdct_Recharge(DataRow dataRow) : base(dataRow) { } #endregion 默认构造 #region 对应表结构的常量属性 public const string _RECHARGE_ID="RECHARGE_ID"; public const string _PAY_TYPE="PAY_TYPE"; public const string _AMOUNT="AMOUNT"; public const string _ACCOUNT="ACCOUNT"; public const string _USER_ID="USER_ID"; public const string _STATUS="STATUS"; public const string _CREATE_TIME="CREATE_TIME"; public const string _REMARKS="REMARKS"; public const string _TABLENAME="Tdct_Recharge"; #endregion 对应表结构的常量属性 #region 公开属性 /// <summary> /// 充值记录ID /// [default:0] /// </summary> public int RechargeId { get { return Convert.ToInt32(DataRow[_RECHARGE_ID]); } set { setProperty(_RECHARGE_ID,value); } } /// <summary> /// 支付类型 1支付宝,2银行卡,3微信 /// [default:0] /// </summary> public int PayType { get { return Convert.ToInt32(DataRow[_PAY_TYPE]); } set { setProperty(_PAY_TYPE,value); } } /// <summary> /// 充值金额 /// [default:0] /// </summary> public decimal Amount { get { return Convert.ToDecimal(DataRow[_AMOUNT]); } set { setProperty(_AMOUNT,value); } } /// <summary> /// 充值账号 /// [default:string.Empty] /// </summary> public string Account { get { return DataRow[_ACCOUNT].ToString(); } set { setProperty(_ACCOUNT,value); } } /// <summary> /// 充值用户 /// [default:0] /// </summary> public int UserId { get { return Convert.ToInt32(DataRow[_USER_ID]); } set { setProperty(_USER_ID,value); } } /// <summary> /// 充值状态 0等待银行到账,1已经到账,2操作失败 /// [default:0] /// </summary> public RechargeStatus Status { get { return (RechargeStatus)Convert.ToInt32(DataRow[_STATUS]); } set { setProperty(_STATUS,(int)value); } } /// <summary> /// 充值时间 /// [default:new DateTime()] /// </summary> public DateTime CreateTime { get { return Convert.ToDateTime(DataRow[_CREATE_TIME].ToString()); } } /// <summary> /// 备注 /// [default:string.Empty] /// </summary> public string Remarks { get { return DataRow[_REMARKS].ToString(); } set { setProperty(_REMARKS,value); } } #endregion 公开属性 #region 私有成员 protected override string TableName { get { return _TABLENAME; } } protected override DataRow BuildRow() { DataTable dt = new DataTable(_TABLENAME); dt.Columns.Add(_RECHARGE_ID, typeof(int)).DefaultValue = 0; dt.Columns.Add(_PAY_TYPE, typeof(int)).DefaultValue = 0; dt.Columns.Add(_AMOUNT, typeof(decimal)).DefaultValue = 0; dt.Columns.Add(_ACCOUNT, typeof(string)).DefaultValue = string.Empty; dt.Columns.Add(_USER_ID, typeof(int)).DefaultValue = 0; dt.Columns.Add(_STATUS, typeof(int)).DefaultValue = 0; dt.Columns.Add(_CREATE_TIME, typeof(DateTime)).DefaultValue = new DateTime(); dt.Columns.Add(_REMARKS, typeof(string)).DefaultValue = string.Empty; return dt.NewRow(); } #endregion 私有成员 #region 常用方法 protected bool DeleteByCondition(string condition) { string sql = @"DELETE FROM TDCT_RECHARGE WHERE " + condition; return base.DeleteBySql(sql); } public bool Delete(int rechargeId) { string condition = "RECHARGE_ID=:RECHARGE_ID"; AddParameter(_RECHARGE_ID, rechargeId); return DeleteByCondition(condition); } public bool Delete() { string condition = "RECHARGE_ID=:RECHARGE_ID"; AddParameter(_RECHARGE_ID, RechargeId); return DeleteByCondition(condition); } public bool Insert() { RechargeId=base.GetSequence("SELECT SEQ_TDCT_RECHARGE.NEXTVAL FROM DUAL"); string sql=@"INSERT INTO TDCT_RECHARGE( RECHARGE_ID, PAY_TYPE, AMOUNT, ACCOUNT, USER_ID, STATUS, REMARKS) VALUES( :RECHARGE_ID, :PAY_TYPE, :AMOUNT, :ACCOUNT, :USER_ID, :STATUS, :REMARKS)"; AddParameter(_RECHARGE_ID,DataRow[_RECHARGE_ID]); AddParameter(_PAY_TYPE,DataRow[_PAY_TYPE]); AddParameter(_AMOUNT,DataRow[_AMOUNT]); AddParameter(_ACCOUNT,DataRow[_ACCOUNT]); AddParameter(_USER_ID,DataRow[_USER_ID]); AddParameter(_STATUS,DataRow[_STATUS]); AddParameter(_REMARKS,DataRow[_REMARKS]); return base.InsertBySql(sql); } public bool Update() { return UpdateByCondition(string.Empty); } protected bool UpdateByCondition(string condition) { //移除主键标记 ChangePropertys.Remove(_RECHARGE_ID); if (ChangePropertys.Count == 0) { return true; } StringBuilder sql = new StringBuilder(); sql.AppendLine("UPDATE TDCT_RECHARGE SET"); while (ChangePropertys.MoveNext()) { sql.AppendFormat(" {0}{1}=:{1} ", (ChangePropertys.CurrentIndex == 0 ? string.Empty : ","), ChangePropertys.Current); AddParameter(ChangePropertys.Current, DataRow[ChangePropertys.Current]); } sql.AppendLine(" WHERE RECHARGE_ID=:RECHARGE_ID"); AddParameter(_RECHARGE_ID, DataRow[_RECHARGE_ID]); if (!string.IsNullOrEmpty(condition)) sql.AppendLine(" AND " + condition); bool result = base.UpdateBySql(sql.ToString()); ChangePropertys.Clear(); return result; } protected bool SelectByCondition(string condition) { string sql = @" SELECT RECHARGE_ID, PAY_TYPE, AMOUNT, ACCOUNT, USER_ID, STATUS, CREATE_TIME, REMARKS FROM TDCT_RECHARGE WHERE " + condition; return base.SelectBySql(sql); } public bool SelectByPK(int rechargeId) { string condition = "RECHARGE_ID=:RECHARGE_ID"; AddParameter(_RECHARGE_ID, rechargeId); return SelectByCondition(condition); } public Tnet_User Get_Tnet_User_ByUserId() { Tnet_User da=new Tnet_User(); da.SelectByPK(UserId); return da; } #endregion 常用方法 //提示:此类由代码生成器生成,如无特殊情况请不要更改。如要扩展请在外部同名类中扩展 } /// <summary> /// Data Access Layer Object Collection Of Tdct_Recharge /// </summary> public partial class Tdct_RechargeCollection : DataAccessCollectionBase { #region 默认构造 public Tdct_RechargeCollection() { } public Tdct_RechargeCollection(DataTable table) : base(table) { } #endregion 默认构造 #region 私有成员 protected override DataAccessBase GetItemByIndex(int index) { return new Tdct_Recharge(DataTable.Rows[index]); } protected override DataTable BuildTable() { return new Tdct_Recharge().CloneSchemaOfTable(); } protected override string TableName { get { return Tdct_Recharge._TABLENAME; } } protected bool ListByCondition(string condition) { string sql = @" SELECT RECHARGE_ID, PAY_TYPE, AMOUNT, ACCOUNT, USER_ID, STATUS, CREATE_TIME, REMARKS FROM TDCT_RECHARGE WHERE " + condition; return base.ListBySql(sql); } public bool ListByRechargeId(int rechargeId) { string condition = "RECHARGE_ID=:RECHARGE_ID"; AddParameter(Tdct_Recharge._RECHARGE_ID, rechargeId); return ListByCondition(condition); } public bool ListAll() { string condition = "1=1"; return ListByCondition(condition); } public bool DeleteByCondition(string condition) { string sql = "DELETE FROM TDCT_RECHARGE WHERE " + condition; return DeleteBySql(sql); } #endregion #region 公开成员 public Tdct_Recharge this[int index] { get { return new Tdct_Recharge(DataTable.Rows[index]); } } public bool DeleteAll() { return this.DeleteByCondition(string.Empty); } #region Linq public Tdct_Recharge Find(Predicate<Tdct_Recharge> match) { foreach (Tdct_Recharge item in this) { if (match(item)) return item; } return null; } public Tdct_RechargeCollection FindAll(Predicate<Tdct_Recharge> match) { Tdct_RechargeCollection list = new Tdct_RechargeCollection(); foreach (Tdct_Recharge item in this) { if (match(item)) list.Add(item); } return list; } public bool Contains(Predicate<Tdct_Recharge> match) { foreach (Tdct_Recharge item in this) { if (match(item)) return true; } return false; } public bool DeleteAt(Predicate<Tdct_Recharge> match) { BeginTransaction(); foreach (Tdct_Recharge item in this) { item.ReferenceTransactionFrom(Transaction); if (!match(item)) continue; if (!item.Delete()) { Rollback(); return false; } } Commit(); return true; } #endregion Linq #endregion //提示:此类由代码生成器生成,如无特殊情况请不要更改。如要扩展请在外部同名类中扩展 } }
/***********************由技术员编写**************************** * * Data Access Layer Of Winner Framework * FileName : Tdct_Recharge.extension.cs * Version : V 1.1.0 * Author:架构师 曾杰(Jie) * E_Mail : 6e9e@163.com * Tencent QQ:554044818 * Blog : http://www.cnblogs.com/fineblog/ * CreateTime : 2017-10-11 17:55:39 * ***************************************************/ using System; using System.Collections.Generic; using System.Data; using System.Text; using Winner.Framework.Core; using Winner.Framework.Core.DataAccess; using Winner.Framework.Core.DataAccess.Oracle; using Winner.Framework.Utils; using Winner.DCTS.Entities; using Winner.DCTS.Entities.enums; namespace Winner.DCTS.DataAccess { /// <summary> /// Data Access Layer Object Of Tdct_Recharge /// </summary> public partial class Tdct_Recharge : DataAccessBase { //Custom Extension Class public bool Delete(int rechargeId, int userId) { string condition = "RECHARGE_ID=:RECHARGE_ID AND USER_ID=:USER_ID AND STATUS=0"; AddParameter(_RECHARGE_ID, rechargeId); AddParameter(_USER_ID, userId); return DeleteByCondition(condition); } public bool SelectById(int rechargeId) { string condition = "RECHARGE_ID=:RECHARGE_ID AND STATUS=:STATUS"; AddParameter(_RECHARGE_ID, rechargeId); AddParameter(_STATUS, RechargeStatus.等待充值.GetHashCode()); return SelectByCondition(condition); } } /// <summary> /// Data Access Layer Object Collection Of Tdct_Recharge /// </summary> public partial class Tdct_RechargeCollection : DataAccessCollectionBase { public bool ListByUserId(int userId) { string condition = " USER_ID = :USER_ID ORDER BY RECHARGE_ID DESC "; AddParameter("USER_ID", userId); return ListByCondition(condition); } /// <summary> /// 查询列表 /// </summary> /// <param name="status"></param> /// <param name="paytype"></param> /// <returns></returns> public bool ListByKeyWord(string keyword,int status,int paytype) { string sql = "SELECT TR.*,VU.USER_CODE,VU.USER_NAME FROM TDCT_RECHARGE TR JOIN VNET_USER VU ON TR.USER_ID=VU.USER_ID WHERE 1=1 "; if(!string.IsNullOrEmpty(keyword)) { sql += " AND( TR.ID=:ID"; sql += " OR VU.USER_NAME=:USERNAME)"; AddParameter("ID", keyword); AddParameter("USERNAME", keyword); } if (status>=0) { sql += " AND TR.STATUS=:STATUS"; AddParameter("STATUS", status); } if(paytype>=0) { sql += " AND TR.PAY_TYPE=:PAYTYPE"; AddParameter("PAYTYPE",paytype); } sql += " ORDER BY TR.RECHARGE_ID DESC"; return ListBySql(sql); } //Custom Extension Class } }
对于项目结构不理解的需要先看《Winner2.0框架解决方案命分层规范》 才能看懂,为什么我们会这样写。 我想了想关于DataAccessBase,我们还是又浅入深的去讲。
先从最最简单的 AddParameter() 这个方法开始讲起,明眼一看就知道这是一个 “参数化” 传值,至于参数化传值有什么好处就自行百度吧,我看到很多公司的项目本身可以封装
一层参数化方法,但是却“痴迷”于用 存储过程 却解决参数化问题,然后放大存储过程“预编译”的优势,其实这个没有必要。无论是哪种数据库都是支持C# 参数化 操作的。
我们看看AddParameter() 这个方法是怎么实现的:
#region AddParameter /// <summary> /// 添加DAO.NET的SqlCommand命令对象参数 /// </summary> /// <param name="parameterName">参数名称</param> /// <param name="value">参数值</param> protected DbParameter AddParameter(string parameterName, object value) { if (string.IsNullOrEmpty(parameterName)) { throw new Exception("传入的参数名称为空"); } if (string.IsNullOrEmpty(DBFactory.ParameterSign)) { throw new Exception("未指定参数符合(例如:Oracle::、SqlServer:@....)"); } if (!parameterName.StartsWith(this.DBFactory.ParameterSign)) { parameterName = this.DBFactory.ParameterSign + parameterName; } DbParameter parm = DBFactory.DBProvider.CreateParameter(); parm.ParameterName = parameterName; parm.Value = value; AddParameter(parm); return parm; } /// <summary> /// 添加DAO.NET的SqlCommand命令对象参数 /// </summary> /// <param name="parameterName">参数名称</param> /// <param name="direction">参数模式</param> /// <param name="func">处理返回值函数</param> /// <example> /// <code> /// <![CDATA[ /// //接收的参数 /// int id; /// AddParameter("Id", ParameterDirection.ReturnValue, value => id = value.ConvertTo<int>()); /// ]]> /// </code> /// </example> protected DbParameter AddParameter(string parameterName, ParameterDirection direction, Func<DbParameter, object> func) { if (string.IsNullOrEmpty(parameterName)) { throw new Exception("传入的参数名称为空"); } if (string.IsNullOrEmpty(DBFactory.ParameterSign)) { throw new Exception("未指定参数符合(例如:Oracle::、SqlServer:@....)"); } if (!parameterName.StartsWith(this.DBFactory.ParameterSign)) { parameterName = this.DBFactory.ParameterSign + parameterName; } DbParameter parm = DBFactory.DBProvider.CreateParameter(); parm.ParameterName = parameterName; parm.Direction = direction; parm.Value = string.Empty; parm.Size = 2000; AddParameter(parm); Parameters.OutValuesParamerer.Add(parm, func); return parm; } /// <summary> /// 添加DAO.NET的SqlCommand命令对象参数 /// </summary> /// <param name="parameterName">参数名称</param> /// <param name="func">处理返回值函数</param> /// <example> /// <code> /// <![CDATA[ /// //接收的参数 /// int id; 程序员的自我救赎---7.1:权限系统讲解