Automapper with Ioc

Asked

Viewed 87 times

1

public class Vigencia
{
    public DateTime DataInicialVigencia { get; set; }
    public DateTime DataFinalVigencia { get; set; }
    public Guid VeiculoId { get; set; }
    public Guid RastreadorId { get; set; }
}

public class Veiculo
{
    public Guid Id { get; set; }
    public string Marca { get; set; }
    public string Modelo { get; set; }
    public string Placa { get; set; }
}

public class Rastreador
{
    public Guid Id { get; set; }
    public string NumeroSerie { get; set; }
}

public interface IRastreador
{
    Rastreador ObterRastredorPorId(Guid Id);
}

public interface IVeiculo
{
    Rastreador ObterVeiculoPorId(Guid Id);
}

public class ClienteViewModel
{
    public string Marca { get; set; }
    public string Modelo { get; set; }
    public string Placa { get; set; }
    public List<Vigencia> ListVigencia { get; set; }
}

How do I map the Clienteviewmodel class using Automapper?

Note: Interfaces are already implemented using Simpleinjector as an Ioc containner.

1 answer

1


You can do it this way

    static void Main(string[] args)
    {
        Mapper.CreateMap<Veiculo, ClienteViewModel>();
        Mapper.CreateMap<List<Vigencia>, ClienteViewModel>()
            .ForMember(x => x.ListVigencia, y => y.MapFrom(s => s));

        var vig = new Vigencia
        {
            DataInicialVigencia = DateTime.Now,
            DataFinalVigencia = DateTime.Now,
            VeiculoId = Guid.NewGuid(),
            RastreadorId = Guid.NewGuid()
        };

        var vic = new Veiculo
        {
            Id = Guid.NewGuid(),
            Marca = "AAA",
            Modelo = "BBB",
            Placa = "CCC"
        };

        var map = Mapper.Map<ClienteViewModel>(vic);
        Console.WriteLine(map.Modelo);
        Console.WriteLine(map.Marca);
        Console.WriteLine(map.Placa);

        Mapper.Map<List<Vigencia>, ClienteViewModel>(new List<Vigencia> { vig }, map);

        foreach (Vigencia v in map.ListVigencia)
        {
            Console.WriteLine(v.RastreadorId);

        }
    }

Browser other questions tagged

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