S
S
Saharman2019-10-25 18:07:41
ASP.NET
Saharman, 2019-10-25 18:07:41

How can I convert nested object in Automapper?

Good evening! I have two classes that are connected in a many-to-many relationship. The structure is like this:

class A
{
     public class A
    {
        public Guid Id { get; set; }

        public List<AB> AB { get; set; }
    }

    public class B
    {
        public Guid Id { get; set; }

        public List<AB> AB { get; set; }
    }

    public class AB
    {
        public Guid AId { get; set; }

        public Guid BId { get; set; }

        public A A { get; set; }

        public B B { get; set; }

        public string Status { get; set; }
    }

    public class ADto
    {
        public Guid Id { get; set; }

        public List<BDto> B { get; set; }

        public string Status { get; set; }
    }

    public class BDto
    {
        public Guid Id { get; set; }

        public List<ADto> A { get; set; }

        public string Status { get; set; }
    }
}

I want to use Automapper in my project. How can I explain that, for example, for the ADto object, I must take all objects B from the linking tables, and also take the status for B from the linking table and place it in BDto? That is, in theory, in ForMember I can specify that I map objects B from the linking tables to the list B, but how can I explain in the Automapper setting that I also want to convert this B to BDto using, again, automapper?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arthur Mustafin, 2019-11-04
@virtual_hack2root

No way, stop using the automapper for other purposes, in ForMember you can not use custom scripts, code, or properties of the second nesting level, as well as LINQ expressions containing code. You can write your own custom mapping, but it will not support compile-time optimization, that is, it will be executed at runtime like regular code, and there will be no advantages over regular code, that is, besides this, the readability of such code will be below the plinth.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question