基于 abp vNext 和 .NET Core 开发博客项目 - 自定义仓储之增删改查 (2)

仓储接口实现类如下:

//PostRepository.cs using Meowv.Blog.Domain.Blog; using Meowv.Blog.Domain.Blog.Repositories; using Volo.Abp.Domain.Repositories.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore; namespace Meowv.Blog.EntityFrameworkCore.Repositories.Blog { /// <summary> /// PostRepository /// </summary> public class PostRepository : EfCoreRepository<MeowvBlogDbContext, Post, int>, IPostRepository { public PostRepository(IDbContextProvider<MeowvBlogDbContext> dbContextProvider) : base(dbContextProvider) { } } } //CategoryRepository.cs using Meowv.Blog.Domain.Blog; using Meowv.Blog.Domain.Blog.Repositories; using Volo.Abp.Domain.Repositories.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore; namespace Meowv.Blog.EntityFrameworkCore.Repositories.Blog { /// <summary> /// CategoryRepository /// </summary> public class CategoryRepository : EfCoreRepository<MeowvBlogDbContext, Category, int>, ICategoryRepository { public CategoryRepository(IDbContextProvider<MeowvBlogDbContext> dbContextProvider) : base(dbContextProvider) { } } } //TagRepository.cs using Meowv.Blog.Domain.Blog; using Meowv.Blog.Domain.Blog.Repositories; using System.Collections.Generic; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore; namespace Meowv.Blog.EntityFrameworkCore.Repositories.Blog { /// <summary> /// TagRepository /// </summary> public class TagRepository : EfCoreRepository<MeowvBlogDbContext, Tag, int>, ITagRepository { public TagRepository(IDbContextProvider<MeowvBlogDbContext> dbContextProvider) : base(dbContextProvider) { } /// <summary> /// 批量插入 /// </summary> /// <param></param> /// <returns></returns> public async Task BulkInsertAsync(IEnumerable<Tag> tags) { await DbContext.Set<Tag>().AddRangeAsync(tags); await DbContext.SaveChangesAsync(); } } } //PostTagRepository.cs using Meowv.Blog.Domain.Blog; using Meowv.Blog.Domain.Blog.Repositories; using System.Collections.Generic; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore; namespace Meowv.Blog.EntityFrameworkCore.Repositories.Blog { /// <summary> /// PostTagRepository /// </summary> public class PostTagRepository : EfCoreRepository<MeowvBlogDbContext, PostTag, int>, IPostTagRepository { public PostTagRepository(IDbContextProvider<MeowvBlogDbContext> dbContextProvider) : base(dbContextProvider) { } /// <summary> /// 批量插入 /// </summary> /// <param></param> /// <returns></returns> public async Task BulkInsertAsync(IEnumerable<PostTag> postTags) { await DbContext.Set<PostTag>().AddRangeAsync(postTags); await DbContext.SaveChangesAsync(); } } } //FriendLinkRepository.cs using Meowv.Blog.Domain.Blog; using Meowv.Blog.Domain.Blog.Repositories; using Volo.Abp.Domain.Repositories.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore; namespace Meowv.Blog.EntityFrameworkCore.Repositories.Blog { /// <summary> /// PostTagRepository /// </summary> public class FriendLinkRepository : EfCoreRepository<MeowvBlogDbContext, FriendLink, int>, IFriendLinkRepository { public FriendLinkRepository(IDbContextProvider<MeowvBlogDbContext> dbContextProvider) : base(dbContextProvider) { } } }

在TagRepository和PostTagRepository仓储接口的实现中,因为数据量不大,可以直接用了EF Core自带的AddRangeAsync批量保存数据。

到这里,关于博客的自定义仓储便完成了,此时项目层级目录图,如下:

3

增删改查

接下来在就可以在.Application服务层愉快的玩耍了,写服务之前,我们要分析我们的项目,要有哪些功能业务。由于是博客项目,无非就是一些增删改查。今天先不写博客业务,先完成对数据库文章表meowv_posts的一个简单CRUD。

在.Application层新建Blog文件夹,添加一个IBlogService.cs博客接口服务类,分别添加增删改查四个方法。

这时就要用到我们的数据传输对象(DTO)了,简单理解,DTO就是从我们的领域模型中抽离出来的对象,它是很纯粹的只包含我们拿到的数据,不参杂任何行为逻辑。

在.Application.Contracts层新建Blog文件夹,同时新建一个PostDto.cs类,与.Domain层中的Post.cs与之对应,他们很相似,但是不一样。

于是IBlogService.cs接口服务类的CRUD为:

//IBlogService.cs using Meowv.Blog.Application.Contracts.Blog; using System.Threading.Tasks; namespace Meowv.Blog.Application.Blog { public interface IBlogService { Task<bool> InsertPostAsync(PostDto dto); Task<bool> DeletePostAsync(int id); Task<bool> UpdatePostAsync(int id, PostDto dto); Task<PostDto> GetPostAsync(int id); } }

接口写好了,少不了实现方式,直接在Blog文件夹新建Impl文件夹,用来存放我们的接口实现类BlogService.cs,注意:都是以Service结尾的噢~

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

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