背景:
由于为了代码统一及规范,我们通常定义统一返回结果集。
代码:
1.定义返回结果结构
using org.huage.AuthManagement.Entity.Enum;
namespace org.huage.AuthManagement.Entity.Common;
public class ResponseResult<T>
{
/// <summary>
/// 状态结果
/// </summary>
public ResultStatus Status { get; set; } = ResultStatus.Success;
private string? _msg;
/// <summary>
/// 消息描述
/// </summary>
public string? Message
{
get
{
// 如果没有自定义的结果描述,则可以获取当前状态的描述
return !string.IsNullOrEmpty(_msg) ? _msg : EnumHelper.GetDescription(Status);
}
set
{
_msg = value;
}
}
/// <summary>
/// 返回结果
/// </summary>
public T Data { get; set; }
/// <summary>
/// 成功状态返回结果
/// </summary>
/// <param name="result">返回的数据</param>
/// <returns></returns>
public static ResponseResult<T> SuccessResult(T data)
{
return new ResponseResult<T> { Status = ResultStatus.Success, Data = data };
}
/// <summary>
/// 隐式将T转化为ResponseResult<T>
/// </summary>
/// <param name="value"></param>
public static implicit operator ResponseResult<T>(T value)
{
return new ResponseResult<T> { Data = value };
}
/// <summary>
/// 失败状态返回结果
/// </summary>
/// <param name="code">状态码</param>
/// <param name="msg">失败信息</param>
/// <returns></returns>
public static ResponseResult<T> FailResult(string? msg = null)
{
return new ResponseResult<T> { Status = ResultStatus.Fail, Message = msg };
}
/// <summary>
/// 异常状态返回结果
/// </summary>
/// <param name="code">状态码</param>
/// <param name="msg">异常信息</param>
/// <returns></returns>
public static ResponseResult<T> ErrorResult(string? msg = null)
{
return new ResponseResult<T> { Status = ResultStatus.Error, Message = msg };
}
/// <summary>
/// 自定义状态返回结果
/// </summary>
/// <param name="status"></param>
/// <param name="result"></param>
/// <returns></returns>
public static ResponseResult<T> Result(ResultStatus status, T data, string? msg = null)
{
return new ResponseResult<T> { Status = status, Data = data, Message = msg };
}
}
ResultStatus定义:
public enum ResultStatus
{
[Description("请求成功")]
Success = 1,
[Description("请求失败")]
Fail = 0,
[Description("请求异常")]
Error = -1
}
使用:
[HttpGet("GetWeatherForecast")]
public ResponseResult<IEnumerable<WeatherForecast>> GetAll()
{
var datas = Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
});
return ResponseResult<IEnumerable<WeatherForecast>>.SuccessResult(datas);
}
2.优化:
上面我们通过完善ResponseResult<T>
类的封装,确实在某些程度上节省了一部分操作,但是还是有点美中不足,那就是每次返回结果的时候,虽然定义了几个常用的静态方法去操作返回结果,但是每次还得通过手动去把ResponseResult<T>
类给请出来才能使用,现在呢想在操作返回结果的时候不想看到它了。这个呢也很简单,我们可以借助微软针对MVC的Controller的封装进一步得到一个思路,那就是定义一个基类的Controller,我们在Controller基类中,把常用的返回结果封装一些方法,这样在Controller的子类中呢就可以直接调用这些方法,而不需要关注如何去编写方法的返回类型了,话不多说动手把Controller基类封装起来。
[ApiController]
[Route("api/[controller]")]
public class ApiControllerBase : ControllerBase
{
/// <summary>
/// 成功状态返回结果
/// </summary>
/// <param name="result">返回的数据</param>
/// <returns></returns>
protected ResponseResult<T> SuccessResult<T>(T result)
{
return ResponseResult<T>.SuccessResult(result);
}
/// <summary>
/// 失败状态返回结果
/// </summary>
/// <param name="code">状态码</param>
/// <param name="msg">失败信息</param>
/// <returns></returns>
protected ResponseResult<T> FailResult<T>(string? msg = null)
{
return ResponseResult<T>.FailResult(msg);
}
/// <summary>
/// 异常状态返回结果
/// </summary>
/// <param name="code">状态码</param>
/// <param name="msg">异常信息</param>
/// <returns></returns>
protected ResponseResult<T> ErrorResult<T>(string? msg = null)
{
return ResponseResult<T>.ErrorResult(msg);
}
/// <summary>
/// 自定义状态返回结果
/// </summary>
/// <param name="status"></param>
/// <param name="result"></param>
/// <returns></returns>
protected ResponseResult<T> Result<T>(ResultStatus status, T result, string? msg = null)
{
return ResponseResult<T>.Result(status, result, msg);
}
}
有了这么一个大神的辅助,一切似乎又向着美好进发了一步,这样的话每次我们自定义的Controller可以继承ApiControllerBase
类,从而使用里面的简化操作。所以再写起来代码,大概是这么一个效果
public class WeatherForecastController : ApiControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
[HttpGet("GetWeatherForecast")]
public ResponseResult<IEnumerable<WeatherForecast>> GetAll()
{
var datas = Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
});
return SuccessResult(datas);
}
}
以上就是全部内容了。