Automapper Entities Viewmodel

Asked

Viewed 433 times

5

I am with a scenario where my context returns a query referring to two entities User and Cartaocreditousuario, as the following scenario below. During automapper action on my Viewmodel, is not mapping the entity Cartaocreditousuario reference to its properties exists Usersviewmodel.

Dominio - Usuario

public class Usuario
{
    public Usuario()
    {
        Id = Guid.NewGuid().ToString();
        CartaoCreditoUsuario = new List<CartaoCreditoUsuario.CartaoCreditoUsuario>();
    }
    public string Id{ get; set; }
    public string Email{ get; set; }
    public virtual ICollection<CartaoCreditoUsuario.CartaoCreditoUsuario> CartaoCreditoUsuario { get; set; }
    ...
}

Dominio - Cartaocreditousuario

public class CartaoCreditoUsuario 
{
    public CartaoCreditoUsuario()
    {
        CartaoCreditoId = Guid.NewGuid();
    }

    public string Id { get; set; }
    public Guid CartaoCreditoId { get; set; }
    public bool Aura { get; set; }
    public bool Elo { get; set; }
    public bool Hipercard { get; set; }
    public bool MasterCard { get; set; }
    public bool Visa { get; set; }
    public bool AmericanExpress { get; set; }
    public virtual Usuario.Usuario Usuario { get; set; }        
}

Viewmodel - Cartaocreditousuarioativosviewmodel

public class CartaoCreditoUsuarioAtivosViewModel
{
    public bool Aura { get; set; }
    public bool Elo { get; set; }
    public bool Hipercard { get; set; }
    public bool MasterCard { get; set; }
    public bool Visa { get; set; }
    public bool AmericanExpress { get; set; }
}

Viewmodel - Usersviewmodel

public class UsuarioAtivosViewModel
{

    public UsuarioAtivosViewModel()
    {
        CartaoCreditoUsuario = new List<CartaoCreditoUsuarioAtivosViewModel>();
    }

    public string Id { get; set; }
    public  string Email { get; set; }

    public  ICollection<CartaoCreditoUsuarioAtivosViewModel> CartaoCreditoUsuario { get; set; } 
...   
}

DomainToViewModelMappingProfile

public class DomainToViewModelMappingProfile : Profile
{

  protected override void Configure()
    {
        CreateMap<Usuario, UsuarioViewModel>();
        CreateMap<Endereco, EnderecoViewModel>();           
        CreateMap<Usuario, RegisterViewModel>();
        CreateMap<Endereco, RegisterViewModel>();
        CreateMap<Usuario, UsuarioAtivosViewModel>();
        CreateMap<AtendimentoUsuario, RegisterViewModel>();
        CreateMap<LocalAtendimentoUsuario, RegisterViewModel>();
        CreateMap<CartaoCreditoUsuario, RegisterViewModel>();
        CreateMap<CartaoCreditoUsuario, CartaoCreditoUsuarioAtivosViewModel>();
        CreateMap<CartaoCreditoUsuario, UsuarioAtivosViewModel>();
    ...
    }


public class ViewModelToDomainMappingProfile : Profile
{
     protected override void Configure()
    {
        CreateMap<UsuarioViewModel, Usuario>();
        CreateMap<EnderecoViewModel, Endereco>();      
        CreateMap<RegisterViewModel, Usuario>();
        CreateMap<RegisterViewModel, Endereco>();
        CreateMap<UsuarioAtivosViewModel, Usuario>();
        CreateMap<UsuarioAtivosViewModel, CartaoCreditoUsuario>();
        CreateMap<RegisterViewModel, AtendimentoUsuario>();
        CreateMap<RegisterViewModel, CartaoCreditoUsuario>();
        CreateMap<RegisterViewModel, LocalAtendimentoUsuario>();
        CreateMap<CartaoCreditoUsuarioAtivosViewModel, CartaoCreditoUsuario>();
        ...
    }
}

As image below, I have the following return from my context

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

According to the images above, my return is according to the expected, however, at the time I access in my view the collection property Cartaocreditousuario, is presented an exception due to my object is null, because the same not mapped according to the other property of my viewmodel, as image below.

inserir a descrição da imagem aqui

I would like, if possible, some help from colleagues with this problem.

Dese I already thank in advance.

  • Is there a specific reason for using Automapper? In my view, the class of Model and Viewmodel are identical.

  • Thank you for your reply. Yes. My Viewmodel contains different domain data. Another situation that my Dataannotations are not used in the domain but in another layer in my Solution as other particularities. Thank you again for the answer.

  • And why attribute annotations are not in Model nor in Viewmodel, Yes in a third layer? There’s no need for that. I think that’s the main problem with your application not working. If I direct an answer, it will be in the sense of abandoning the DDD, the Automapper and this layering that only hinders. I’d like the answer anyway?

  • How’s your controller that passes the data to View?

  • @Thank you for your reply, but I will not abandon the DDD strategy.

  • @Edvaldofarias follows my Actionresult [Route("list")] public Actionresult Index() { Return View(_Usuarioappservice.Get all(); }

  • Then you need to do the mapping in the controller this way: Mapper.Map<Ienumerable<Usuario>, Ienumerable<Usuarioviewmodel>>(_usuarioApp.Getall()); - If not you will not be doing the mapping, this way the controller knows that it is to do the mapping at that moment what you do in the auto mapper and pre-mapping class.

  • The error message has nothing to do with its object being null. Simply the error says that its class UsuarioAtivosViewModel does not contain a Property LocalAtendimentoUsuario, and in fact in your example code (although I imagine you didn’t put everything, only the minimum necessary), you don’t actually have a Property called LocalAtendimentoUsuario. The name of your CartaoCreditoUsuarioAtivosViewModel is called CartaoCreditoUsuario , and not LocalAtendimentoUsuario. Fix your view and try again.

  • @Brunoleite, solved the problem ?

Show 4 more comments

1 answer

3


Looking briefly at the images you posted, I see the following problem:

Your Model view is a IEnumerable<UsuarioAtivosViewModel>. Obviously, a IEnumerable<T> does not have a property called LocalAtendimentoUsuario.

Suppose you fix this (e. g. @Model UsuarioAtivosViewModel), your code indicates that this view model also does not have a property called LocalAtendimentoUsuario.

I think it’s because of those inconsistencies that you’re making the mistake.

(of course, being that answer a year later, I think now has nothing to do with)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.