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; } }View Code
对应的还接口的实现如下,为了更好的独立性,在这里实现了成功和失败赋值方法。
/// <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 IResultDto 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 IResultDto 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 IResultDto 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 IResultDto Failed(int code = default, string msg = "failed") { this.Code = code; this.Successful = false; this.Msg = msg; return this; } }View Code
这在以前我们的结果封装我们差不多就可以告一段落了,但是现在我们的接口文档他不认识我们返回类型的实体呀,还玩啥,别急我们可以再稍微扩展下,加个泛型的返回,看起来就完美了。
/// <summary> /// 泛型结果模型 /// </summary> /// <typeparam name="T"></typeparam> public interface IResultDto<T> : IResultDto { /// <summary> /// 常规数据 /// </summary> new T Data { get; set; } }View Code
我们实现泛型接口如下,同样在这里实现了成功和失败赋值方法。
/// <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 IResultDto<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 IResultDto<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 IResultDto<T> Failed(string msg, int enumCode) { this.Code = enumCode; this.Successful = false; this.Msg = msg; return this; } }View Code
标签:WebAPI,插件,Code,IResultDto,编程,set,msg,get,public From: https://www.cnblogs.com/noert/p/16770897.html