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

/// 失败/// /// 我们尝试返回该类型结果
return ResultTo.Success(tokenDto, "登录成功");通过swagger文档我们看下返回效果
插件化编程之WebAPI统一返回模型

文章插图
细心的你是不是发现了啥,我们的接口文档他不认识我们返回类型的实体呀,还玩啥,别急我们可以再稍微扩展下,加个泛型的返回,看起来就完美了 。
/// <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; }}【插件化编程之WebAPI统一返回模型】我们实现泛型接口如下,同样在这里实现了成功和失败赋值方法 。
/// <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 = https://www.huyubaike.com/biancheng/default, string msg ="success"){this.Code = (int)EnumCode.Succeed;this.Successful = true;this.Data = https://www.huyubaike.com/biancheng/data;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 = https://www.huyubaike.com/biancheng/default, string msg ="success"){this.Code = (int)EnumCode.Succeed;this.Successful = true;this.Data = https://www.huyubaike.com/biancheng/data;this.Msg = msg;return this;}/// /// 异常/// /// 我们再次尝试返回该类型结果
return ResultTo<TokenDto>.Success(tokenDto, "登录成功");看看swagger文档的显示,正是我们需要的
插件化编程之WebAPI统一返回模型

文章插图
根据经验我们还要需要返回列表
/// <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 = https://www.huyubaike.com/biancheng/default, string msg ="success"){this.Code = (int)EnumCode.Succeed;this.Successful = true;this.Data = https://www.huyubaike.com/biancheng/data;this.Msg = msg;return this;}///

推荐阅读