感悟:
写了一天的代码,发现程序员的一天真的好累。
第一次用设计模式,发现其实设计模式的作用是让代码尽可能做到高内聚低耦合,但是对于一些简单的系统,的确是提高了一些代码量,不过以后系统的功能复杂了,设计模式应该还是非常好用的。
缺陷:
下面的代码只是实现了逻辑,但是并没有进行文本框的判空。
BLL层
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HanlderRegister;
using System.Data;
namespace BLL
{
public class ReChargeBLL
{
//实例化工厂
Factory.ReChargeFactory fact = new Factory.ReChargeFactory();
public void rechargeBll(Entity.ReChargeEntity ReCharge)
{
//实现接口
IDAL.IReCharge idel = fact.ReCharge();
//查询用户是否存在
DataTable TableUserNameResult = idel.SelectUserName(ReCharge);
//查询基础金额
List<Entity.BasicEntity> ListBasic = idel.SelectBasic();
bool flag=false;
if (flag==false)
{
//判断用户是否存在
if (TableUserNameResult.Rows.Count != 1)
{
flag = true;
throw new NotFiniteNumberException("您输入的用户名不存在,请重新输入");
}
}
if (flag==false)
{
//判断是否大于最小充值金额
if (ReCharge.AddMoney < ListBasic[0].LimitCash)
{
flag = true;
throw new NotFiniteNumberException(string.Format("您充值的金额必须大于最小充值金额:{0}", ListBasic[0].LimitCash));
}
}
if (flag==false)
{
#region 实例化user,并传值
Entity.UserEntity User = new Entity.UserEntity();
User.UserName = ReCharge.UserName;//用户名
#endregion
//查询原有金额
List<Entity.UserEntity> ListUser = idel.SelectUserMoney(User);
//计算出最新的金额
User.Money = ListUser[0].Money + ReCharge.AddMoney;
//更新user表中的金额
int i = idel.UpdateUser(User);
//向冲值表插入信息
int j = idel.InsertReCharge(ReCharge);
}
}
}
}
接口层:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace IDAL
{
public interface IReCharge
{
//查询用户是否存在
DataTable SelectUserName(Entity.ReChargeEntity ReCharge);
//查询基础数据
List<Entity.BasicEntity> SelectBasic();
//向Recharge表中插入数据
int InsertReCharge(Entity.ReChargeEntity ReCharge);
//查询User原本的钱
List<Entity.UserEntity> SelectUserMoney(Entity.UserEntity User);
//更新User表中的金额
int UpdateUser(Entity.UserEntity User);
}
}
D层
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
public class ReChargeDAL:IDAL.IReCharge
{
//实例化SQLHelper
SqlHelper sqlHelper = new SqlHelper();
//实例化一个表
DataTable table = new DataTable();
DAL.CovertListHelper tolist = new CovertListHelper();
/// <summary>
/// 查询UserName是否存在
/// </summary>
/// <param name="ReCharge"></param>
/// <returns></returns>
public DataTable SelectUserName(Entity.ReChargeEntity ReCharge)
{
//防止SQL注入
SqlParameter[] sqlParameter = {new SqlParameter("@UserName",ReCharge.UserName) };
//sql语句
string sql = "Select username From user_info Where username=@UserName";
//调用方法,返回行数
table=sqlHelper.ExecuteQuery(sql, sqlParameter, CommandType.Text);
//返回到B层
return table;
}
/// <summary>
/// 查询最少充值金额
/// </summary>
/// <returns></returns>
public List<Entity.BasicEntity> SelectBasic()
{
//SQL语句
string sql = "Select top 1 limitcash from basicdata_info order by datetime desc";
//调用方法,查询信息
table=sqlHelper.ExecuteQuery(sql, CommandType.Text);
//转换为泛型
List < Entity.BasicEntity > list = tolist.convertToList<Entity.BasicEntity>(table);
//返回到B层
return list;
}
/// <summary>
/// 插入用户充值金额
/// </summary>
/// <param name="Recharge"></param>
/// <returns></returns>
public int InsertReCharge(Entity.ReChargeEntity Recharge)
{
SqlParameter[] sqlParameter =
{
new SqlParameter("@UserName",Recharge.UserName),
new SqlParameter("@AddDateTime",Recharge.AddDateTime),
new SqlParameter("@AddMoney",Recharge.AddMoney),
new SqlParameter("@CreateName",Recharge.CreateName),
new SqlParameter("@Status",Recharge.Status)
};
//sql语句
string sql = "Insert into ReCharge_info values(@UserName,@AddDateTime,@AddMoney,@CreateName,@Status);";
//调用方法,返回受影响的行数
int i=sqlHelper.ExecteNonQuery(sql, sqlParameter, CommandType.Text);
return i;
}
/// <summary>
/// 查询原先的余额
/// </summary>
/// <param name="User"></param>
/// <returns></returns>
public List<Entity.UserEntity> SelectUserMoney(Entity.UserEntity User)
{
//防止sql注入
SqlParameter[] sqlParameter = { new SqlParameter("@UserName",User.UserName),new SqlParameter("@Money",User.Money )};
//SQL语句
string sql = "Select money From User_info Where username=@UserName";
table = sqlHelper.ExecuteQuery(sql, sqlParameter, CommandType.Text);
List<Entity.UserEntity> list = tolist.convertToList<Entity.UserEntity>(table);
return list;
}
/// <summary>
/// 更新user的余额
/// </summary>
/// <param name="User"></param>
/// <returns></returns>
public int UpdateUser(Entity.UserEntity User)
{
SqlParameter[] sqlParameter = { new SqlParameter("@UserName",User.UserName),new SqlParameter("@Money",User.Money)};
string sql = "Update user_info set money=@Money Where username=@UserName";
int i=sqlHelper.ExecteNonQuery(sql, sqlParameter, CommandType.Text);
return i;
}
}
}
职责链:
抽象类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HanlderRegister
{
public abstract class RegisterHanlder
{
protected RegisterHanlder _successor;
public void SetSuccessor(RegisterHanlder Successor)
{
this._successor = Successor;
}
public abstract void HandleRequest(Entity.UserEntity User,int ResultRow,int LimitCash);
}
}
判断两次密码是否一致
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HanlderRegister
{
public class PWDSameHandler: RegisterHanlder
{
/// <summary>
/// 判断两次密码是否一致
/// </summary>
/// <param name="User"></param>
/// <param name="ResultRow"></param>
/// <param name="LimitCash"></param>
public override void HandleRequest(Entity.UserEntity User,int ResultRow,int LimitCash)
{
if (!(User.PassWord0 == User.PassWord1))//如果密码相同
{
throw new NotImplementedException("两次输入密码不同,请重新输入");
}
else if (_successor != null)
{
_successor.HandleRequest(User,ResultRow,LimitCash);
}
}
}
}
判断用户名是否存在
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Entity;
namespace HanlderRegister
{
public class UserNameUsing: RegisterHanlder
{
/// <summary>
/// 判断用户名是否被使用
/// </summary>
/// <param name="User"></param>
/// <param name="ResultRow"></param>
/// <param name="LimitCash"></param>
public override void HandleRequest(UserEntity User,int ResultRow,int LimitCash)
{
if (ResultRow==1)
{
throw new NotImplementedException("该用户名已经存在,请重新输入");
}
else if(_successor!=null)
{
_successor.HandleRequest(User, ResultRow, LimitCash);
}
}
}
}
判断是否小于最小金额
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Entity;
namespace HanlderRegister
{
public class LimitCashHandler:RegisterHanlder
{
/// <summary>
/// 判断是否大于最小金额
/// </summary>
/// <param name="User"></param>
/// <param name="ResultRow"></param>
/// <param name="LimitCash"></param>
public override void HandleRequest(UserEntity User, int ResultRow, int LimitCash)
{
if (User.Money<LimitCash)
{
throw new NotImplementedException( string.Format("你输入的金额必须大于最低充值金额{0}",LimitCash.ToString()));
}
}
}
}
充值+职责链的核心是:
1、在D层的帮助下,B层提前准备好需要判断的数据
2、把数据放到职责链的参数中,然后通过职责链进行判断,
3、判断成功,给出提示成功,否则利用try…catch…语句进行抛出错误。
第一次设计模式,感觉还行,就是突然发现代码里好多啊,但是只是因为自己做的这个系统功能不够强大,如果功能多,可以整理出一个泛能的职责链,然后就可以解决一类问题,降低了代码的冗余。