Skip to content

Instantly share code, notes, and snippets.

@ognyandim
Last active June 22, 2021 07:03
Show Gist options
  • Save ognyandim/2ac30b5370b2be2b18b794bec0399e34 to your computer and use it in GitHub Desktop.
Save ognyandim/2ac30b5370b2be2b18b794bec0399e34 to your computer and use it in GitHub Desktop.
AutoMapper-two-to-one
using System;
using System.Text.Json;
using AutoMapper;
namespace automapper_two_entities
{
public static class Program
{
public static void Main()
{
Console.WriteLine("Hello World!");
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Book, BookRichInfo>();
cfg.CreateMap<Author, BookRichInfo>();
});
IMapper mapper = config.CreateMapper();
Book b = new Book { Title = "The Pilgrim's Progress", Rating = 10, Wikipage = "https://en.wikipedia.org/wiki/John_Bunyan" };
Author a = new Author { AuthorName = "John bunyan" };
var dto = mapper.Map<Book, BookRichInfo>(b);
mapper.Map<Author, BookRichInfo>(a, dto);
var dtoSer = JsonSerializer.Serialize(dto);
System.Console.WriteLine(dtoSer);
}
}
public class BookRichInfo
{
public string Title { get; set; }
public string AuthorName { get; set; }
public int Rating { get; set; }
}
public class Book
{
public string Title { get; set; }
public int Rating { get; set; }
public string Wikipage { get; internal set; }
}
public class Author
{
public string AuthorName { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment