本篇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-一次性定义映射、复杂类型属性映射的更多相关文章

  1. AutoMapper在MVC中的运用01-配置、使用、单元测试、举例

    MVC中,如果想在Domain Model和View Model之间建立映射,用AutoMapper是一个不错的选择.不仅如此,AutoMapper能在不同对象之间建立映射,比如string与int类 ...

  2. AutoMapper在MVC中的运用小结

    配置.单元测试.AOP注入 Decimal转换成String类型 源数组转换成目标数组 源中的集合(数组)属性转换成目标中的集合(数组)属性 子类父类间的映射 源字典集合转换成目标字典集合 枚举映射 ...

  3. AutoMapper在MVC中的运用04-string映射各种类型、一个属性映射多个属性等

    本篇AutoMapper使用场景: ※ 类型转换,源string类型分别转换成int, DateTime,Type ※ 源和目标都包含复杂类型属性 ※ 把源中的一个属性映射到目标中的多个属性 类型转换 ...

  4. AutoMapper在MVC中的运用03-字典集合、枚举映射,自定义解析器

    本篇AutoMapper使用场景: ※ 源字典集合转换成目标字典集合 ※ 枚举映射 ※ 自定义解析器 ※ 源中的复杂属性和Get...方法转换成目标属性 源字典集合转换成目标字典集合 □ Domain ...

  5. AutoMapper在MVC中的运用02-Decimal转String、集合、子父类映射

    本篇AutoMapper使用场景: ※ Decimal转换成String类型 ※ 源数组转换成目标数组 ※ 源中的集合(数组)属性转换成目标中的集合(数组)属性 ※ 子类父类间的映射 Decimal转 ...

  6. AutoMapper在MVC中的运用07-映射在订单场景的例子

    本文参考了Taswar Bhatti的博客,他写了<Instant AutoMapper>这本书.遗憾的是,这本电子版书在国内还买不到,也下载不到.也只能从他的有限几篇博文中来窥探一二了. ...

  7. AutoMapper在MVC中的运用05-映射中的忽略、处理null、多种映射转换

    本篇AutoMapper使用场景: ※ 动态实现接口方法或属性 ※ 目标中的属性如果比源多,可以忽略多出的属性 ※ 目标有virtual属性,可忽略 ※ 目标属性值为null的解决办法 ※ int转s ...

  8. Asp.net Mvc中利用ValidationAttribute实现xss过滤

    在网站开发中,需要注意的一个问题就是防范XSS攻击,Asp.net mvc中已经自动为我们提供了这个功能.用户提交数据时时,在生成Action参数的过程中asp.net会对用户提交的数据进行验证,一旦 ...

  9. 在Asp.Net MVC中使用ModelBinding构造Array、List、Collection以及Dictionary

    在asp.net mvc中,我们可以在html表单中使用特定的格式传递参数,从而通过model binder构造一些集合类型. 第一种方式 public ActionResult Infancy(Pe ...

随机推荐

  1. nagios使用问题的解决方案

    通过web界面修改某个服务时报错例如对某个服务进行临时安排其执行时间,或者不让它发警告,web页面上都有这样的设置.但是常常会有错误信息如下: Could not open command file ...

  2. readb(), readw(), readl(),writeb(), writew(), writel() 宏函数【转】

    转自:http://www.netfoucs.com/article/hustyangju/70429.html readb(), readw(), readl()函数功能:从内存映射的 I/O 空间 ...

  3. TAU调研咨询

    厦门宇能科技有限公司 GPRS-RTU/DTU.3/4G路由器.无线远程抄表.管网监控 咨询电话:0592-5710250 2017-07-04 9:36:16 您好,欢迎光临.请问有什么可以帮到您? ...

  4. MySQL多源复制【转】

    什么是多源复制? 首先,我们需要清楚 multi-master 与multi-source 复制不是一样的. Multi-Master 复制通常是环形复制, 你可以在任意主机上将数据复制给其他主机. ...

  5. Codeforces 219C Color Stripe(思维+字符串)

    题目链接:http://codeforces.com/problemset/problem/219/C 题目大意: 给出一个字符串,只包含k种字符,问最少修改多少个字符(不增长新的种类)能够得到一个新 ...

  6. InnoDB Lock浅谈

    数据库使用锁是为了支持更好的并发,提供数据的完整性和一致性.InnoDB是一个支持行锁的存储引擎,锁的类型有:共享锁(S).排他锁(X).意向共享(IS).意向排他(IX).为了提供更好的并发,Inn ...

  7. 方法调用---springMVC中调用controller的方法

    我们有一个路由StudentController,里面有一个方法count().如果要在另外一个GradeController中调用count()方法有2种方式: 因为StudentControlle ...

  8. CSS------当内容超出div宽度后自动换行和限制文字不超出div宽度和高度

    如图: 1.自动换行 </div> 2.限制宽高度 </div> (注意:如果div放在li中还需要加上display:inline-block属性)

  9. ubuntu ifconfig只有lo没有ens33的问题

    如果ifconfig只显示了lo, ifconfig -a 却正常显示ens33.那么可以按照如下的操作: service network-manager stop rm /var/lib/Netwo ...

  10. spring中注册bean(通过代码动态注册)

    看公司的源代码,在一个类中使用到了BeanDefinitionBuilder这个类,在学习之后才知道在项目中可能没有注册bean,在使用的时候才会进行注册,就涉及到了动态bean的注册,所以,在文章中 ...