• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
vs之bug
博客园    首页    新随笔    联系   管理    订阅  订阅

使用自定义异常简化数据验证

    以前在接受用户输入参数做验证判断时一直用 if else 语句,十分繁琐不胜其烦。

            if (requestData.MemberId == 0)
            {
                resultData.Code = ResponseCode.Pram_MemberId.ToString();
                resultData.Message = ResponseCodeEntity.GetCodeDes(ResponseCode.Pram_MemberId);
                return;
            }
            if (requestData.SerialId == "")
            {
                resultData.Code = ResponseCode.Pram_SerialId.ToString();
                resultData.Message = ResponseCodeEntity.GetCodeDes(ResponseCode.Pram_SerialId);
                return;
            }
            if (requestData.ProductType == 0)
            {
                resultData.Code = ResponseCode.Pram_ProductType.ToString();
                resultData.Message = ResponseCodeEntity.GetCodeDes(ResponseCode.Pram_ProductType);
                return;
            }

每处判断失败后都要return,有些情况下还要将参数传递给封装方法,再判断方法的返回值。

也就是因为这个 return,使得我觉得这些代码很难改动,只能用 if 一行一行写下去。

后来经同事提醒,想到抛出自定义异常的方式来处理,因为异常可以中断程序,在外层捕获,这样就解决了return的问题。

先看看效果

        rqt.Valid(p => p.AloneId == 0, BDInterfaceCode.Pram_AloneId)
                .Valid(p => string.IsNullOrEmpty(p.Passwd), BDInterfaceCode.Pram_Passwd)
                .Valid(p => string.IsNullOrEmpty(p.Mobile) && string.IsNullOrEmpty(p.Email), BDInterfaceCode.Pram_MobileAndEmail)
                .Valid(p => !string.IsNullOrEmpty(p.Mobile) && !ValidateHelper.IsMobile(p.Mobile), BDInterfaceCode.Pram_Mobile)
                .Valid(p => !string.IsNullOrEmpty(p.Email) && !ValidateHelper.IsMail(p.Email), BDInterfaceCode.Pram_Email);

是不是看起来清爽了很多。

首先要定义一个自定义异常,根据自己的需要定义字段,我这里定义了错误码和错误信息。BDInterfaceCode 是一个错误码的枚举,params string[] des 用于添加额外的信息。

    public class ValidException : Exception
    {public string BdCode;
        public new string Message;

        public ValidException(BDInterfaceCode bdcode, params string[] des)
        {
            BdCode = bdcode.GetCodeValue();
            Message = bdcode.GetCodeDes();
if (des != null && des.Length > 0 && des[0] != null)
            {
                Message += "-" + des[0];
            }
}
}
然后定义扩展方法,如果验证失败就抛出这个异常。
    public static class BaseBDUtils
    {
        public static RQT Valid<RQT>(this RQT rqt, Func<RQT, bool> pridicate, BDInterfaceCode bdcode, params string[] des) 
        {
            if (pridicate(rqt))
            {
                throw new ValidException(bdcode, des);
            }
            else
            {
                return rqt;
            }
        }
}

最后就是在调用的时候捕捉异常了

            try
            {
                request.Valid(p => string.IsNullOrEmpty( p.Pwd), BDInterfaceCode.Pram_Pwd)
.Valid(p
=> string.IsNullOrEmpty( p.BdData), BDInterfaceCode.Pram_BdData,"用户输入数据不能为空"); } catch (ValidException ex) { //处理自定义异常 log(ex.Message); } catch (Exception ex) { //todo:系统异常 }

这个方法简单易行,很适合小项目。但如果逻辑复杂一点,比如在最外层异常捕获之前还有try catch 就有可能出问题。

 

如果你有更好的技巧,欢迎交流



 

 

 

 

posted @ 2013-11-28 10:54  vs_bug  阅读(1238)  评论(1)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3