(精华)2020年8月22日 ABP vNext 定制Repository
【摘要】
前一节我们看到了使用缺省Repository的局限性。解决这种局限性有两种办法,一种是在Application层引入EF,这样可以在ApplicationService中使用EF的扩展,如Include等...
前一节我们看到了使用缺省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);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
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);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
对应的实现在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();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
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;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
完整的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<List<PoemDto>>()
};
}
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)
};
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。
原文链接:codeboy.blog.csdn.net/article/details/108169836
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)