zoukankan      html  css  js  c++  java
  • c# 调用服务返回结果模板化

    一般我们返回一个结果,主要有返回值,执行结果信息,所以定义一个类

     public  class QuestResult
        {

            /// <summary>
            /// 返回值
            /// </summary>
            public object Result { get; set; }

           /// <summary>
           /// 结果编码
           /// </summary>
            public ErrorCode Error { get; set; }

           /// <summary>
           /// 结果编码描述
           /// </summary>
            public string ErrorMsg { get; set; }

            /// <summary>
            /// 结果附近信息
            /// 主要是异常信息或者其它描述
            /// 例如:结果被截取
            /// </summary>
            public string ReslutMsg { get; set; }


        }

    对于返回的结果编码,采用枚举型方便扩展,而且可以为每个结对应的固定一个描述信息

    public enum ErrorCode
        {

            /// <summary>
            /// 成功
            /// </summary>
            /// 
            [Description("执行成功")]
            Sucess,

            /// <summary>
            ///执行超时
            /// </summary>
            /// 
            [Description("执行超时")]
            TimeOut,

            /// <summary>
            /// 执行异常
            /// </summary>
            /// 
            [Description("执行异常")]
            Exception,

            /// <summary>
            /// 结果被截取
            /// </summary>
            /// 
            [Description("结果被截取")]
            Truncate,

        }

    再启用一个扩展方法,获取枚举的描述信息

      /// </summary>
       public static class CommonExtend
        {
            /// <summary>
            /// 枚举描述
            /// </summary>
            private static Dictionary<string, string> dicEnum = new Dictionary<string, string>();
            /// <summary>
            /// 枚举描述特性获取信息
            /// </summary>
            /// <param name="value">枚举</param>
            /// <param name="isNameInstend">没有特性时是否直接使用字段名称</param>
            /// <returns></returns>
            public static string EnumDescription(this Enum value,bool isNameInstend=false)
            {
                Type type = value.GetType();
                string name = Enum.GetName(type, value);
                if (name == null)
                {
                    return null;
                }
                string description = "";
                if (dicEnum.TryGetValue(type.FullName+"_"+name,out description))
                {
                    return description;
                }
                FieldInfo field = type.GetField(name);
                DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
                if (attribute == null && isNameInstend == true)
                {
                    return name;
                }
                //
                if(attribute!=null)
                {
                    dicEnum[type.FullName + "_" + name] = attribute.Description;
                }
                return attribute == null ? null : attribute.Description;
            }
        }

    这样就方便获取所有信息了,结果类中定义的ErrorMsg不是编码描述,而是在执行错误后的Exception信息。

    这样一个类似标准的返回结果模板就差不多了。

  • 相关阅读:
    5. Mybatis UPDATE更新,DELETE删除
    3. Mybatis Insert
    4. selectKey语句属性配置细节
    2. Mybatis Select
    uoj#282. 长度测量鸡(构造)
    uoj#276. 【清华集训2016】汽水(分数规划+点分治)
    uoj#275. 【清华集训2016】组合数问题(数位dp)
    uoj#274. 【清华集训2016】温暖会指引我们前行(LCT)
    uoj#273. 【清华集训2016】你的生命已如风中残烛(组合数学)
    uoj#272. 【清华集训2016】石家庄的工人阶级队伍比较坚强(矩阵+三维FWT)
  • 原文地址:https://www.cnblogs.com/jinyu20180311/p/10312362.html
Copyright © 2011-2022 走看看