简体   繁体   English

自动映射器子映射与中间的实体

[英]Automapper child mapping with entity in the middle

I have the following entity structure that I want to map to a Dto 我有以下要映射到Dto的实体结构

Context Entities 上下文实体

public class CallPoint
{
    public int Id { get; set; }
    public string Name {get; set;}
    public ICollection {UserCallPoint} UserCallPoints {get; set;}
}

public class UserCallPoint
{
    public int Id {get; set;}
    public CallPoint CallPoint {get; set;}
    public User User {get; set;
}

public class User
{
    public int Id {get; set;}
    public string Email {get; set;}
    public ICollection<UserCallPoint> UserCallPoints {get; set;
}

DTO DTO

public class CallPointDto
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<UserDto> Users { get; set; }
}

public class UserDto
{
    public int Id { get; set; }
    public string Email { get; set; }
    public List<CallPointDto> CallPoints { get; set; }
}

So basically I have a many to many mapping but there is an entity in between that acts as a lookup table basically. 所以基本上我有很多对很多的映射,但是它们之间确实有一个实体,基本上充当查找表。 How do I do this mapping? 我该如何映射? I am currently just looping and mapping like below but it is far too slow 我目前只是像下面这样循环和映射,但是它太慢了

    private List<CallPointDto> MapCallPoint(List<CallPoint> callPoints)
    {
        List<CallPointDto> callPointDtos = new List<CallPointDto>();
        foreach (var callPoint in callPoints)
        {
            var callPointDto = _mapper.Map<CallPointDto>(callPoint);
            callPointDto.Users = new List<UserDto>();
            foreach (var item in callPoint.UserCallPoints)
            {
                UserDto userDto = _mapper.Map<UserDto>(item.User);
                callPointDto.Users.Add(userDto);
            }
            callPointDtos.Add(callPointDto);
        }
        return callPointDtos;
    }

How would I do this with a Custom Resolver? 如何使用自定义解析器执行此操作?

I can't give you the answer that you are looking for because of the problem that the n:m relation could run endlessly. 由于n:m关系可能会无限运行,因此我无法为您提供所需的答案。 But i would suggest you to work with different sub-DTOs like this: 但我建议您像这样使用不同的子DTO:

public class CallPointBaseDto
{
    public int Id { get; set; }
    public string Name { get; set; }
}
public class CallPointDto : CallPointBaseDto
{
    public List<UserBaseDto> Users { get; set; }
}

public class UserBaseDto
{
    public int Id { get; set; }
    public string Email { get; set; }
}
public class UserDto : UserBaseDto
{
    public List<CallPointBaseDto> CallPoints { get; set; }
}

Mapping: 对应:

cfg.CreateMap<CallPoint, CallPointDto>()
    .ForMember(dest => dest.Users, opt => opt.MapFrom(src => src.UserCallPoints.Select(ucp => ucp.User)));
cfg.CreateMap<User, UserBaseDto>();

The downside of this, is of course, you don't have the CallPoints in your UserBaseDto, but if you need it, you can call this User explicitly and map it the other way. 当然,这样做的缺点是,UserBaseDto中没有CallPoints,但是如果需要,可以显式调用此User并以其他方式映射它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM