本篇AutoMapper使用場景:
※ 當源和目標具有同名的複雜型別屬性、集合型別屬性,這2種屬性對應的類間也需建立對映
※ 一次性定義好源和目標的所有對映
※ 一次性定義好源和目標的所有對映,目標中有複雜型別屬性
※ 一次性定義好源和目標的所有對映,目標中有複雜型別屬性,為複雜型別建立自定義解析器
□ Domain model
public class BookStore
{
public string Name { get; set; }
public Address Address { get; set; }
public List<Book> Books { get; set; }
}
public class Address
{
public string Country { get; set; }
public string City { get; set; }
public string Street { get; set; }
public string PostCode { get; set; }
}
public class Book
{
public string Title { get; set; }
public string Description { get; set; }
public string Language { get; set; }
public decimal Price { get; set; }
public DateTime? PublishDate { get; set; }
public Publisher Publisher { get; set; }
public int? Paperback { get; set; }
public List<Author> Authors { get; set; }
}
public class Publisher
{
public string Name { get; set; }
}
public class Author
{
public string Name { get; set; }
public string Description { get; set; }
public ContanctInfo ContactIfno { get; set; }
}
public class ContanctInfo
{
public string Email { get; set; }
public string Blog { get; set; }
public string Twitter { get; set; }
}
□ View model
public class BookStoreDto
{
public string Name { get; set; }
public AddressDto Address { get; set; }
public List<BookDto> Books { get; set; }
}
public class AddressDto
{
public string Country { get; set; }
public string City { get; set; }
public string Street { get; set; }
public string PostCode { get; set; }
}
public class BookDto
{
public string Title { get; set; }
public string Description { get; set; }
public string Language { get; set; }
public decimal Price { get; set; }
public DateTime? PublishDate { get; set; }
public string Publisher { get; set; }
public int? Paperback { get; set; }
public string FirstAuthorName { get; set; }
public string FirstAuthorDescription { get; set; }
public string FirstAuthorEmail { get; set; }
public string FirstAuthorBlog { get; set; }
public string FirstAuthorTwitter { get; set; }
public string SecondAuthorName { get; set; }
public string SecondAuthorDescription { get; set; }
public string SecondAuthorEmail { get; set; }
public string SecondAuthroBlog { get; set; }
public string SecondAuthorTwitter { get; set; }
}
當源和目標具有同名的複雜型別屬性、集合型別屬性,這2種屬性對應的類間也需建立對映
Mapper.CreateMap<BookStoreDto, BookStore>();
Mapper.CreateMap<AddressDto, Address>();
Mapper.CreateMap<BookDto, Book>();
BookStore bookStore = Mapper.Map<BookStoreDto, BookStore>(bookStoreDto);
一次性定義好源和目標的所有對映
Mapper.CreateMap<BookDto, ContanctInfo>()
.ConstructUsing(s => new ContanctInfo //第一個引數為源
{
Blog = s.FirstAuthorBlog,
Email = s.FirstAuthorEmail,
Twitter = s.FirstAuthorTwitter
});
ContanctInfo contactInfo = Mapper.Map<BookDto, ContanctInfo>(bookDto);
一次性定義好源和目標的所有對映,目標中有複雜型別屬性
Mapper.CreateMap<BookDto, Author>()
.ConstructUsing(s => new Author
{
Name = s.FirstAuthorName,
Description = s.FirstAuthorDescription,
ContactIfno = new ContanctInfo {
Blog = s.FirstAuthorBlog,
Email = s.FirstAuthorEmail,
Twitter = s.FirstAuthorTwitter
}
});
Author author = Mapper.Map<BookDto, Author>(bookDto); //間接得到了ContactInfo
一次性定義好源和目標的所有對映,目標中有複雜型別屬性,為複雜型別建立自定義解析器
Mapper.CreateMap<BookDto, Author>()
.ForMember(d => d.Name, opt => opt.MapFrom(s => s.FirstAuthorName))
.ForMember(d => d.Description, opt => opt.MapFrom(s => s.FirstAuthorDescription))
.ForMember(d => d.ContactIfno, opt => opt.ResolveUsing<FirstAuthorContactInfoResolver>());
Author author = Mapper.Map<BookDto, Author>(bookDto); //間接得到了ContactInfo
□ 自定義解析器
public class FirstAuthorContactInfoResolver : ValueResolver<BookDto, ContanctInfo>
{
protected override ContanctInfo ResolveCore(BookDto source)
{
return Mapper.Map<BookDto, ContanctInfo>(source);
}
}