• 插件化编程之WebAPI统一返回模型


    WebApi返回数据我们一般包裹在一个公共的模型下面的,而不是直接返回最终数据,在返回参数中,显示出当前请求的时间戳,是否请求成功,如果错误那么错误的消息是什么,状态码(根据业务定义的值)等等。我们常规的返回值常需要如下字段,为此我们可以先定义IResultDto 接口
    复制代码
    /// 
        /// 结果模型
        /// 
        public interface IResultDto
        {
            /// 
            /// 成功
            /// 
            bool Successful { get; set; }
    
            /// 
            /// 返回标识码
            /// 
            int Code { get; set; }
    
            /// 
            /// 操作码(用于幂等性验证,每次请求都会返回一个新的,以便用户下次更新使用)
            /// 
            string OpCode { get; set; }
    
            /// 
            /// 消息
            /// 
            string Msg { get; set; }
    
    
            /// 
            /// 常规数据
            /// 
            dynamic Data { get; set; }
    
        }

    /// 
        /// 结果模型
        /// 
        public interface IResultDto
        {
            /// 
            /// 成功
            /// 
            bool Successful { get; set; }
    
            /// 
            /// 返回标识码
            /// 
            int Code { get; set; }
    
            /// 
            /// 操作码(用于幂等性验证,每次请求都会返回一个新的,以便用户下次更新使用)
            /// 
            string OpCode { get; set; }
    
            /// 
            /// 消息
            /// 
            string Msg { get; set; }
    
    
            /// 
            /// 常规数据
            /// 
            dynamic Data { get; set; }
    
        }
    复制代码

       对应的还接口的实现如下,为了更好的独立性,在这里实现了成功和失败赋值方法。

    复制代码
    /// 
        /// 返回模型
        /// 
        public class ResultDto : IResultDto
        {
            /// 
            /// 返回标识码
            /// 
            public int Code { get; set; }
    
            /// 
            /// 成功
            /// 
            [JsonIgnore]
            public bool Successful { get; set; }
    
            /// 
            /// 消息
            /// 
            public string Msg { get; set; }
    
    
            /// 
            /// 操作码(用于幂等性验证,每次请求都会返回一个新的,以便用户下次更新使用)
            /// 
            public string OpCode { get; set; }
    
            /// 
            /// 常规数据
            /// 
            public dynamic Data { get; set; }
    
    
            /// 
            /// 成功
            /// 
            /// 
            public ResultDto Success(string msg = "success")
            {
                this.Code = (int)EnumCode.Succeed;
                this.Successful = true;
                this.Msg = msg;
                return this;
            }
    
            /// 
            /// 成功
            /// 
            /// 
            /// 
            /// 
            public ResultDto Success(dynamic data = default, string msg = "success")
            {
                this.Code = (int)EnumCode.Succeed;
                this.Successful = true;
                this.Data = data;
                this.Msg = msg;
                return this;
            }
    
            /// 
            /// 成功
            /// 
            /// 
            /// 
            /// 
            public ResultDto Success(dynamic data = default, dynamic data2 = default, string msg = "success")
            {
                this.Code = (int)EnumCode.Succeed;
                this.Successful = true;
                this.Data = data;
                this.Msg = msg;
                return this;
            }
    
            /// 
            /// 失败
            /// 
            /// 
            /// 说明
            public ResultDto Failed(int code = default, string msg = "failed")
            {
                this.Code = code;
                this.Successful = false;
                this.Msg = msg;
                return this;
            }
    
    
        }

    /// 
        /// 返回模型
        /// 
        public class ResultDto : IResultDto
        {
            /// 
            /// 返回标识码
            /// 
            public int Code { get; set; }
    
            /// 
            /// 成功
            /// 
            [JsonIgnore]
            public bool Successful { get; set; }
    
            /// 
            /// 消息
            /// 
            public string Msg { get; set; }
    
    
            /// 
            /// 操作码(用于幂等性验证,每次请求都会返回一个新的,以便用户下次更新使用)
            /// 
            public string OpCode { get; set; }
    
            /// 
            /// 常规数据
            /// 
            public dynamic Data { get; set; }
    
    
            /// 
            /// 成功
            /// 
            /// 
            public ResultDto Success(string msg = "success")
            {
                this.Code = (int)EnumCode.Succeed;
                this.Successful = true;
                this.Msg = msg;
                return this;
            }
    
            /// 
            /// 成功
            /// 
            /// 
            /// 
            /// 
            public ResultDto Success(dynamic data = default, string msg = "success")
            {
                this.Code = (int)EnumCode.Succeed;
                this.Successful = true;
                this.Data = data;
                this.Msg = msg;
                return this;
            }
    
            /// 
            /// 成功
            /// 
            /// 
            /// 
            /// 
            public ResultDto Success(dynamic data = default, dynamic data2 = default, string msg = "success")
            {
                this.Code = (int)EnumCode.Succeed;
                this.Successful = true;
                this.Data = data;
                this.Msg = msg;
                return this;
            }
    
            /// 
            /// 失败
            /// 
            /// 
            /// 说明
            public ResultDto Failed(int code = default, string msg = "failed")
            {
                this.Code = code;
                this.Successful = false;
                this.Msg = msg;
                return this;
            }
    
    
        }
    复制代码

     我们尝试返回该类型结果

     return ResultTo.Success(tokenDto, "登录成功");

     通过swagger文档我们看下返回效果

      细心的你是不是发现了啥,我们的接口文档他不认识我们返回类型的实体呀,还玩啥,别急我们可以再稍微扩展下,加个泛型的返回,看起来就完美了。

    复制代码
       /// 
        /// 泛型结果模型
        /// 
        /// 
        public interface IResultDto : IResultDto  
        {
            /// 
            /// 常规数据
            /// 
            new T Data { get; set; }
    
        }

        /// 
        /// 泛型结果模型
        /// 
        /// 
        public interface IResultDto : IResultDto  
        {
            /// 
            /// 常规数据
            /// 
            new T Data { get; set; }
    
        }
    复制代码

    我们实现泛型接口如下,同样在这里实现了成功和失败赋值方法。

    复制代码
    /// 
        /// 泛型类 返回模型
        /// 
        /// 
        public class ResultDto : ResultDto, IResultDto
        {
            /// 
            /// 常规数据
            /// 
            public new T Data { get; set; }
    
            /// 
            /// 成功
            /// 
            /// 
            /// 
            /// 
            public ResultDto Success(T data = default, string msg = "success")
            {
                this.Code = (int)EnumCode.Succeed;
                this.Successful = true;
                this.Data = data;
                this.Msg = msg;
                return this;
            }
    
    
            /// 
            /// 异常
            /// 
            /// 
            /// 
            /// 
            public ResultDto Failed(string msg = "err")
            {
                this.Code = (int)EnumCode.Err;
                this.Successful = false;
                this.Msg = msg;
                return this;
            }
    
            /// 
            /// 异常
            /// 
            /// 
            /// 
            /// 
            public ResultDto Failed(string msg, int enumCode)
            {
                this.Code = enumCode;
                this.Successful = false;
                this.Msg = msg;
                return this;
            }
    
    
        }

    /// 
        /// 泛型类 返回模型
        /// 
        /// 
        public class ResultDto : ResultDto, IResultDto
        {
            /// 
            /// 常规数据
            /// 
            public new T Data { get; set; }
    
            /// 
            /// 成功
            /// 
            /// 
            /// 
            /// 
            public ResultDto Success(T data = default, string msg = "success")
            {
                this.Code = (int)EnumCode.Succeed;
                this.Successful = true;
                this.Data = data;
                this.Msg = msg;
                return this;
            }
    
    
            /// 
            /// 异常
            /// 
            /// 
            /// 
            /// 
            public ResultDto Failed(string msg = "err")
            {
                this.Code = (int)EnumCode.Err;
                this.Successful = false;
                this.Msg = msg;
                return this;
            }
    
            /// 
            /// 异常
            /// 
            /// 
            /// 
            /// 
            public ResultDto Failed(string msg, int enumCode)
            {
                this.Code = enumCode;
                this.Successful = false;
                this.Msg = msg;
                return this;
            }
    
    
        }
    复制代码

    我们再次尝试返回该类型结果

      return ResultTo.Success(tokenDto, "登录成功");

    看看swagger文档的显示,正是我们需要的

     根据经验我们还要需要返回列表

    复制代码
     /// 
        /// 泛型结果模型集合
        /// 
        public interface IResultListDto : IResultDto  
        {
    
            /// 
            /// 常规数据
            /// 
            new List Data { get; set; }
    
        }

     /// 
        /// 泛型结果模型集合
        /// 
        public interface IResultListDto : IResultDto  
        {
    
            /// 
            /// 常规数据
            /// 
            new List Data { get; set; }
    
        }
    复制代码
    复制代码
     /// 
        /// 泛型类 返回模型
        /// 
        /// 
        public class ResultListDto : ResultDto, IResultListDto  
        {
    
            /// 
            /// 常规数据
            /// 
            public new List Data { get; set; }
    
            /// 
            /// 成功
            /// 
            /// 
            /// 
            /// 
            public ResultListDto Success(List data = default, string msg = "success")
            {
                this.Code = (int)EnumCode.Succeed;
                this.Successful = true;
                this.Data = data;
                this.Msg = msg;
                return this;
            }
    
    
            /// 
            /// 异常
            /// 
            /// 
            /// 
            /// 
            public ResultListDto Failed(string msg = "err")
            {
                this.Code = (int)EnumCode.Err;
                this.Successful = false;
                this.Msg = msg;
                return this;
            }
    
            /// 
            /// 异常
            /// 
            /// 
            /// 
            /// 
            public ResultListDto Failed(string msg, int enumCode)
            {
                this.Code = enumCode;
                this.Successful = false;
                this.Msg = msg;
                return this;
            }
    
    
        }

        /// 
        /// 泛型类 返回模型
        /// 
        /// 
        public class ResultListDto : ResultDto, IResultListDto  
        {
    
            /// 
            /// 常规数据
            /// 
            public new List Data { get; set; }
    
            /// 
            /// 成功
            /// 
            /// 
            /// 
            /// 
            public ResultListDto Success(List data = default, string msg = "success")
            {
                this.Code = (int)EnumCode.Succeed;
                this.Successful = true;
                this.Data = data;
                this.Msg = msg;
                return this;
            }
    
    
            /// 
            /// 异常
            /// 
            /// 
            /// 
            /// 
            public ResultListDto Failed(string msg = "err")
            {
                this.Code = (int)EnumCode.Err;
                this.Successful = false;
                this.Msg = msg;
                return this;
            }
    
            /// 
            /// 异常
            /// 
            /// 
            /// 
            /// 
            public ResultListDto Failed(string msg, int enumCode)
            {
                this.Code = enumCode;
                this.Successful = false;
                this.Msg = msg;
                return this;
            }
    
    
        }
    复制代码

    顺着这个思路,我们可以继续实现分页,多参数返回等等,如果你觉得麻烦,当然也可以直接在nuget直接引用 Sy.ResultExtension这个组件,码农小哥已经为你实现了该功能。
     1、动态参数接收情况

    复制代码
    public async Task GetJsonResult(AllWebSiteDto input)
    {
      return ResultTo.Success("返回成功");
      //return ResultTo.Failed("返回失败",errCode);
    }
    复制代码

    2、泛型接收

    复制代码
    public async Task> GetJsonResult(AllWebSiteDto input)
     {
       return ResultTo.Success(model,"返回成功");
      //return ResultTo.Failed("返回失败",errCode);
     
     }
    public async Task> GetJsonResult(AllWebSiteDto input)
     {
        return ResultTo.Success(newsDto,webSiteDto,"返回成功");
        //return ResultTo.Failed("返回失败");
     }
    复制代码

    3、泛型接收列表

    复制代码
            public async Task> GetJsonResult2(AllWebSiteDto input)
            {
                return ResultToList.Success(list, "返回成功");
                //return ResultToList.Failed("返回失败",errCode);
    
            }
            public async Task> GetJsonResult3(AllWebSiteDto input)
            {
                return ResultToList.Success(newsDtos, webSiteDtos,"返回成功");
                //return ResultToList.Failed("返回失败");
            }
    复制代码

    4、泛型接口分页列表

    复制代码
            public async Task> GetJsonResult2(AllWebSiteDto input)
            {
                return ResultToPage.Success(list,total,page, "返回成功");
                //return ResultToPage.Failed("返回失败",errCode);
    
            }
            public async Task> GetJsonResult3(AllWebSiteDto input)
            {
                return ResultToPage.Success(newsDtos, webSiteDtos, total, page,"返回成功");
                //return ResultToPage.Failed("返回失败");
            }
    复制代码

     

  • 相关阅读:
    【Numpy-矩阵库~python】
    Clickhouse在货品标签场景的应用
    Oracle表格分类浅析1——普通堆表
    JavaSE入门篇——类和对象(实例理解)
    Spring 源码阅读 10:自定义的 EntityResolver 是如何帮助 Spring 校验 XML 配置的
    [附源码]计算机毕业设计JAVAjsp美容院业务管理系统
    面试总和(一)
    iOS基础开发介绍
    RocketMQ源码分析(十)之MappedFileQueue
    粒子滤波PF—从贝叶斯滤波到粒子滤波PF——Part-I(贝叶斯滤波)
  • 原文地址:https://www.cnblogs.com/noert/p/16771697.html