具有复杂类型的AutoMapper



我在尝试使用AutoMapper的复杂类型时遇到了问题。

我有两个对象,一个域和一个ViewModel在我的视图中使用;

ViewModel

public class RegisterCoupleModel
{
    [Required(ErrorMessage = "campo obrigatório")]
    [Display(Name = "Primeiro nome", Order = 0, GroupName = "Noivo")]
    [StringLength(60)]
    public string FistNameGroom
    {
        get;
        set;
    }
    [Required(ErrorMessage = "campo obrigatório")]
    [Display(Name = "Último nome", Order = 1, GroupName = "Noivo")]
    [StringLength(60)]
    public string LastNameGroom
    {
        get;
        set;
    }
    [Required(ErrorMessage = "campo obrigatório")]
    [RegularExpression(@"^[w-.]+@([w-]+.)+[w-]{2,4}$", ErrorMessage = "Formato de email inválido.")]
    [DataType(DataType.EmailAddress)]
    [Remote("IsEmailAvailable", "Validation", "", ErrorMessage = "este email já está sendo usado por outro usuário.")]
    [Display(Name = "Email", Order = 2, GroupName = "Noivo")]
    [StringLength(180)]
    public string EmailGroom
    {
        get;
        set;
    }
    [Required(ErrorMessage = "campo obrigatório")]
    [Display(Name = "Primeiro nome", Order = 0, GroupName = "Noiva")]
    [StringLength(60)]
    public string FistNameBride
    {
        get;
        set;
    }
    [Required(ErrorMessage = "campo obrigatório")]
    [Display(Name = "Último nome", Order = 1, GroupName = "Noiva")]
    [StringLength(60)]
    public string LastNameBride
    {
        get;
        set;
    }
    [Required(ErrorMessage = "campo obrigatório")]
    [RegularExpression(@"^[w-.]+@([w-]+.)+[w-]{2,4}$", ErrorMessage = "Formato de email inválido.")]
    [DataType(DataType.EmailAddress)]
    [Remote("IsEmailAvailable", "Validation", "", ErrorMessage = "este email já está sendo usado por outro usuário.")]
    [Display(Name = "Email", Order = 2, GroupName = "Noiva")]
    [StringLength(180)]
    public string EmailBride
    {
        get;
        set;
    }
    [Required(ErrorMessage = "campo obrigatório")]
    [Display(Name = "Url")]
    [RegularExpression(@"^[a-zA-Z0-9_-.+]{5,22}$", ErrorMessage = "Chave inválida")]
    [StringLength(22, MinimumLength = 5, ErrorMessage = "A chave deve ter entre {0} e {1} caracteres.")]
    [Remote("IsKeywordAvaliable", "Validation")]
    public string UrlKeyword
    {
        get;
        set;
    }
    [Display(Name = "Voce é humano?")]
    [UIHint("ReCaptcha")]
    public string ReCaptcha
    {
        get;
        set;
    }
}

域对象

public class Couple
{
    [Key]
    public Guid Id { get; set; }
    public string UrlKeyword { get; set; }
    public virtual Partner Groom { get; set; }
    public virtual Partner Bride { get; set; }
    public DateTime Marriage { get; set; }
    public DateTime Dating { get; set; }
    public DateTime Engagement { get; set; }
    public virtual ICollection<User> Users { get; set; }
}

如果你看一下,你会发现我的对象Couple域具有属性BrideGroom,它们实际上是相同类型

如何将域对象Couple映射到RegisterCoupleModel ?

这里我只写了:

在settings中,automapper尝试做如下操作:

CreateMap<RegisterCoupleModel, Couple>()
    .ForMember(dest => dest.Bride.User.FirstName, opt => opt.MapFrom(source => source.FistNameBride))
    .ForMember(dest => dest.Bride.User.LastName, opt => opt.MapFrom(source => source.LastNameBride))
    .ForMember(dest => dest.Bride.User.Email, opt => opt.MapFrom(source => source.EmailBride))
    .ForMember(dest => dest.Groom.User.FirstName, opt => opt.MapFrom(source => source.FistNameGroom))
    .ForMember(dest => dest.Groom.User.LastName, opt => opt.MapFrom(source => source.LastNameGroom))
    .ForMember(dest => dest.Groom.User.Email, opt => opt.MapFrom(source => source.EmailGroom));

但是显示如下错误:

表达式'dest => dest. bride . user。FirstName'必须解析为顶级成员。参数名称:lambdaExpression

我知道这需要尝试使用嵌套类型映射属性的行为。

但是我怎么能把RegisterCoupleModel映射到Couple和两个属性BrideGroom是相同的类型?

我在StackOverflow上发现了一个问题,看起来像这样,但它帮助了我。

我要做的是封装属性FirstNameBrideLastNameBrideEmailBride firstname新郎 lastname新郎 email新郎到一个嵌套的视图模型类型,让我们说PersonDetails:

public class PersonDetails
{
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public string Email { get; set; }
}

并相应地更新父VM:

public class RegisterCoupleModel
{
   public PersonDetails GroomDetails { get; set; }
   public PersonDetails BrideDetails { get; set; }
}

然后您可以提供从PersonDetailsUser(或任何类型的新郎)的映射。用户):

Mapper.CreateMap<PersonDetails,User>();

请注意我是如何得到如此显式的属性映射的->因为字段在源和目标中具有相同的名称,所以不需要显式映射。只要有可能,就尽量这样做。少代码->好代码。

那么除了上面的映射之外,只需这样做:

Mapper.CreateMap<RegisterCoupleModel, Couple>();

并且AutoMapper将看到RegisterCoupleModel有两个PersonDetails对象,看到它已经有一个映射定义,然后自动使用它。

应该可以(我以前做过)。

你不应该总是使用平面视图模型,在必要的地方嵌套它们(比如重用字段)。

相关内容

  • 没有找到相关文章

最新更新