首页 > 编程语言 >插件化编程之WebAPI统一返回模型

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

时间:2022-10-09 12:11:46浏览次数:59  
标签:WebAPI 插件 set string get Successful 编程 msg public

WebApi返回数据我们一般包裹在一个公共的模型下面的,而不是直接返回最终数据,在返回参数中,显示出当前请求的时间戳,是否请求成功,如果错误那么错误的消息是什么,状态码(根据业务定义的值)等等。我们常规的返回值常需要如下字段,为此我们可以先定义IResultDto 接口

/// <summary>
    /// 结果模型
    /// </summary>
    public interface IResultDto
    {
        /// <summary>
        /// 成功
        /// </summary>
        bool Successful { get; set; }

        /// <summary>
        /// 返回标识码
        /// </summary>
        int Code { get; set; }

        /// <summary>
        /// 操作码(用于幂等性验证,每次请求都会返回一个新的,以便用户下次更新使用)
        /// </summary>
        string OpCode { get; set; }

        /// <summary>
        /// 消息
        /// </summary>
        string Msg { get; set; }


        /// <summary>
        /// 常规数据
        /// </summary>
        dynamic Data { get; set; }

    }

/// <summary>
    /// 结果模型
    /// </summary>
    public interface IResultDto
    {
        /// <summary>
        /// 成功
        /// </summary>
        bool Successful { get; set; }

        /// <summary>
        /// 返回标识码
        /// </summary>
        int Code { get; set; }

        /// <summary>
        /// 操作码(用于幂等性验证,每次请求都会返回一个新的,以便用户下次更新使用)
        /// </summary>
        string OpCode { get; set; }

        /// <summary>
        /// 消息
        /// </summary>
        string Msg { get; set; }


        /// <summary>
        /// 常规数据
        /// </summary>
        dynamic Data { get; set; }

    }

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

/// <summary>
    /// 返回模型
    /// </summary>
    public class ResultDto : IResultDto
    {
        /// <summary>
        /// 返回标识码
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 成功
        /// </summary>
        [JsonIgnore]
        public bool Successful { get; set; }

        /// <summary>
        /// 消息
        /// </summary>
        public string Msg { get; set; }


        /// <summary>
        /// 操作码(用于幂等性验证,每次请求都会返回一个新的,以便用户下次更新使用)
        /// </summary>
        public string OpCode { get; set; }

        /// <summary>
        /// 常规数据
        /// </summary>
        public dynamic Data { get; set; }


        /// <summary>
        /// 成功
        /// </summary>
        /// <returns></returns>
        public ResultDto Success(string msg = "success")
        {
            this.Code = (int)EnumCode.Succeed;
            this.Successful = true;
            this.Msg = msg;
            return this;
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="code"></param>
        /// <param name="msg">说明</param>
        public ResultDto Failed(int code = default, string msg = "failed")
        {
            this.Code = code;
            this.Successful = false;
            this.Msg = msg;
            return this;
        }


    }

/// <summary>
    /// 返回模型
    /// </summary>
    public class ResultDto : IResultDto
    {
        /// <summary>
        /// 返回标识码
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 成功
        /// </summary>
        [JsonIgnore]
        public bool Successful { get; set; }

        /// <summary>
        /// 消息
        /// </summary>
        public string Msg { get; set; }


        /// <summary>
        /// 操作码(用于幂等性验证,每次请求都会返回一个新的,以便用户下次更新使用)
        /// </summary>
        public string OpCode { get; set; }

        /// <summary>
        /// 常规数据
        /// </summary>
        public dynamic Data { get; set; }


        /// <summary>
        /// 成功
        /// </summary>
        /// <returns></returns>
        public ResultDto Success(string msg = "success")
        {
            this.Code = (int)EnumCode.Succeed;
            this.Successful = true;
            this.Msg = msg;
            return this;
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="code"></param>
        /// <param name="msg">说明</param>
        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文档我们看下返回效果

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

   /// <summary>
    /// 泛型结果模型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IResultDto<T> : IResultDto  
    {
        /// <summary>
        /// 常规数据
        /// </summary>
        new T Data { get; set; }

    }

    /// <summary>
    /// 泛型结果模型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IResultDto<T> : IResultDto  
    {
        /// <summary>
        /// 常规数据
        /// </summary>
        new T Data { get; set; }

    }

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

/// <summary>
    /// 泛型类 返回模型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ResultDto<T> : ResultDto, IResultDto<T>
    {
        /// <summary>
        /// 常规数据
        /// </summary>
        public new T Data { get; set; }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public ResultDto<T> Success(T data = default, string msg = "success")
        {
            this.Code = (int)EnumCode.Succeed;
            this.Successful = true;
            this.Data = data;
            this.Msg = msg;
            return this;
        }


        /// <summary>
        /// 异常
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public ResultDto<T> Failed(string msg = "err")
        {
            this.Code = (int)EnumCode.Err;
            this.Successful = false;
            this.Msg = msg;
            return this;
        }

        /// <summary>
        /// 异常
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public ResultDto<T> Failed(string msg, int enumCode)
        {
            this.Code = enumCode;
            this.Successful = false;
            this.Msg = msg;
            return this;
        }


    }

/// <summary>
    /// 泛型类 返回模型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ResultDto<T> : ResultDto, IResultDto<T>
    {
        /// <summary>
        /// 常规数据
        /// </summary>
        public new T Data { get; set; }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public ResultDto<T> Success(T data = default, string msg = "success")
        {
            this.Code = (int)EnumCode.Succeed;
            this.Successful = true;
            this.Data = data;
            this.Msg = msg;
            return this;
        }


        /// <summary>
        /// 异常
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public ResultDto<T> Failed(string msg = "err")
        {
            this.Code = (int)EnumCode.Err;
            this.Successful = false;
            this.Msg = msg;
            return this;
        }

        /// <summary>
        /// 异常
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public ResultDto<T> Failed(string msg, int enumCode)
        {
            this.Code = enumCode;
            this.Successful = false;
            this.Msg = msg;
            return this;
        }


    }

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

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

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

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

 /// <summary>
    /// 泛型结果模型集合
    /// </summary>
    public interface IResultListDto<T> : IResultDto  
    {

        /// <summary>
        /// 常规数据
        /// </summary>
        new List<T> Data { get; set; }

    }

 /// <summary>
    /// 泛型结果模型集合
    /// </summary>
    public interface IResultListDto<T> : IResultDto  
    {

        /// <summary>
        /// 常规数据
        /// </summary>
        new List<T> Data { get; set; }

    }
 /// <summary>
    /// 泛型类 返回模型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ResultListDto<T> : ResultDto, IResultListDto<T>  
    {

        /// <summary>
        /// 常规数据
        /// </summary>
        public new List<T> Data { get; set; }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public ResultListDto<T> Success(List<T> data = default, string msg = "success")
        {
            this.Code = (int)EnumCode.Succeed;
            this.Successful = true;
            this.Data = data;
            this.Msg = msg;
            return this;
        }


        /// <summary>
        /// 异常
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public ResultListDto<T> Failed(string msg = "err")
        {
            this.Code = (int)EnumCode.Err;
            this.Successful = false;
            this.Msg = msg;
            return this;
        }

        /// <summary>
        /// 异常
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public ResultListDto<T> Failed(string msg, int enumCode)
        {
            this.Code = enumCode;
            this.Successful = false;
            this.Msg = msg;
            return this;
        }


    }

    /// <summary>
    /// 泛型类 返回模型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ResultListDto<T> : ResultDto, IResultListDto<T>  
    {

        /// <summary>
        /// 常规数据
        /// </summary>
        public new List<T> Data { get; set; }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public ResultListDto<T> Success(List<T> data = default, string msg = "success")
        {
            this.Code = (int)EnumCode.Succeed;
            this.Successful = true;
            this.Data = data;
            this.Msg = msg;
            return this;
        }


        /// <summary>
        /// 异常
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public ResultListDto<T> Failed(string msg = "err")
        {
            this.Code = (int)EnumCode.Err;
            this.Successful = false;
            this.Msg = msg;
            return this;
        }

        /// <summary>
        /// 异常
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public ResultListDto<T> Failed(string msg, int enumCode)
        {
            this.Code = enumCode;
            this.Successful = false;
            this.Msg = msg;
            return this;
        }


    }

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

标签:WebAPI,插件,set,string,get,Successful,编程,msg,public
From: https://www.cnblogs.com/noert/p/16771697.html

相关文章

  • Unity3D 网页插件Embedded Browser(ZFBrowser)PC端打包文件无法加载网页解决方法
    网页插件EmbeddedBrowser(ZFBrowser)PC端打包文件无法加载网页介绍:使用EmbeddedBrowser开发unity项目内嵌网页,打包后发现出现一个问题网页插件无响应,而在编辑器模式下场......
  • 高端的程序员,总是用最朴素的编程方式
     ......
  • 在java 的基础上增量学习 shell 编程
     shell 脚本其实还是我们比较常用的,在开发中经常会有需求,要写脚本实现某某功能。 比如,别人让写一个告警脚本,将消息推送到钉钉上。 这篇文章作为shell 的入门吧。 jav......
  • 插件化编程之WebAPI统一返回模型
      WebApi返回数据我们一般包裹在一个公共的模型下面的,而不是直接返回最终数据,在返回参数中,显示出当前请求的时间戳,是否请求成功,如果错误那么错误的消息是什么,状态码(根......
  • 用自己的编程语言实现了一个网站(增强版)
    前言前段时间在《用自己的编程语言实现了一个网站》用介绍了用GScript写的一个简单“网站”,虽然是打上引号的;页面长这样:看起来确实非常的挫,其实之前一直也想做一个G......
  • 如何从Chrome商店下载.crx的插件
    很多时候我们是直接安装插件了事,但是有些时候我们想下载.crx的插件,要怎么下载呢。亲测有效!!!1.下载首先每个Chrome上的插件都有一个ID,是该插件网址上最后一串字符串,将这个I......
  • windows编程之TextOut与DrawText
    目录windows编程之TextOut与DrawTextTextOut是什么函数TextOut函数原型DrawText是什么函数DrawText函数原型TextOut与DrawText区别后话windows编程之TextOut与DrawTex......
  • Python核心编程
    Python内存管理对象自己有引用计数,计变量的数。引用计数增加,引用计数减少。del垃圾回收。obj==eval(repr(obj))内置函数cmp调用类的​​__cmp__​​方法类型的......
  • 网络编程概述、网络编程三要素、InetAddress类及端口和协议介绍
    目录​​一、网络编程概述​​​​二、网络编程三要素​​​​IP地址:​​​​端口号:​​​​协议:​​​​三、InetAddress​​​​四、端口和协议​​一、网络编程概述计算......
  • C++——网络编程(一)
    一、网络结构C/SClient-Server结构,Server负责管理数据,Client负责与用户交互。Server会成为性能的瓶颈。B/SBrowser/Server,这种模式......