c# - 对象内部的自动映射集合

标签 c# asp.net-mvc automapper

我是 AutoMapper 工具的新手,顺便说一句,到目前为止它非常棒。我遇到了在 Model 和相应的 ViewModel 对象中映射集合的困难。

为简单起见,我对代码进行了整理:

型号:

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

对应的 View 模型:

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.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# - 对象内部的自动映射集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41247396/

相关文章:

c# - 如何在不使用 AutoMapper 为覆盖属性的每个派生类显式映射的情况下映射虚拟属性?

c# - 如何将服务注入(inject)AutoMapper?

c# - 使用 ASP.NET Core MVC 根据从下拉列表中选择的值填充文本框

asp.net - 缩小失败。返回未缩小的内容

asp.net - 架构设计良好的 ASP.NET WebForms 站点示例

c# - 如何在 C# 中使用返回对象的 ASP.NET Web API

c# - Automapper,映射异常

c# - 使用 C# 运行带有关键字 GO 的 .sql 语句?

c# - 在不使用 .Result 的情况下如何在 C# 中不能异步的方法中调用异步方法

c# - 获取给定星期年份、给定月份和给定星期的开始和结束日期