Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 460
Juan Calderon Avatar asked Dec 05 '18 17:12

Juan Calderon


2 Answers

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)) 
like image 69
Paw Baltzersen Avatar answered Oct 07 '22 11:10

Paw Baltzersen


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.

like image 40
Marcello R. Avatar answered Oct 07 '22 10:10

Marcello R.