本篇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. React-Native 之 Navigator与NavigatorIOS使用

    前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...

  2. ASP.NET应用技巧:非托管COM组件的使用

    众所周知,asp.net是基于通用语言运行库创建的,也就是所谓的托管执行环境.生成的代码称为托管代码.编译器能够从源代码的描述中产生元数据信息,而运行库又从元数据中获得托管代码的信息.而我们编写的组件 ...

  3. Error updating database. Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String异常处理

    问题原因:Mybatis中对于时间参数进行比较时的一个BUG. 如果拿传入的时间类型参数与空字符串‘‘进行对比判断则会引发异常.,所以应该去掉该判断, 只保留非空判断就正常了 <if test= ...

  4. C# Message类的属性Msg所关联的消息ID

    C# Message类的属性Msg所关联的消息ID   https://msdn.microsoft.com/en-us/library/windows/desktop/ms645606(v=vs.8 ...

  5. pyqt5-基础

    PyQt5是一套来自Digia的Qt5应用框架和Python的粘合剂.支持Python2.x和Python3.x版本. PyQt5以一套Python模块的形式来实现功能.它包含了超过620个类,600 ...

  6. 《python源码剖析》,看看

    这书高级了,有点超出理解能力. 但走出舒适区,不是大家都在说的么?:) 看完了些章节,还是很有收获的, 截图存照.

  7. CSS 浮动和清除

    CSS 浮动和清除浮动 在写页面布局的过程中,浮动是大家经常用的属性.在好多的排版布局中都是用的的浮动比如说下面这些地方都是应用到了浮动. 在我学习浮动的时候可是熬坏了脑筋,在这里我分享一下我对浮动这 ...

  8. linux上jenkins连接windows并执行exe文件

    1.如果要通过ssh的方式来连接windows的话,首先需要在windows上安装freesshd来配置启动.配置ssh(win10上自带了openssh可以进行安装使用,但我机器装不上) 1.1.下 ...

  9. PHP获取访问者公网IP

    if(!empty($_SERVER["HTTP_CLIENT_IP"])){  $cip = $_SERVER["HTTP_CLIENT_IP"]; } el ...

  10. sorted()排序详解

    sorted()排序详解     http://wiki.python.org/moin/HowTo/Sorting?highlight=%28howto%29#The_Old_Way_Using_t ...