FindTypeMapFor method not exists in AutoMapper 11
Asked Answered
E

2

5

I am currently upgrading a web API that was developed on NET Core 5.0 and upgrading it to NET Core 6.0. When upgrading the NuGet AutoMapper package to version 11.0.1, I find that the FindTypeMapFor method does not exist in the ConfigurationProvider definition.

public Dictionary<string, PropertyMappingValue> GetPropertyMappingFromAutomapper<TSource, TDestination>(List<string> reverseOrderProperties) where TSource : class where TDestination : class
    {
        Dictionary<string, PropertyMappingValue> dictionaryPropertyMapping = new(StringComparer.OrdinalIgnoreCase);

        if (typeof(TSource).Equals(typeof(ForNotIncludeDto)) || typeof(TSource).Equals(typeof(ForNotSortingDto)) || typeof(TSource).Equals(typeof(ForNotDistinctDto)))
        {
            return dictionaryPropertyMapping;
        }

        TypeMap typeMap = this.Mapper.ConfigurationProvider.FindTypeMapFor<TSource, TDestination>();

        if (typeMap is null)
        {
            throw new Exception($"Cannot find exact property mapping instance " + $"for <{typeof(TSource)},{typeof(TDestination)}>");
        }

        List<PropertyMap> propertyMaps = typeMap.PropertyMaps.Where(x => x.Ignored == false).ToList();
        List<PathMap> pathMaps = typeMap.PathMaps.Where(x => x.Ignored == false).ToList();

        foreach (MemberInfo member in typeMap.SourceTypeDetails.AllMembers)
        {
            List<string> originPropertyMap = propertyMaps.Where(x => x.SourceMember is not null && x.SourceMember.Name.Equals(member.Name)).Select(x => x.DestinationName).ToList();

            if (originPropertyMap.Count.Equals(0))
            {
                originPropertyMap = pathMaps.Where(x => x.SourceMember is not null && x.SourceMember.Name.Equals(member.Name)).Select(x => x.DestinationName).ToList();
            }

            if (originPropertyMap.Count > 0)
            {
                dictionaryPropertyMapping.Add(member.Name, new PropertyMappingValue(originPropertyMap, reverseOrderProperties.Where(x => x.Equals(member.Name)).Any()));
            }
        }

        return dictionaryPropertyMapping;
    }

How can I get the TypeMap object of a specific mapping?

How can I use FindTypeMapFor or some method that will replace it?

Exile answered 7/3, 2022 at 1:58 Comment(8)
What are you trying to achieve with that method?Fleshpots
Check if there is a mapping for TSource and TDestination, then get the property mapping. List<PropertyMap> propertyMaps = typeMap.PropertyMaps.Where(x => x.Ignored == false).ToList();Sara
Yes, but why are you touching AM's internal data?Fleshpots
To return a dictionary with the mapping of priorities and thus verify if there is a mapping between the DTO -> TDestination and the Entity -> TSource; For example, in the API request the client requests to order the information by a property of the DTO, that query is taken to the database and the mapping between the DTO and the Entity allows me to know that property A in the DTO corresponds to Property C of the Entity.Sara
You should look into expression mapping.Fleshpots
@LucianBargaoanu Please could you help me understand better with an example, thanks.Sara
Check the docs and the examples in the repo.Fleshpots
@LucianBargaoanu My use case for FindTypeMapFor: given a List<object>, identify and map those elements that can be mapped to type T in the more elegant way than trying it and catching AutoMapperMappingException.Excessive
S
16

This was moved to the "Internal" object on AutoMapper 11.

Here's the new usage:

using AutoMapper.Internal;

Mapper.ConfigurationProvider.Internal().FindTypeMapFor
Suitable answered 13/4, 2022 at 13:52 Comment(1)
Doesn't work. ConfigurationProvider is not a static field.Battlefield
T
3

I'm using Automapper v12.0.1, try using the following code.

using AutoMapper.Internal;
...
var internalAPI = InternalApi.Internal(configInstance);
var map = internalAPI.FindTypeMapFor(source.GetType(), destination.GetType());
Tajuanatak answered 24/4, 2023 at 6:45 Comment(1)
This is just a more verbose way to write the code above. So maybe just upvote that one instead? :)Fleshpots

© 2022 - 2024 — McMap. All rights reserved.