(精华)2020年8月22日 ABP vNext 定制Repository

前一节我们看到了使用缺省Repository的局限性。解决这种局限性有两种办法,一种是在Application层引入EF,这样可以在ApplicationService中使用EF的扩展,如Include等,弥补通用Repository的不足。还有一种办法是编写定制的Repository。我们不希望应用层依赖特定的数据库框架(不远的将来我们会把数据移动到MongoDb),所以我们采用第二种办法。

定制Repository需要两部分代码:在领域层的Repository接口和在特定数据访问层种的实现。我们需要定义两个定制的Repository:ICategoryPoemRepository和IPoemRepository,这两个接口在ZL.AbpNext.Poem.Core项目Repositories目录下:

using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.Domain.Repositories;
using ZL.AbpNext.Poem.Core.Poems;

namespace ZL.AbpNext.Poem.Core.Repositories
{
     
    public interface ICategoryPoemRepository : IRepository<CategoryPoem, int>
    {
     
        List<Category> GetPoemCategories(int poemid);

        List<Core.Poems.Poem> GetPoemsOfCategory(int categoryid);
    }
}
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace ZL.AbpNext.Poem.Core.Repositories
{
     
    public interface IPoemRepository : IRepository<Core.Poems.Poem , int>
    {
     
        Task<List<Core.Poems.Poem>> GetPagedPoems(int maxResult,int skip,string author,string keyword,string[] categories,out int total);
    }
}

对应的实现在ZL.AbpNext.Poem.EF项目中:

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using ZL.AbpNext.Poem.Core.Poems;
using ZL.AbpNext.Poem.Core.Repositories;
using ZL.AbpNext.Poem.EF.EntityFramework;

namespace ZL.AbpNext.Poem.EF.Repositories
{
     
    public class CategoryPoemRepository : EfCoreRepository<PoemDbContext, CategoryPoem, int>, ICategoryPoemRepository
    {
     
        public CategoryPoemRepository(IDbContextProvider<PoemDbContext> dbContextProvider)
        : base(dbContextProvider)
        {
     

        }
        public List<Category> GetPoemCategories(int poemid)
        {
     
            var set = DbContext.Set<CategoryPoem>().Include(o => o.Category).AsQueryable();
            var lst = set.Where(p => p.PoemId == poemid).Select(o => o.Category);
            return lst.ToList();
        }

        public List<Core.Poems.Poem> GetPoemsOfCategory(int categoryid)
        {
     
            var set = DbContext.Set<CategoryPoem>().Include(o => o.Poem).AsQueryable();
            var lst = set.Where(p => p.CategoryId == categoryid).Select(o=>o.Poem);
            return lst.ToList();
        }
    }
}

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using ZL.AbpNext.Poem.Core.Repositories;
using ZL.AbpNext.Poem.EF.EntityFramework;

namespace ZL.AbpNext.Poem.EF.Repositories
{
     
    public class PoemRepository : EfCoreRepository<PoemDbContext, Core.Poems.Poem, int>, IPoemRepository
    {
     
        public PoemRepository(IDbContextProvider<PoemDbContext> dbContextProvider)
        : base(dbContextProvider)
        {
     

        }

        public Task<List<Core.Poems.Poem>> GetPagedPoems(int maxResult, int skip, string author, string keyword, string[] categories, out int total)
        {
     
            var set=DbContext.Set<Core.Poems.Poem>().Include(o=>o.Author).Include(o=>o.PoemCategories).AsQueryable();
            if (!string.IsNullOrEmpty(author))
            {
     
                set = set.Where(o => o.Author.Name == author);
            }
            if (!string.IsNullOrEmpty(keyword))
            {
     
                set = set.Where(o => o.Title.Contains(keyword) );
            }
            if (categories != null && categories.Length > 0)
            {
     
                foreach (var category in categories)
                {
     
                    set = set.Where(o => o.PoemCategories.Any(q => q.Category.CategoryName == category));
                }
            }
            total = set.Count();
            var lst = set.OrderBy(o => o.Id).PageBy(skip,maxResult).ToListAsync();
            return lst;
        }
    }
}

完整的PoemAppService如下:

using System.Collections.Generic;
using System.Linq;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using ZL.AbpNext.Poem.Core.Poems;
using ZL.AbpNext.Poem.Core.Repositories;

namespace ZL.AbpNext.Poem.Application.Poems
{
     
    public class PoemAppService : ApplicationService, IPoemAppService
    {
     
        private readonly IPoemRepository _poemRepository;
        private readonly IRepository<Category> _categoryRepository;
        private readonly IRepository<Poet> _poetRepository;
        private readonly ICategoryPoemRepository _categoryPoemRepository;
        public PoemAppService(IPoemRepository poemRepository
            , IRepository<Category> categoryRepository
            , IRepository<Poet> poetRepository
            , ICategoryPoemRepository categoryPoemRepository)
        {
     
            _poemRepository = poemRepository;
            _categoryRepository = categoryRepository;
            _poetRepository = poetRepository;
            _categoryPoemRepository = categoryPoemRepository;
        }

