Last active
October 18, 2016 12:08
-
-
Save dmdymov/11aab6658c6dbe2d5803ca5d6cefd24c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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