In AutoMapper 8.0 missing ResolveUsing

Prior to AutoMapper 8.0, I have used this code:

CreateMap<ApplicationRole, RoleViewModel>()
.ForMember(d => d.Permissions, map => map.MapFrom(s => s.Claims))
.ForMember(d => d.UsersCount, map => map.ResolveUsing(s => s.Users?.Count ?? 0))
                    .ReverseMap();

The documentation says that you have to change ResolveUsing for MapFrom, but I have a Error "No propagation Null"

.ForMember(d => d.UsersCount, map => map.MapFrom(s => s.Users?.Count ?? 0))

How I have to resolve it?


Solution 1:

Replace ResolveUsing with MapFrom, and add one more input parameter to the lambda (TDestination).

.ForMember(d => d.UsersCount, map => map.MapFrom((s,d) => s.Users?.Count ?? 0))

Solution 2:

In AutoMapper 8.0 missing ResolveUsing

I also have the same issue and I'm using the following prototype of ResolveUsing:

void ResolveUsing(Func<TSource, TResult> mappingFunction);

Instead of replacing existing code, I preferred to create an extension method:

using System;
using AutoMapper;

namespace myLibrary.Extensions
{
    public static class AutoMapperCompatibilityExtensions
    {
        // Summary:
        //     Resolve destination member using a custom value resolver callback. Used instead
        //     of MapFrom when not simply redirecting a source member This method cannot be
        //     used in conjunction with LINQ query projection
        //
        // Parameters:
        //   resolver:
        //     Callback function to resolve against source type
        public static void ResolveUsing<TSource, TDestination, TMember, TResult>(this IMemberConfigurationExpression<TSource, TDestination, TMember> member, Func<TSource, TResult> resolver) => member.MapFrom((Func<TSource, TDestination, TResult>)((src, dest) => resolver(src)));
    }
}

Later, in my code, I simply referred the namespace:

using myLibrary.Extensions;

...
    ... map.ResolveUsing(s =>  ...
...

Hope this helps.

Solution 3:

You don't need to use this expression, you can "Users.Count" and it'll return 0 if the list is empty.