AutoMapper在MVC中的运用06

本篇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);

        }

    }     


AutoMapper在MVC中的运用06,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。