AutoMapper 8.0 missing GetPropertyMaps

2020-07-22 18:42发布

问题:

Prior to AutoMapper 8.0, I used this code to find a property mapping by string, example: entity model has property named "currency_id" and DTO has property named "currency". I have defined bi-directional mapping in AutoMapper, and I used this code to find source/target property relat

    public static string GetDestinationPropertyFor<TSrc, TDst>(IMapper IMapper, string sourceProperty)
    {
        var mapper = AutoMapper.IMapper.ConfigurationProvider;

        // TSrc = source generic type
        // TDst = destination generic type
        var map = mapper.FindTypeMapFor<TSrc, TDst>();

        var propertyMap = map.GetPropertyMaps()
                              .FirstOrDefault(pm => 
                                    pm.SourceMember.Name == sourceProperty
                              );


        return propertyMap.DestinationProperty.Name;
    }

In AutoMapper Profile:

        this.CreateMap<EntityModels.contact, DTO.contact>()
            .ForMember(m => m.currency, src => src.MapFrom(f => f.currency_id))
        ;

        this.CreateMap<DTO.contact, EntityModels.contact>()
            .ForMember(m => m.currency_id, src => src.MapFrom(f => f.currency))
        ;

When I called my method like this:

var _dboField = GetDestinationPropertyFor<DTO.contact, EntityModels.contact>(this.mapper, "currency");

Console.WriteLine(_dboField);
// output should be "currency_id"

After upgrading to AutoMapper 8.0 I got this error at build:

'TypeMap' does not contain a definition for 'GetPropertyMaps' and no accessible extension method 'GetPropertyMaps' accepting a first argument of type 'TypeMap' could be found (are you missing a using directive or an assembly reference?)

What are replacements for GetPropertyMaps in AutoMapper 8.0?

Thanks!

回答1:

As Lucian suggested, MemberMaps is a possible replacement. However, PropertyMaps does exactly the same as GetPropertyMaps in AutoMapper 7.0. DestinationProperty has also been renamed to DestinationMember.

AutoMapper 7.0 code:

public static string GetDestinationPropertyFor<TSrc, TDst>(IMapper IMapper, string sourceProperty)
    {
        var mapper = AutoMapper.IMapper.ConfigurationProvider;

        // TSrc = source generic type
        // TDst = destination generic type
        var map = mapper.FindTypeMapFor<TSrc, TDst>();

        var propertyMap = map.GetPropertyMaps()
                              .FirstOrDefault(pm => 
                                    pm.SourceMember.Name == sourceProperty
                              );


        return propertyMap.DestinationProperty.Name;
    }

AutoMapper 8.0 code:

public static string GetDestinationPropertyFor<TSrc, TDst>(IMapper IMapper, string sourceProperty)
    {
        var mapper = AutoMapper.IMapper.ConfigurationProvider;

        // TSrc = source generic type
        // TDst = destination generic type
        var map = mapper.FindTypeMapFor<TSrc, TDst>();

        var propertyMap = map.PropertyMaps
                              .FirstOrDefault(pm => 
                                    pm.SourceMember.Name == sourceProperty
                              );


        return propertyMap.DestinationMember.Name;
    }