Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dmdymov/dda231c096248b31134c50e3cbbc72b6 to your computer and use it in GitHub Desktop.
Save dmdymov/dda231c096248b31134c50e3cbbc72b6 to your computer and use it in GitHub Desktop.
using AutoMapper;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
public class Program
{
static void Main(string[] args)
{
Mapper.Initialize(x =>
{
x.AddProfile<ThingProfile>();
});
Mapper.AssertConfigurationIsValid();
var dto1 = new ThingDto();
dto1.Items = "1,2";
var dto2 = new ThingDto();
var thing1 = Mapper.Map<Thing>(dto1);
var thing2 = Mapper.Map<Thing>(dto2); //Exception here, null reference in resolver.
}
}
public class Thing
{
public List<string> Items
{
get;
set;
}
}
public class ThingDto
{
public string Items
{
get;
set;
}
}
public class Resolver : IMemberValueResolver<object, object, string, List<string>>
{
public List<string> Resolve(object source,
object destination,
string sourceMember,
List<string> destMember,
ResolutionContext context)
{
return sourceMember.Split(',').ToList();
}
}
public class ThingProfile : Profile
{
public ThingProfile()
{
CreateMap<ThingDto, Thing>()
.ForMember(dest => dest.Items, conf =>
{
conf.Condition(src => !string.IsNullOrEmpty(src.Items));
conf.ResolveUsing<Resolver, string>(source => source.Items);
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment