Last active
December 29, 2017 12:52
-
-
Save MaLiN2223/990e42299dc7790c02f91179c9715c96 to your computer and use it in GitHub Desktop.
Extensions for automapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class AutomapperExtensions | |
{ | |
public static IMappingExpression<TSource, TDestination> Ignore<TSource, TDestination>( | |
this IMappingExpression<TSource, TDestination> map, | |
Expression<Func<TDestination, object>> selector) | |
{ | |
map.ForMember(selector, options => options.Ignore()); | |
return map; | |
} | |
public static IMappingExpression<TSource, TDestination> Define<TSource, TDestination>( | |
this IMappingExpression<TSource, TDestination> map, | |
Expression<Func<TDestination, object>> selector1, | |
Expression<Func<TSource, object>> selector2) | |
{ | |
return map.ForMember(selector1, options => options.MapFrom(selector2)); | |
} | |
public static IMappingExpression<TSource, TDestination> Define<TSource, TDestination, TType>( | |
this IMappingExpression<TSource, TDestination> map, | |
Expression<Func<TDestination, TType?>> selector1, | |
Expression<Func<TSource, TType>> selector2) where TType : struct | |
{ | |
var expr = selector2.Compile(); | |
return map.ForMember(selector1, x => x.MapFrom(y => (TType?)expr(y))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment