基于 abp vNext 和 .NET Core 开发博客项目 - 统一规范API,包装返回模型 (2)

现在来实现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></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; } } }

到这里我们的返回模型就圆满了,看一下此时下我们的项目层级目录。

1

接下来去实践一下,修改我们之前创建的增删改查接口的返回参数。

//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文档发起请求,这里我们调用一下查询接口看看返回的样子,看看效果吧。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wssgyd.html