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
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.
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.
– Leonel Sanches da Silva
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.
– Bruno Leite
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?
– Leonel Sanches da Silva
How’s your controller that passes the data to View?
– Edvaldo Farias
@Thank you for your reply, but I will not abandon the DDD strategy.
– Bruno Leite
@Edvaldofarias follows my Actionresult [Route("list")] public Actionresult Index() { Return View(_Usuarioappservice.Get all(); }
– Bruno Leite
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.
– Edvaldo Farias
The error message has nothing to do with its object being null. Simply the error says that its class
UsuarioAtivosViewModel
does not contain a PropertyLocalAtendimentoUsuario
, 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 calledLocalAtendimentoUsuario
. The name of yourCartaoCreditoUsuarioAtivosViewModel
is calledCartaoCreditoUsuario
, and notLocalAtendimentoUsuario
. Fix your view and try again.– Alisson
@Brunoleite, solved the problem ?
– Renan Carlos