上一篇文章(http://www.javashuo.com/article/p-rscsbfwx-bv.html)使用自定义仓储完成了简单的增删改查案例,有心的同窗能够看出,咱们的返回参数一塌糊涂,显得很不友好。html
在实际开发过程当中,每一个公司可能不尽相同,但都大同小异,咱们的返回数据都是包裹在一个公共的模型下面的,而不是直接返回最终数据,在返回参数中,显示出当前请求的时间戳,是否请求成功,若是错误那么错误的消息是什么,状态码(状态码能够是咱们本身定义的值)等等。可能显得很繁琐,不必,但这样作的好处毋庸置疑,除了美化了咱们的API以外,也方便了前端同窗的数据处理。前端
咱们将统一的返回模型放在.ToolKits
层中,以前说过这里主要是公共的工具类、扩展方法。git
新建一个Base文件夹,添加响应实体类ServiceResult.cs
,在Enum文件夹下单独定义一个ServiceResultCode
响应码枚举,0/1。分别表明 成功和失败。github
//ServiceResultCode.cs namespace Meowv.Blog.ToolKits.Base.Enum { /// <summary> /// 服务层响应码枚举 /// </summary> public enum ServiceResultCode { /// <summary> /// 成功 /// </summary> Succeed = 0, /// <summary> /// 失败 /// </summary> Failed = 1, } }
//ServiceResult.cs using Meowv.Blog.ToolKits.Base.Enum; using System; namespace Meowv.Blog.ToolKits.Base { /// <summary> /// 服务层响应实体 /// </summary> public class ServiceResult { /// <summary> /// 响应码 /// </summary> public ServiceResultCode Code { get; set; } /// <summary> /// 响应信息 /// </summary> public string Message { get; set; } /// <summary> /// 成功 /// </summary> public bool Success => Code == ServiceResultCode.Succeed; /// <summary> /// 时间戳(毫秒) /// </summary> public long Timestamp { get; } = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); /// <summary> /// 响应成功 /// </summary> /// <param name="message"></param> /// <param name="data"></param> /// <returns></returns> public void IsSuccess(string message = "") { Message = message; Code = ServiceResultCode.Succeed; } /// <summary> /// 响应失败 /// </summary> /// <param name="message"></param> /// <param name="data"></param> /// <returns></returns> public void IsFailed(string message = "") { Message = message; Code = ServiceResultCode.Failed; } /// <summary> /// 响应失败 /// </summary> /// <param name="exexception></param> /// <param name="data"></param> /// <returns></returns> public void IsFailed(Exception exception) { Message = exception.InnerException?.StackTrace; Code = ServiceResultCode.Failed; } } }
能够看到,还定义了 string 类型的 Message,bool 类型的 Success,Success取决于Code == ServiceResultCode.Succeed
的结果。还有一个当前的时间戳Timestamp。api
其中还有IsSuccess(...)
和IsFailed(...)
方法,当咱们成功返回数据或者当系统出错或者参数异常的时候执行,这一点也不难理解吧。异步
这个返回模型暂时只支持无需返回参数的api使用,还须要扩展一下,当咱们须要返回其它各类复杂类型的数据就行不通了。因此还须要添加一个支持泛型的返回模型,新建模型类:ServiceResultOfT.cs
,这里的T就是咱们的返回结果,而后继承咱们的ServiceResult,指定T为class。并从新编写一个IsSuccess(...)
方法,代码以下:async
//ServiceResultOfT.cs using Meowv.Blog.ToolKits.Base.Enum; namespace Meowv.Blog.ToolKits.Base { /// <summary> /// 服务层响应实体(泛型) /// </summary> /// <typeparam name="T"></typeparam> public class ServiceResult<T> : ServiceResult where T : class { /// <summary> /// 返回结果 /// </summary> public T Result { get; set; } /// <summary> /// 响应成功 /// </summary> /// <param name="result"></param> /// <param name="message"></param> public void IsSuccess(T result = null, string message = "") { Message = message; Code = ServiceResultCode.Succeed; Result = result; } } }
此时针对无需返回参数和须要返回参数的api均可以知足要求了。可是还有一种就没办法了,那就是带分页的数据,咱们都应该知道想要分页,数据总数确定是必不可少的。函数
因此此时还须要扩展一个分页的响应实体,当咱们使用的时候,直接将分页响应实体做为上面写的ServiceResult<T>
中的T参数,便可知足需求。工具
新建文件夹Paged,添加总数接口IHasTotalCount
、返回结果列表接口IListResult
post
//IHasTotalCount.cs namespace Meowv.Blog.ToolKits.Base.Paged { public interface IHasTotalCount { /// <summary> /// 总数 /// </summary> int Total { get; set; } } } //IListResult.cs using System.Collections.Generic; namespace Meowv.Blog.ToolKits.Base.Paged { public interface IListResult<T> { /// <summary> /// 返回结果 /// </summary> IReadOnlyList<T> Item { get; set; } } }
IListResult<T>
接受一个参数,并将其指定为IReadOnlyList
返回。
如今来实现IListResult
接口,新建ListResult
实现类,继承IListResult
,在构造函数中为其赋值,代码以下:
//ListResult.cs using System.Collections.Generic; namespace Meowv.Blog.ToolKits.Base.Paged { public class ListResult<T> : IListResult<T> { IReadOnlyList<T> item; public IReadOnlyList<T> Item { get => item ?? (item = new List<T>()); set => item = value; } public ListResult() { } public ListResult(IReadOnlyList<T> item) { Item = item; } } }
最后新建咱们的分页响应实体接口:IPagedList
和分页响应实体实现类:PagedList
,它同时也要接受一个泛型参数 T。
接口继承了IListResult<T>
和IHasTotalCount
,实现类继承ListResult<T>
和IPagedList<T>
,在构造函数中为其赋值。代码以下:
//IPagedList.cs namespace Meowv.Blog.ToolKits.Base.Paged { public interface IPagedList<T> : IListResult<T>, IHasTotalCount { } } //PagedList.cs using Meowv.Blog.ToolKits.Base.Paged; using System.Collections.Generic; namespace Meowv.Blog.ToolKits.Base { /// <summary> /// 分页响应实体 /// </summary> /// <typeparam name="T"></typeparam> public class PagedList<T> : ListResult<T>, IPagedList<T> { /// <summary> /// 总数 /// </summary> public int Total { get; set; } public PagedList() { } public PagedList(int total, IReadOnlyList<T> result) : base(result) { Total = total; } } }
到这里咱们的返回模型就圆满了,看一下此时下咱们的项目层级目录。
接下来去实践一下,修改咱们以前建立的增删改查接口的返回参数。
//IBlogService.cs using Meowv.Blog.Application.Contracts.Blog; using Meowv.Blog.ToolKits.Base; using System.Threading.Tasks; namespace Meowv.Blog.Application.Blog { public interface IBlogService { //Task<bool> InsertPostAsync(PostDto dto); Task<ServiceResult<string>> InsertPostAsync(PostDto dto); //Task<bool> DeletePostAsync(int id); Task<ServiceResult> DeletePostAsync(int id); //Task<bool> UpdatePostAsync(int id, PostDto dto); Task<ServiceResult<string>> UpdatePostAsync(int id, PostDto dto); //Task<PostDto> GetPostAsync(int id); Task<ServiceResult<PostDto>> GetPostAsync(int id); } }
接口所有为异步方式,用ServiceResult
包裹做为返回模型,添加和更新T参数为string类型,删除就直接不返回结果,而后查询为:ServiceResult<PostDto>
,再看一下实现类:
//BlogService.cs ... public async Task<ServiceResult<string>> InsertPostAsync(PostDto dto) { var result = new ServiceResult<string>(); var entity = new Post { Title = dto.Title, Author = dto.Author, Url = dto.Url, Html = dto.Html, Markdown = dto.Markdown, CategoryId = dto.CategoryId, CreationTime = dto.CreationTime }; var post = await _postRepository.InsertAsync(entity); if (post == null) { result.IsFailed("添加失败"); return result; } result.IsSuccess("添加成功"); return result; } public async Task<ServiceResult> DeletePostAsync(int id) { var result = new ServiceResult(); await _postRepository.DeleteAsync(id); return result; } public async Task<ServiceResult<string>> UpdatePostAsync(int id, PostDto dto) { var result = new ServiceResult<string>(); var post = await _postRepository.GetAsync(id); if (post == null) { result.IsFailed("文章不存在"); return result; } post.Title = dto.Title; post.Author = dto.Author; post.Url = dto.Url; post.Html = dto.Html; post.Markdown = dto.Markdown; post.CategoryId = dto.CategoryId; post.CreationTime = dto.CreationTime; await _postRepository.UpdateAsync(post); result.IsSuccess("更新成功"); return result; } public async Task<ServiceResult<PostDto>> GetPostAsync(int id) { var result = new ServiceResult<PostDto>(); var post = await _postRepository.GetAsync(id); if (post == null) { result.IsFailed("文章不存在"); return result; } var dto = new PostDto { Title = post.Title, Author = post.Author, Url = post.Url, Html = post.Html, Markdown = post.Markdown, CategoryId = post.CategoryId, CreationTime = post.CreationTime }; result.IsSuccess(dto); return result; } ...
当成功时,调用IsSuccess(...)
方法,当失败时,调用IsFailed(...)
方法。最终咱们返回的是new ServiceResult()
或者new ServiceResult<T>()
对象。
同时不要忘记在Controller中也须要修改一下,以下:
//BlogController.cs ... ... public async Task<ServiceResult<string>> InsertPostAsync([FromBody] PostDto dto) ... ... public async Task<ServiceResult> DeletePostAsync([Required] int id) ... ... public async Task<ServiceResult<string>> UpdatePostAsync([Required] int id, [FromBody] PostDto dto) ... ... public async Task<ServiceResult<PostDto>> GetPostAsync([Required] int id) ... ...
此时再去咱们的Swagger文档发起请求,这里咱们调用一下查询接口看看返回的样子,看看效果吧。
本篇内容比较简单,主要是包装咱们的api,让返回结果显得比较正式一点。那么,你学会了吗?😁😁😁