zoukankan      html  css  js  c++  java
  • 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);

            }

        }     


  • 相关阅读:
    ecshop后台根据条件查询后不填充table 返回的json数据,content为空?
    smarty中判断一个变量是否存在于一个数组中或是否存在于一个字符串中?
    getJSON回调函数不执行问题?
    高德地图关键字搜索删除上一次搜索的Marker
    多表连接查询详解
    网址图标设置
    CSS 引入方式 选择器
    Html 表单标签 Form
    Html 基本标签
    Python Socket实现简单web服务器
  • 原文地址:https://www.cnblogs.com/darrenji/p/3570492.html
Copyright © 2011-2022 走看看