Skip to content

Instantly share code, notes, and snippets.

@MaLiN2223
Last active December 29, 2017 12:52
Show Gist options
  • Save MaLiN2223/990e42299dc7790c02f91179c9715c96 to your computer and use it in GitHub Desktop.
Save MaLiN2223/990e42299dc7790c02f91179c9715c96 to your computer and use it in GitHub Desktop.
Extensions for automapper
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