        public CategoryDto AddCategory(CategoryDto category)
        {
     
            var cate = _categoryRepository.FirstOrDefault(o => o.CategoryName == category.CategoryName);

            if (cate == null)
            {
     
                cate= _categoryRepository.InsertAsync(new Category {
      CategoryName = category.CategoryName },true).Result;
            }
            return ObjectMapper.Map<Category,CategoryDto>(cate); 
            
        }

        public void AddPoemToCategory(CategoryPoemDto categoryPoem)
        {
     
            var categorypoem = _categoryPoemRepository.FirstOrDefault(o => o.CategoryId == categoryPoem.CategoryId && o.PoemId == categoryPoem.PoemId);
            if (categorypoem == null)
            {
     
                categorypoem = new CategoryPoem {
      CategoryId = categoryPoem.CategoryId, PoemId = categoryPoem.PoemId };
                _categoryPoemRepository.InsertAsync(categorypoem,true);
            }
        }

        public PoetDto AddPoet(PoetDto poet)
        {
     
            var addpoet=_poetRepository.InsertAsync(new Poet
            {
     
                 Name=poet.Name,
                 Description=poet.Description
            },true).Result;
            return new PoetDto
            {
     
                Id=addpoet.Id,
                Name=addpoet.Name,
                Description=addpoet.Description
            };
        }

        public void DeleteCategory(CategoryDto category)
        {
     
            if (category.Id > 0)
            {
     
                var cat = _categoryRepository.FirstOrDefault(o => o.Id == category.Id);
                if (cat != null)
                {
     
                    _categoryRepository.DeleteAsync(cat, true);
                }
            }
            else if (!string.IsNullOrEmpty(category.CategoryName))
            {
     
                var cat = _categoryRepository.FirstOrDefault(o => o.CategoryName == category.CategoryName);
                if (cat != null)
                {
     
                    _categoryRepository.DeleteAsync(cat,true);
                }
            }
        }

        public List<CategoryDto> GetAllCategories()
        {
     
            return ObjectMapper.Map<List<Category>, List<CategoryDto>>(_categoryRepository.ToList());
        }

        public List<CategoryPoemDto> GetCategoryPoems()
        {
     
            return ObjectMapper.Map<List<CategoryPoem>, List<CategoryPoemDto>>(_categoryPoemRepository.ToList());
        }

        public PagedResultDto<PoemDto> GetPagedPoems(PagedResultRequestDto dto)
        {
     
            var count = _poemRepository.Count();
            var lst = _poemRepository.OrderBy(o => o.Id).PageBy(dto).ToList();

            return new PagedResultDto<PoemDto>
            {
     
                TotalCount = count,
                Items = ObjectMapper.Map<List<Core.Poems.Poem>, List<PoemDto>>(lst) //lst.MapTo>()
            };
        }

        public PagedResultDto<PoetDto> GetPagedPoets(PagedResultRequestDto dto)
        {
     
                var count = _poetRepository.Count();
                var lst = _poetRepository.OrderBy(o => o.Id).PageBy(dto).ToList();
                var items = new List<PoetDto>();
                
                return new PagedResultDto<PoetDto>
                {
     
                    TotalCount = count,
                    Items = ObjectMapper.Map<List<Poet>, List<PoetDto>>(lst)
                };
        }

        public List<CategoryDto> GetPoemCategories(int poemid)
        {
     
            var lst = _categoryPoemRepository.GetPoemCategories(poemid);
            return ObjectMapper.Map<List<Category>, List<CategoryDto>>(lst);
        }

        public List<PoemDto> GetPoemsOfCategory(int categoryid)
        {
     
            var lst = _categoryPoemRepository.GetPoemsOfCategory(categoryid);
            return ObjectMapper.Map<List<Core.Poems.Poem>, List<PoemDto>>(lst);
        }

        public void RemovePoemFromCategory(CategoryPoemDto categoryPoem)
        {
     
            var categorypoem = _categoryPoemRepository.FirstOrDefault(o => o.CategoryId == categoryPoem.CategoryId && o.PoemId == categoryPoem.PoemId);
            if (categorypoem != null)
            {
     
                _categoryPoemRepository.DeleteAsync(categorypoem,true);
            }
        }

        public PagedResultDto<PoemDto> SearchPoems(SearchPoemDto dto)
        {
     
           
            int total;
            var lst = _poemRepository.GetPagedPoems(dto.MaxResultCount, dto.SkipCount, dto.AuthorName, dto.Keyword,dto.Categories, out total).Result;
            return new PagedResultDto<PoemDto> {
     
                TotalCount = total,
                Items=ObjectMapper.Map<List<Core.Poems.Poem>, List<PoemDto>>(lst)
        };
        }

        public PagedResultDto<PoetDto> SearchPoets(SearchPoetDto dto)
        {
     
            var res = _poetRepository.AsQueryable();
            if (!string.IsNullOrEmpty(dto.Keyword))
            {
     
                res = res.Where(o => o.Name.Contains(dto.Keyword));
            }
            var count = res.Count();
            var lst = res.OrderBy(o => o.Id).PageBy(dto).ToList();

            return new PagedResultDto<PoetDto>
            {
     
                TotalCount = count,
                Items = ObjectMapper.Map< List < Poet> ,List <PoetDto>>(lst)
            };
        }
    }
}

你可能感兴趣的:(#,ABP,vNext微服务框架)