本篇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. grep和sed匹配多个字符关键字的用法

    GNU sed和UNIX sed 写法不一样 匹配多个关键词,打印出匹配的行,效果类似于 grep grep hello\|world file > output 或者用扩展正则 grep -E ...

  2. Redis常见业务场景应用

    一定时间范围内不可重复发短信问题 Redis实现消息队列 Redis实现Session共享 ...

  3. free命令中的buffer和cached的比较(转)

    原文链接:https://www.jianshu.com/p/cd2dd59d1566 最近在搞监控,突然看到我系统的内存要用完了,赶紧登录服务器看看, ~]# dstat -m     16G内存就 ...

  4. jexus配置支持Owin

    vi打开配置文件,加一行 OwinMain=xxx.dll ###################### # Web Site: Default ########################### ...

  5. Python_oldboy_自动化运维之路_线程,进程,协程(十一)

    本节内容: 线程 进程 协程 IO多路复用 自定义异步非阻塞的框架 线程和进程的介绍: 举个例子,拿甄嬛传举列线程和进程的关系: 总结:1.工作最小单元是线程,进程说白了就是提供资源的 2.一个应用程 ...

  6. JAVA随笔(一)

    数组变量是数组的管理者,而不是拥有者.数组变量的比较,是判断它们是否管理同一个数组.将一个数组变量赋值给 另一个数组变量并不产生新的数组.想产生新的数组只能通过new来完成. 同样,String类型的 ...

  7. PHP中curl模拟post上传及接收文件

    public function Action_Upload(){ $this->path_config(); exit(); $furl="@d:\develop\JMFramewor ...

  8. Luogu P1549 棋盘问题(2)

    题意 在N×N的棋盘上(1≤N≤10),填入1,2,-,N^2,共N^2个数,使得任意两个相邻的数之和为素数. 思路 先线性筛(非标准版),然后用a数组记录以i为下标的数是不是质数(就是标记数组),然 ...

  9. 安装配置tomcat,java运行环境

    1.下载JDK,安装 官网下载地址:http://java.sun.com/javase/downloads/index.jsp 下载后,安装,选择你想把JDK安装的目录: 比如:JDK安装目录:E: ...

  10. 入门NodeJS

    入门NodeJS https://www.cnblogs.com/dotnetcrazy/p/10118756.html NodeJS 1.环境配置 之前讲ES6的时候有提过一部分Node的知识,简单 ...