Skip to content

Instantly share code, notes, and snippets.

@kmorcinek
Last active April 7, 2016 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmorcinek/64641a11a4cd5b8c18d8 to your computer and use it in GitHub Desktop.
Save kmorcinek/64641a11a4cd5b8c18d8 to your computer and use it in GitHub Desktop.
AutoMapper convenient extensions for simple mapping
public static class AutoMapperExtensions
{
/// <summary>
/// Maps properties as ignored.
/// </summary>
public static IMappingExpression<TSource, TDestination> Ignore<TSource, TDestination>(
this IMappingExpression<TSource, TDestination> map,
Expression<Func<TDestination, object>> selector)
{
map.ForMember(selector, config => config.Ignore());
return map;
}
/// <summary>
/// Sets mapping from source property to destination property. Convenient extension method.
/// </summary>
public static IMappingExpression<TSource, TDestination> MapProperty<TSource, TDestination, TProperty>(
this IMappingExpression<TSource, TDestination> map,
Expression<Func<TSource, TProperty>> sourceMember,
Expression<Func<TDestination, object>> targetMember)
{
map.ForMember(targetMember, opt => opt.MapFrom(sourceMember));
return map;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment