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


        以前在接受用户输入参数做验证判断时一直用 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 就有可能出问题。

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



  • 相关阅读:
    calcite介绍
    kylin介绍
    hbase(三)coprocessor
    流式计算-窗口
    实验室服务器琐事
    流畅的python笔记
    语义分割相关网络简述
    leetcode 696
    树的非递归遍历
    leetcode 665
  • 原文地址:https://www.cnblogs.com/vs-bug/p/3447264.html
Copyright © 2020-2023  润新知