Поведение AutoMapper с доступными только для чтения свойствами в источнике, не сопоставленном с назначением - PullRequest
0 голосов
/ 17 января 2020

У меня есть конфигурация, которая выполняет обратное сопоставление, чтобы разложить исходный объект в целевой объект, однако, если свойство исходного объекта доступно только для чтения, оно автоматически игнорируется, и мне приходится вручную сопоставлять его.

Некоторые из моих исходных объектов имеют до 100 полей, поэтому создание ручных сопоставлений кажется громоздким, и я не уверен, почему такое поведение существует в автоматическом сопоставлении.

См. Ниже фрагмент кода для рабочего примера того, чем я являюсь описание.

using System;
using AutoMapper;

namespace automappertests
{
    class Source
    {
        public int Field1Suffix1 { get; set; }
        public int Field1Suffix2 { get; set; }
        public int Field2Suffix1 { get; set; }

        // !!Attention!!
        // This is a readonly property with a private setter
        public int Field2Suffix2 => Field2Suffix1; 
    }

    class Destination
    {
        public Wrapper Field1 { get; set; }
        public Wrapper Field2 { get; set; }
    }

    class Wrapper
    {
        public int Suffix1 { get; set; }
        public int Suffix2 { get; set; }

        public static Wrapper New(int suffix1, int suffix2) =>
            new Wrapper
            {
                Suffix1 = suffix1,
                Suffix2 = suffix2
            };
    }

    class DestinationProfile : Profile
    {
        public DestinationProfile()
        {
            CreateMap<Destination, Source>()
                .ReverseMap()
                // !!Attention!!
                // Why do I need to have an explicit ForMember MapFrom for the readonly property?
                .ForMember(m => m.Field2, o => o.MapFrom(src => Wrapper.New(src.Field2Suffix1, src.Field2Suffix2)));
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var config = new MapperConfiguration(cfg => { cfg.AddProfile<DestinationProfile>(); });
            var mapper = config.CreateMapper();
            var source = new Source()
            {
                Field1Suffix1 = 1,
                Field1Suffix2 = 2,
                Field2Suffix1 = 3,
            };
            var destination = mapper.Map<Source, Destination>(source);
            Console.WriteLine($"Field1.Suffix1 = {destination.Field1.Suffix1}");
            Console.WriteLine($"Field1.Suffix2 = {destination.Field1.Suffix2}");
            Console.WriteLine($"Field2.Suffix1 = {destination.Field2.Suffix1}");
            Console.WriteLine($"Field2.Suffix2 = {destination.Field2.Suffix2}");
            Console.WriteLine("Press any key to continue.");
            Console.ReadKey();
        }
    }
}
Output without .ForMember:
Field1.Suffix1 = 1
Field1.Suffix2 = 2
Field2.Suffix1 = 3
Field2.Suffix2 = 0
Output with .ForMember:
Field1.Suffix1 = 1
Field1.Suffix2 = 2
Field2.Suffix1 = 3
Field2.Suffix2 = 3

1 Ответ

0 голосов
/ 17 января 2020

На самом деле у вас нет сеттера. Добавить явный закрытый сеттер.

public int Field2Suffix2 { get => Field2Suffix1; private set => Field2Suffix1 = value; }
...