Last active
November 23, 2018 21:26
-
-
Save StillLearnin/754108825f82647e8f2e85bb073e34bf to your computer and use it in GitHub Desktop.
This file contains 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; | |
using System.Collections.Generic; | |
namespace AutoMapper_Repro | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Hello World!"); | |
Mapper.Initialize(cfg => | |
{ | |
cfg.CreateMap<Person, PersonDTO>() | |
.ReverseMap() | |
.ForMember(props => props.Siblings, opts => opts.Ignore()); | |
cfg.CreateMap<Family, FamilyDTO>() | |
.ReverseMap(); | |
}); | |
var familyDTO = new FamilyDTO(); | |
var family = new Family | |
{ | |
Father = new Person { Name = "Dad" }, | |
Mother = new Person { Name = "Mom" }, | |
Children = new List<Person> | |
{ | |
new Person { Name = "Bill", | |
Siblings = new List<Person> { | |
new Person { Name = "Jimmy" }, | |
new Person { Name = "Sue" } | |
} | |
} | |
} | |
}; | |
Mapper.Map(family, familyDTO); | |
familyDTO.Children[0].Name = "William"; | |
Mapper.Map(familyDTO, family); | |
throw new Exception("William (Bill) no longer has siblings!"); | |
} | |
} | |
class PersonDTO | |
{ | |
public string Name { get; set; } | |
} | |
class Person | |
{ | |
public string Name { get; set; } | |
public List<Person> Siblings { get; set; } | |
} | |
class FamilyDTO | |
{ | |
public PersonDTO Father { get; set; } | |
public PersonDTO Mother { get; set; } | |
public List<PersonDTO> Children { get; set; } | |
} | |
class Family | |
{ | |
public Person Father { get; set; } | |
public Person Mother { get; set; } | |
public List<Person> Children { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment