Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ITypeConverter interface has been changed in AutoMapper 2.0

The ITypeConverter interface has been changed to have a "TDestination Convert(ResolutionContext context)" instead of "TDestination Convert(TSource source)" for the Convert method.

http://automapper.codeplex.com/wikipage?title=Custom%20Type%20Converters

In my code, now I get this error:

'BusinessFacade.Mappers.DecimalToNullableInt' does not implement interface member 'AutoMapper.ITypeConverter.Convert(AutoMapper.ResolutionContext)'

Any good full sample for new mapper like my mappers ? I don't want change any code (or minimum code) in my projects...

My mapper

 public class DecimalToNullableInt : ITypeConverter<decimal, int?>
    {
        public int? Convert(decimal source)
        {
            if (source == 0)
                return null;
            return (int)source;
        }
    }

UPDATE

The ITypeConverter interface has been changed to have a "TDestination Convert(ResolutionContext context)" instead of "TDestination Convert(TSource source)" for the Convert method.

the documentation is just out of date. There is an ITypeConverter, as well as a base TypeConverter convenience class. The TypeConverter hides the ResolutionContext, while ITypeConverter exposes it.

http://automapper.codeplex.com/wikipage?title=Custom%20Type%20Converters

https://github.com/AutoMapper/AutoMapper/wiki/Custom-type-converters

http://groups.google.com/group/automapper-users/browse_thread/thread/6c523b95932f4747

like image 503
Kiquenet Avatar asked Nov 17 '11 14:11

Kiquenet


1 Answers

You'll have to grab the decimal from the ResolutionContext.SourceValue property:

    public int? Convert(ResolutionContext context)
    {
        var d = (decimal)context.SourceValue;
        if (d == 0)
        {
            return null;
        }
        return (int) d;
    }
like image 95
PatrickSteele Avatar answered Sep 19 '22 20:09

PatrickSteele