物体内部的自动映射集合

我是AutoMapper工具的新手,到目前为止BTW还是很棒的.我在映射模型和相应的ViewModel对象内部的集合时遇到了困难.

为简单起见,我修剪了以下代码:

模型:

public class VoteQuestion
{        
    public virtual ICollection<VoteAnswerOption> VoteAnswerOptions { get; set; }
}

对应的ViewModel:

public class CreateVoteQuestionViewModel
{        
    public List<VoteAnswerOptionViewModel> PossibleAnswers { get; set; }
}

另一个型号:

public class VoteAnswerOption
{
    public string Answer { get; set; }
}

并对应ViewModel:

public class VoteAnswerOptionViewModel
{
    public string Answer { get; set; }
}

在“ Startup.cs”中设置我的Mapper.尝试了一些选项,除了映射集合外,它还适用于其他所有功能.

        Mapper.Initialize(config =>
        {          
            config.CreateMap<VoteAnswerOption, VoteAnswerOptionViewModel>().ReverseMap();

            config.CreateMap<List<VoteAnswerOptionViewModel>, ICollection<VoteAnswerOption>>().ReverseMap();

            config.CreateMap<VoteQuestion, CreateVoteQuestionViewModel>()
            .ForMember(dest => dest.PossibleAnswers, opts => opts.MapFrom(src => src.VoteAnswerOptions))
            .ForMember(dest=>dest.PossibleAnswers,opts=>opts.MapFrom(src=>Mapper.Map<ICollection<VoteAnswerOption>, List<VoteAnswerOptionViewModel>>(src.VoteAnswerOptions)))
            .ReverseMap();
        });

最后,我的Controller动作中的映射:

var newQuestion = Mapper.Map<CreateVoteQuestionViewModel, VoteQuestion>(voteQuestion);

我想念什么?

解决方法:

该测试通过:请注意,仅当映射简单且不包含任何ForMember调用时,才可以使用ReverseMap().

public class VoteQuestion {
    public virtual ICollection<VoteAnswerOption> VoteAnswerOptions { get; set; }
}


public class CreateVoteQuestionViewModel {
    public List<VoteAnswerOptionViewModel> PossibleAnswers { get; set; }
}


public class VoteAnswerOption {
    public string Answer { get; set; }
}


public class VoteAnswerOptionViewModel {
    public string Answer { get; set; }
}


[TestFixture]
public class SOTests {
    [Test]
    public void Test_41247396() {
        Mapper.Initialize(config => {
            config.CreateMap<VoteAnswerOption, VoteAnswerOptionViewModel>().ReverseMap();

            config.CreateMap<VoteQuestion, CreateVoteQuestionViewModel>()
                .ForMember(dest => dest.PossibleAnswers, 
                           opts => opts.MapFrom(src => src.VoteAnswerOptions));

            config.CreateMap<CreateVoteQuestionViewModel, VoteQuestion>()
                .ForMember(dest => dest.VoteAnswerOptions, 
                           opts => opts.MapFrom(src => src.PossibleAnswers));

        });

        var voteQuestion = new VoteQuestion {
            VoteAnswerOptions = new List<VoteAnswerOption> {
                new VoteAnswerOption { Answer = "Correct" }
            }
        };

        var newQuestion = Mapper.Map<VoteQuestion, CreateVoteQuestionViewModel>(voteQuestion);
        newQuestion.PossibleAnswers.Count.Should().Be(1);
        newQuestion.PossibleAnswers.Single().Answer.Should().Be("Correct");

        var vm = new CreateVoteQuestionViewModel {
            PossibleAnswers = new List<VoteAnswerOptionViewModel> {
                new VoteAnswerOptionViewModel {Answer = "Spot on"}
            }
        };

        var q = Mapper.Map<CreateVoteQuestionViewModel, VoteQuestion>(vm);
        q.VoteAnswerOptions.Count.Should().Be(1);
        q.VoteAnswerOptions.Single().Answer.Should().Be("Spot on");

    }
}
上一篇:c#-Automapper将int属性值从List映射到List


下一篇:C#-所有成员的Automapper规范