Skip to content

Instantly share code, notes, and snippets.

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 dmdymov/11aab6658c6dbe2d5803ca5d6cefd24c to your computer and use it in GitHub Desktop.
Save dmdymov/11aab6658c6dbe2d5803ca5d6cefd24c to your computer and use it in GitHub Desktop.
using AutoMapper;
using System.Diagnostics;
 
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Mapper.Initialize(x =>
{
x.AddProfile<ThingProfile>();
x.AddProfile<ConcreteThingProfile>();
});
 
var dto1 = new ConcreteThingDto() { Title = "Title" };
var item = Mapper.Map<ConcreteThing>(dto1);
var dto2 = Mapper.Map<ConcreteThingDto>(item);
 
Debug.Assert(dto2.Title == "Title");
}
}
 
public class ThingProfile : Profile
{
public ThingProfile()
{
CreateMap<ThingDto, Thing>()
.ForMember(dest => dest.Name, conf => conf.MapFrom(source => source.Title))
.ReverseMap()
.ForMember(dest => dest.Title, conf => conf.MapFrom(source => source.Name));
}
}
 
public class ConcreteThingProfile : Profile
{
public ConcreteThingProfile()
{
CreateMap<ConcreteThingDto, ConcreteThing>()
.IncludeBase<ThingDto, Thing>()
.ReverseMap()
.IncludeBase<Thing, ThingDto>();
}
}
 
public class Thing
{
public string Name { get; set; }
}
 
public class ConcreteThing : Thing { }
 
public class ThingDto
{
public string Title { get; set; }
}
 
public class ConcreteThingDto : ThingDto { }
}
 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment