public void Start() { #region mapper //DMapper.CreateMap<PublisherDto,Publisher>(); //DMapper.CreateMap<BookDto, Book>(); //DMapper.CreateMap<BookDto, Book>((map) => //{ // map.ForMember(t => t.Title, f => f.Description); //}); //DMapper.CreateMap<BookDto, Book>((map) => //{ // map.ForMember(t => t.Description, f => f.PublishDate.ToString()); //}); //DMapper.CreateMap<BookDto, Book>((map) => //{ // map.ForMember(t => t.Description, f => f.Publisher.Name); //}); //DMapper.CreateMap<BookDto, Book>((map) => //{ // map.ForMember(t => t.Price, f => f.Price); // map.ForMember(t => t.FirstAuthorBlog, f => f.Authors[0].ContactInfo.Blog); // map.ForMember(t => t.Publisher, f => f.Publisher.Name); //}); #endregion #region data Book book = new Book() { Price = 100, Description = "书本描述", PublishDate = DateTime.Now, Language = "Zh-cn", Paperback = 520, Title = "大话数据结构", Publisher = new Publisher() { Name = "发布者", ID = "110" }, Authors = new List<Author>() { new Author() { Name="作者名字-1", Description="程序员一个-1", ContactInfo=new ContactInfo () { Blog="http://www.hhahh2011.com-1", Email="[email protected]" } }, new Author() { Name="作者名字-2", Description="程序员一个-2", ContactInfo=new ContactInfo () { Blog="http://www.hhahh2011.com-2", Email="[email protected]" } } } }; #endregion var mapper = DMapper.CreateMap<BookDto, Book>(map => { map.ForMember(p => p.Description, p => p.Language); }); var dto = mapper.Map(book); CodeTimer.Init(); CodeTimer.Time("DMapper", 10000000, () => { BookDto to = mapper.Map(book); }); CodeTimer.Time("Manual", 10000000, () => { BookDto to = new BookDto(); to.Title = book.Title; to.Publisher = new PublisherDto() { Name = book.Publisher.Name }; to.Description = book.Description; to.Language = book.Language; to.Paperback = book.Paperback; to.Price = book.Price; to.PublishDate = book.PublishDate; }); Console.ReadLine(); }
public BookDto BookToBookDtoMapper(Book from) { BookDto to = new BookDto(); to.Description = from.PublishDate.ToString(); to.Title = from.Price.ToString(); //to.Publisher = new PublisherDto() { Name = from.Publisher.Name, ID = from.Publisher.ID }; return to; }