0

I want to transform two properties of base classes using ConvertUsing, but it is not being called

Base classes

public abstract class BaseDadoMestreViewModel
{
    public DateTime UltimaAtualizacao { get; set; }
    public bool Excluido { get; set; }
    public bool Ativo { get; set; }
}

public abstract class BaseDadoMestre<TEntity> : EntityCrud<TEntity> 
    where TEntity : class
{
    public DateTime UltimaAtualizacao { get; set; }
    public string MarcadoEliminacao { get; set; }
    public bool Desabilitado { get; set; }
}

Classes

public class CalendarioViewModel: BaseDadoMestreViewModel
{
    public DateTime Data { get; set; }
}

public class CalendarioDTO: BaseDadoMestre<CalendarioDTO>
{
    public DateTime Data { get; set; }
}

ITypeConverter

public class BaseConverter<TEntity> : 
    ITypeConverter<BaseDadoMestre<TEntity>, BaseDadoMestreViewModel>,
    ITypeConverter<BaseDadoMestreViewModel, BaseDadoMestre<TEntity>> 
    where TEntity : BaseDadoMestre<TEntity>
{
    public BaseDadoMestreViewModel Convert(BaseDadoMestre<TEntity> source, BaseDadoMestreViewModel destination, ResolutionContext context)
    {
        destination.Excluido = (source.MarcadoEliminacao== "X");
        destination.Ativo = !source.Desabilitado;

        return destination;
    }

    public BaseDadoMestre<TEntity> Convert(BaseDadoMestreViewModel source, BaseDadoMestre<TEntity> destination, ResolutionContext context)
    {
        destination.MarcadoEliminacao = (source.Excluido ? null : "X");
        destination.Desabilitado = !source.Ativo;

        return destination;
    }
}

And finally my map:

CreateMap(typeof(BaseDadoMestre<>), typeof(BaseDadoMestreViewModel)).ConvertUsing(typeof(BaseConverter<>));
CreateMap(typeof(BaseDadoMestreViewModel), typeof(BaseDadoMestre<>)).ConvertUsing(typeof(BaseConverter<>));
CreateMap<CalendarioDTO, CalendarioViewModel>()

When I run the mapping command ConvertUsing is not called.

mapper.Map<CalendarioViewModel>(new CalendarioDTO() { MarcadoEliminacao = "X", Desabilitado = true });
Marcelo
  • 1
  • 1
  • Make for your dtos a non generic class similar to BaseDadoMestreViewModel. Define a map between the non generic classes and then use Include or IncludeBase as suggested below. Use MapFrom instead of ConvertUsing. – Lucian Bargaoanu Aug 26 '18 at 14:00

1 Answers1

0

As from Automapper docs you need to either specify the inheritance when creating the mapping for base classes either from derived.

Check this one:

http://docs.automapper.org/en/stable/Mapping-inheritance.html

enter image description here

Selvirrr
  • 146
  • 7
  • I tried this and it does not work, it still does not run the ITypeConverter. I need to implement ITypeConverter because they are generic classes and I can not write the transformations without it – Marcelo Aug 26 '18 at 02:46
  • Seems that you can not combine Inheritance with ConvertUsing... Check the answer here:https://stackoverflow.com/questions/37448383/automapper-inheritance-mapper-not-working-with-type-converter/37450718 You'll probably need to do mapping in each derived type for "Excluido" and "Ativo" – Selvirrr Aug 26 '18 at 14:01
  • Thank, I searched and could not find it, but someone already had the answer I needed – Marcelo Aug 26 '18 at 16:52
  • This does not seem to address the question, like at all – JSON Jun 19 '20 at 15:15