c# - AutoMapper-从ViewModel映射回DTO时,subObject保持为空

标签 c# mvvm automapper automapper-3

我有这张 map :

Mapper.Initialize(cfg =>
{
    cfg.CreateMap<Sistema.DataEntities.Models.Cliente, CadastroModule.Model.Cliente>().ReverseMap();
});

CadastroModule.Model.Cliente(MVVM模型):
public class Cliente
{
    public int ClienteID { get; set; }
    public string Nome { get; set; }
    public string EnderecoCEP { get; set;}
    public string EnderecoBairro { get; set; }
    public string EnderecoLogradouro { get; set; }
    public int EnderecoNumero { get; set; }
    public string EnderecoComplemento { get; set; }
}

Sistema.DataEntities.Models(POCO):
public class Cliente : Entity
{
    public Cliente()
    {
        Endereco = new Endereco();
    }

    public int ClienteID { get; set; }

    public string Nome { get; set; }

    public Endereco Endereco { get; set; }//1 pra 1 (Complex type EF)
}

Sistema.DataEntities.Models(POCO):
public class Endereco : Entity
{
    public string CEP { get; set; }
    public string Bairro { get; set; }
    public string Logradouro { get; set; }
    public int Numero { get; set; }
    public string Complemento { get; set; }

}

当我尝试它运行完美时:
var t = _clienteService.ClienteService_GetAll().Project().To<Cliente>();

但是当我需要回到poco时会遇到麻烦:
Sistema.DataEntities.Models.Cliente clifinal = Mapper.Map<Cliente, Sistema.DataEntities.Models.Cliente>(ObCliente);

我的结果:

为什么Endereco对象具有空值?

最佳答案

Automapper并不是为“展开”对象而构建的。您的模型-> ViewModel映射按惯例工作;目标中的“Endereco”前缀告诉AutoMapper查找嵌套对象。

为了使它以另一种方式起作用,您必须手动添加从CadastroModule.Model.ClienteSistema.DataEntities.Models.Endereco的映射,然后在从CadastroModule.Model.ClienteSistema.DataEntities.Models.Cliente的反向映射中使用该映射:

cfg.CreateMap<Cliente, CadastroModule.Model.Cliente>()
    .ReverseMap()
    // Map the `Endereco` property from `Cliente`
    .ForMember(dest => dest.Endereco, opt => opt.MapFrom(src => src));

cfg.CreateMap<CadastroModule.Model.Cliente, Endereco>();
    .ForMember(dest => dest.Bairro, opt => opt.MapFrom(src => src.EnderecoBairro))
    .ForMember(dest => dest.CEP, opt => opt.MapFrom(src => src.EnderecoCEP))
    .ForMember(dest => dest.Complemento, opt => opt.MapFrom(src => src.EnderecoComplemento))
    .ForMember(dest => dest.Logradouro, opt => opt.MapFrom(src => src.EnderecoLogradouro))
    .ForMember(dest => dest.Numero, opt => opt.MapFrom(src => src.EnderecoNumero));

除了映射Endereco的每个属性,您还可以将Endereco注册为源前缀:
cfg.CreateMap<Cliente, CadastroModule.Model.Cliente>()
    .ReverseMap()
    .ForMember(dest => dest.Endereco, opt => opt.MapFrom(src => src));

cfg.RecognizePrefixes("Endereco");
cfg.CreateMap<CadastroModule.Model.Cliente, Endereco>();

提出的这种解决方案,我认为the author's post on two way mapping值得一读。本质上,AutoMapper并不是为映射回域实体而构建的。当然,您可以随意使用它,但这可以解释为什么不立即支持这种情况。

关于c# - AutoMapper-从ViewModel映射回DTO时,subObject保持为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25435644/

相关文章:

c# - 将word文档转换为xml代码c#

c# - 数据绑定(bind)到不同 UserControl 中的 ObservableCollection - 如何保留当前选择?

c# - Automapper,映射配置不持久

c# - AutoMapper 的 AssertConfigurationIsValid 仅在第一次加载时失败?

c# - 如何在 AutoMapper 中映射这种父子关系?

c# - C# (.NET) 中的 string.Length 是即时变量吗?

c# - 从 C# 到 PHP 的 HMACSHA1

c# - WCF 客户端对象反序列化通知

c# - ViewModel 与 Model 有不同的字段吗?

java - 在 BaseFragment 中使用 dagger2 实例化 View 模型