简体   繁体   English

自动映射器多对多关系映射

[英]automapper Many-to-Many relation mapping

Let's say, in the BookDetails page (BookForDetailsDto) we also show the authors of that book (AuthorForListingDto). 假设在BookDetails页面(BookForDetailsDto)中,我们还显示了该书的作者(AuthorForListingDto)。 And moreover, I want to show this author list together with a little info (just the name and id) on the books (BookForAuthorListingDto) of each author. 而且,我想显示此作者列表以及每个作者的书(BookForAuthorListingDto)上的一些小信息(名称和ID)。

I have a simple many-to-many relation consisting of Book, Author and BookAuthor objects. 我有一个简单的多对多关系,由Book,Author和BookAuthor对象组成。

public class Book {        
    public int Id { get; set; }
    public string Name { get; set; }
    public List<BookAuthor> Authors { get; set; }
}

public class Author {
    public int Id { get; set; }
    public string Name { get; set; }
    public List<BookAuthor> Books { get; set; }
}

public class BookAuthor {
    public int BookId { get; set; }
    public Book Book { get; set; }
    public int AuthorId { get; set; }
    public Author Author { get; set; }
}

And I have also 3 DTOs (where I am stoping an infinite loop): 而且我还有3个DTO(在这里我要停止无限循环):

public class BookForDetailsDto {
    public int Id { get; set; }
    public string Name { get; set; }
    public List<AuthorForListingDto> Authors { get; set; }
}

public class AuthorForListingDto {
    public int Id { get; set; }
    public string Name { get; set; }
    public List<BookForAuthorListingDto> Books { get; set; }
}

public class BookForAuthorListingDto {
    public int Id { get; set; }
    public string Name { get; set; }
}

Having a configuration as the following: 具有以下配置:

var config = new MapperConfiguration(cfg => {
    cfg.CreateMap<Book, BookForDetailsDto>();
    cfg.CreateMap<BookAuthor, AuthorForListingDto>();
    cfg.CreateMap<AuthorForListingDto, BookForAuthorListingDto>();
});

I'd like to perform a mapping from Book to BookForDetailsDto like this. 我想像这样执行从Book到BookForDetailsD的映射。

BookForDetailsDto BookDto = mapper.Map<BookForDetailsDto>(book);

But as a result, I get System.NullReferenceException. 但是结果是我得到了System.NullReferenceException。 It seems like, just in the first level of mapping, AutoMapper cannot get Author information from BookAuthor object. 似乎在第一级映射中,AutoMapper无法从BookAuthor对象获取Author信息。 I am searching for a configuration option but with no luck. 我正在搜索配置选项,但没有运气。 I should say I am a newbie with automapper and if there is a simple solution I appreciate. 我应该说我是automapper的新手,如果有一个简单的解决方案,我将不胜感激。

Note: I saw a comment which goes like "it is not a good practice to have reference in one DTO to second DTO". 注意:我看到一条评论,类似于“在一个DTO中引用第二个DTO并不是一种好习惯”。 But I cannot figure out how to do otherwise, because ,for example, for a clickable/navigatable child_object we need at least "a key and a display_name", so a child object of type List seems inevitable. 但是我无法弄清楚该怎么做,因为例如对于一个可单击/可导航的child_object,我们至少需要“键和display_name”,因此List类型的子对象似乎是不可避免的。

A new day with a new head... I changed the mappings like the following and it works as expected: 新的一天有了新的头……我更改了如下所示的映射,并且可以按预期运行:

        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Book, BookForDetailsDto>()
                .ForMember(dto => dto.Authorss, opt => opt.MapFrom(x => x.Authors.Select(a => a.Author)));
            cfg.CreateMap<BookAuthor, BookForAuthorListingDto >()
                .ForMember(res => res.Id, opt => opt.MapFrom(dto => dto.Book.Id))
                .ForMember(res => res.Name, opt => opt.MapFrom(dto => dto.Book.Name));
        });

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

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