Skip to content

Instantly share code, notes, and snippets.

@lbargaoanu
Last active February 17, 2023 18:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save lbargaoanu/9c7233441c3a3413cc2b9b9ebb5964a9 to your computer and use it in GitHub Desktop.
Save lbargaoanu/9c7233441c3a3413cc2b9b9ebb5964a9 to your computer and use it in GitHub Desktop.
Sample AM
static void Main(string[] args)
{
var config = new MapperConfiguration(c =>
{
c.CreateMap<Model, Dto>();
c.CreateMap<Item, ItemDto>();
});
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
var source = new Model
{
Id = Guid.NewGuid(),
FooId = Guid.NewGuid(),
ShortDescription = "Yoyodyne Foo",
FullDescription = "Deluxe Foo Manufactured by Yoyodyne, Inc.",
Date = DateTime.Now,
IntValue = 13,
Items = new List<Item>{new Item { Value = 4}, new Item { Value = 3}},
};
source.Parent = source;
try
{
var dest = mapper.Map<Dto>(source);//.Dump();
}
catch(Exception ex)
{
ex.ToString();//.Dump();
}
}
public class Model
{
public Guid? Id { get; set; }
public Guid? FooId { get; set; }
public string FullDescription { get; set; }
public string ShortDescription { get; set; }
public DateTime Date { get; set; }
public int? IntValue { get; set; }
public List<Item> Items { get; set; }
public Model Parent { get; set; }
}
public class Dto
{
public Dto()
{
Items = new HashSet<ItemDto>();
}
public Guid? Id { get; set; }
public string FooId { get; set; }
public string FullDescription { get; set; }
public string ShortDescription { get; set; }
public DateTime Date { get; set; }
public int IntValue { get; set; }
public ICollection<ItemDto> Items { get; set; }
public Dto Parent { get; set; }
}
public class Item
{
public int Value { get; set; }
}
public class ItemDto
{
public int Value { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment