I'm looking for the simplest / most elegant way to flatten a source object utilizing extension methods of the source object.
Source:
class Source
{
public int Value1 { get; set; }
public int Value2 { get; set; }
}
Extension method I'd like to elegantly map:
static class SourceExtensions
{
public static int GetTotal(this Source source)
{
return source.Value1 + source.Value2;
}
}
Destination:
class Destination
{
public int Value1 { get; set; }
public int Value2 { get; set; }
public int Total { get; set; }
}
Is there a better way than this (one where I don't have to call out every extension method)?
using NamespaceContainingMyExtensionMethods;
...
Mapper.CreateMap<Source, Destination>()
.ForMember(destination => destination.Total,
opt => opt.ResolveUsing(source => source.GetTotal()));
Something like:
Mapper.CreateMap<Source, Destination>()
.ResolveUsingExtensionsInNamespace("NamespaceContainingMyExtensionMethods");
I know I can use an inheritance heirarchy on the source objects, but in my situation, it isn't ideal.
I've researched: Does AutoMapper's convention based mappings work with LINQ extension methods? and https://github.com/AutoMapper/AutoMapper/issues/34