AutoMapper Dependency Injection with ITypeConverter

Viewed 2439

I'm trying to use Automapper to convert all my DateTimes from UTC to Local Time but i need to inject an interface into ITypeConverter... I'm receiving this error when I run the application: MissingMethodException: No parameterless constructor defined for this object.

I think that the problem is with the dependency injection code!

Anyone can help me?

UserRepository:

public class UserRepository : IUserRepository
{
    private static readonly MapperConfiguration Config = new MapperConfiguration(cfg =>
    {
        cfg.CreateMap<User, User>();

        cfg.CreateMap<DateTime?, DateTime?>().ConvertUsing<UtcToLocalConverter>();
    });

    public List<User> GetById(string[] ids)
    {
        var result = BuildQuery().Where(w => ids.Contains(w.UserName)).ToList();

        var mapper = Config.CreateMapper();

        return mapper.Map<List<User>>(result); // Throws error !!!
    }

    #region Helper

    private IQueryable<User> BuildQuery()
    {
        return Settings.EpedDb.User;
    }

    #endregion
}

Converter

public class UtcToLocalConverter : ITypeConverter<DateTime?, DateTime?>
{
    public UtcToLocalConverter(IBaseSettings baseClass) // I tried to inject here!!!
    {
        Settings = baseClass;
    }

    private IBaseSettings Settings { get; }

    public DateTime? Convert(DateTime? source, DateTime? destination, ResolutionContext context)
    {
        if (source == null) return null;

        var tzi = TimeZoneInfo.FindSystemTimeZoneById(Settings.UserData.TimeZone);
        return TimeZoneInfo.ConvertTime(DateTime.SpecifyKind((DateTime)source, DateTimeKind.Utc), tzi);
    }
}
2 Answers
Related