如何进行Abp.Dapper多表查询?

时间:2018-09-14 02:38:34

标签: dapper aspnetboilerplate

我有一个实体模型Person -> Phones(collection)和以下代码

人员

[Table("PbPersons")]
public class Person: FullAuditedEntity, IMustHaveTenant
{
    public const int MaxNameLength = 32;
    public const int MaxSurnameLength = 32;
    public const int MaxEmailAddressLength = 255;

    public virtual int TenantId { get; set; }

    [Required]
    [MaxLength(MaxNameLength)]
    public virtual string Name { get; set; }

    [Required]
    [MaxLength(MaxSurnameLength)]
    public virtual string Surname { get; set; }

    [MaxLength(MaxEmailAddressLength)]
    public virtual string EmailAddress { get; set; }

    public virtual ICollection<Phone> Phones { get; set; }

}

电话

[Table("PbPhones")]
public class Phone : CreationAuditedEntity<long>
{
    public const int MaxNumberLength = 16;

    [ForeignKey("PersonId")]
    public virtual Person Person { get; set; }
    public virtual int PersonId { get; set; }

    [Required]
    public virtual PhoneType Type { get; set; }

    [Required]
    [MaxLength(MaxNumberLength)]
    public virtual string Number { get; set; }

}

具有Abp.EF的旧应用层

此代码返回包括电话在内的人员列表

using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Dapper.Repositories;
using Abp.Domain.Repositories;
using Abp.Extensions;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;

public class PersonAppService : WebAppServiceBase, IPersonAppService
{
private readonly IRepository<Person> _personRepository;
private readonly IRepository<Phone, long> _phoneRepository;

public PersonAppService(IRepository<Person> personRepository, 
IRepository<Phone, long> phoneRepository)        
{
    _personRepository = personRepository;
    _phoneRepository = phoneRepository;
}

 public ListResultDto<PersonListDto> GetPeople(GetPeopleInput input)
{

            var persons = _personRepository
            .GetAll()
            .Include(p => p.Phones)
            .WhereIf(
                !input.Filter.IsNullOrEmpty(),
                p => p.Name.Contains(input.Filter) ||
                        p.Surname.Contains(input.Filter) ||
                        p.EmailAddress.Contains(input.Filter)
            )
            .OrderBy(p => p.Name)
            .ThenBy(p => p.Surname)
            .ToList();

            return new ListResultDto<PersonListDto>(Mapper.Map<List<Person>, List<PersonListDto>>(persons));
}

}

带有Abp.Dapper的新应用层

但是,当我使用Abp.Dapper时,GetAll方法不允许包含诸如Include / WhereIf之类的任何方法,我知道这是因为它实现了IEnumerable接口。我还需要返回每个人的电话列表。我该怎么办?,我尝试使用linq(包括,它不可用)失败了。

 using Abp.Application.Services.Dto;
 using Abp.Authorization;
 using Abp.Dapper.Repositories;
 using Abp.Domain.Repositories;
 using Abp.Extensions;
 using AutoMapper;
 using Microsoft.EntityFrameworkCore;
 using System.Collections.Generic;
 using System.Linq;

 public class PersonAppService : WebAppServiceBase, IPersonAppService
 {
private readonly IDapperRepository<Person> _personRepository;
private readonly IRepository<Phone, long> _phoneRepository;

public PersonAppService(IDapperRepository<Person> personRepository, 
IRepository<Phone, long> phoneRepository)        
{
    _personRepository = personRepository;
    _phoneRepository = phoneRepository;
}

 public ListResultDto<PersonListDto> GetPeople(GetPeopleInput input)
{

            var persons = _personRepository.GetAll()              
            .OrderBy(p => p.Name)
            .ThenBy(p => p.Surname)
            .ToList();

            return new ListResultDto<PersonListDto>(Mapper.Map<List<Person>, List<PersonListDto>>(persons));
}

}

1 个答案:

答案 0 :(得分:0)

请参阅同时获取导航数据的测试代码。

 public async Task Should_Include_Navigation_Properties_If_Requested()
        {
            using (var uow = _uowManager.Begin())
            {
                var post = await _postRepository.GetAllIncluding(p => p.Blog).FirstAsync();

                post.Blog.ShouldNotBeNull();
                post.Blog.Name.ShouldBe("test-blog-1");

                await uow.CompleteAsync();
            }
}

https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/test/Abp.EntityFrameworkCore.Tests/Tests/Repository_Tests.cs#L80