Skip to content

Instantly share code, notes, and snippets.

@lbargaoanu
Created May 24, 2016 11:28
Show Gist options
  • Save lbargaoanu/6c1975904cdfee891f22d193f9c01155 to your computer and use it in GitHub Desktop.
Save lbargaoanu/6c1975904cdfee891f22d193f9c01155 to your computer and use it in GitHub Desktop.
public static void Main()
{
var config = new MapperConfiguration(c =>
{
c.CreateMap<Model, Dto>().ForMember(d=>d.Country, o=>o.ResolveUsing<ResolveEntityFromId<Country>, long?>(s => 0));
});
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
var source = new Model { FooId = Guid.NewGuid(), ShortDescription = "Yoyodyne Foo", FullDescription = "Deluxe Foo Manufactured by Yoyodyne, Inc.", Date = DateTime.Now.ToUniversalTime()};
mapper.Map<Dto>(source);
}
public class Model
{
public Guid FooId { get; set; }
public string FullDescription { get; set; }
public string ShortDescription { get; set; }
public DateTime Date { get; set; }
}
public class Dto
{
public Guid FooId { get; set; }
public string FullDescription { get; set; }
public string ShortDescription { get; set; }
public DateTime Date { get; set; }
public Country Country { get; set; }
}
public class Country
{
private int _value;
public int Value
{
get
{
Console.WriteLine("Get value");
return _value;
}
set
{
_value = value;
}
}
}
public class ResolveEntityFromId<T> : IValueResolver<long?, T> where T : new()
{
public T Resolve(long? id, ResolutionContext context)
{
return new T();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment