AutoMapper中用户自定义转换
Custom Type Converters
Sometimes, you need to take complete control over the conversion of one type to another. This is typically when one type looks nothing like the other, a conversion function already exists, and you would like to go from a "looser" type to a stronger type, such as a source type of string to a destination type of Int32.
For example, suppose we have a source type of:
public class Source
{
public string Value1 { get; set; }
public string Value2 { get; set; }
public string Value3 { get; set; }
}
But you would like to map it to:
public class Destination
{
public int Value1 { get; set; }
public DateTime Value2 { get; set; }
public Type Value3 { get; set; }
}
If we were to try and map these two types as-is, AutoMapper would throw an exception (at map time and configuration-checking time), as AutoMapper does not know about any mapping from string to int, DateTime or Type. To create maps for these types, we must supply a custom type converter, and we have three ways of doing so:
void ConvertUsing(Func<TSource, TDestination> mappingFunction);
void ConvertUsing(ITypeConverter<TSource, TDestination> converter);
void ConvertUsing<TTypeConverter>() where TTypeConverter : ITypeConverter<TSource, TDestination>;
The first option is simply any function that takes a source and returns a destination. This works for simple cases, but becomes unwieldy for larger ones. In more difficult cases, we can create a custom ITypeConverter<TSource, TDestination>:
public interface ITypeConverter<TSource, TDestination>
{
TDestination Convert(TSource source);
}
And supply AutoMapper with either an instance of a custom type converter, or simply the type, which AutoMapper will instantiate at run time. The mapping configuration for our above source/destination types then becomes:
[Test]
public void Example()
{
Mapper.CreateMap<string, int>().ConvertUsing(Convert.ToInt32);
Mapper.CreateMap<string, DateTime>().ConvertUsing(new DateTimeTypeConverter());
Mapper.CreateMap<string, Type>().ConvertUsing<TypeTypeConverter>();
Mapper.CreateMap<Source, Destination>();
Mapper.AssertConfigurationIsValid(); var source = new Source
{
Value1 = "",
Value2 = "01/01/2000",
Value3 = "AutoMapperSamples.GlobalTypeConverters.GlobalTypeConverters+Destination"
}; Destination result = Mapper.Map<Source, Destination>(source);
result.Value3.ShouldEqual(typeof (Destination));
} public class DateTimeTypeConverter : ITypeConverter<string, DateTime>
{
public DateTime Convert(string source)
{
return System.Convert.ToDateTime(source);
}
} public class TypeTypeConverter : ITypeConverter<string, Type>
{
public Type Convert(string source)
{
Type type = Assembly.GetExecutingAssembly().GetType(source);
return type;
}
}
AutoMapper中用户自定义转换的更多相关文章
- SQL Server中行列转换 Pivot UnPivot
SQL Server中行列转换 Pivot UnPivot PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PI ...
- awk中分隔符转换
awk中分隔符转换的问题(转) 在awk中明明用OFS重新设置了分隔符,为什么在输出的时候还是原样输出呢! 他是这么写的: echo 1,2,3,4 | awk 'BEGIN{FS=" ...
- kettle删除资源库中的转换或者作业
在资源库中新建转换,作业都很简单,那么加入现在不需要其中某个转换或者作业该怎么办呢? 下图是已经存在的转换跟作业 现在需要删除aa这个转换 操作步骤如下: 1.工具----资源库----探索资源库 出 ...
- Java中String转换Double类型 Java小数点后留两位
Java中String转换Double类型 double num1 = 0.0; String qq = "19.987"; num1 = Double.valueOf(qq.to ...
- VSTO中Word转换Range为Image的方法
VSTO中Word转换Range为Image的方法 前言 VSTO是一套用于创建自定义Office应用程序的Visual Studio工具包,通过Interop提供的增强Office对象,可以对Wor ...
- js中时间戳转换成时间格式
js中时间戳转换成时间格式, // 时间戳转换成时间格式 var formatDate = function(date){ date = new Date(date); var y=date.getF ...
- QT国际化(中英转换)
转载:https://blog.csdn.net/u012528526/article/details/54707233 QT国际化(中英转换) 我们都知道在安卓中,想做国际化很简单,只需要建立对应的 ...
- AS3中String转换成Boolean
AS3中, 对布尔值的转换, 规定所有的非空字符串都是true. 下面都不行: var f:Boolean = new Boolean(str); var f:Boolean = str as Boo ...
- SQL中DateTime转换成Varchar样式
SQL中DateTime转换成Varchar样式语句及查询结果:Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AMSelect ...
随机推荐
- Linux 配置Samba服务
查看系统下是否已经安装了sambarpm -qa |grep samba 安装sambayum -y install samba 配置samba创建目录sambamkdir -p /home/samb ...
- 自学Linux Shell3.2-切换目录命令cd
点击返回 自学Linux命令行与Shell脚本之路 3.2-切换目录命令cd 当登录系统并获得shell命令提示符后,你通常位于自己的主目录中. 使用pwd命令验证: pwd命令以绝对路径的方式显示用 ...
- Java -- JDBC 学习--PreparedStatement
可以通过调用 Connection 对象的 preparedStatement() 方法获取 PreparedStatement 对象.PreparedStatement 接口是 Statement ...
- mvc4+entityFramework5 发布时遇到的纠结问题
最近在研究微软的新平台Vs2012,做好的系统在发布到服务器时纠结了.本地环境是win7的,一切运行正常,发布也很顺利.可是悲催的服务器还是windows 2003的,.net framewrok4. ...
- C#.Net 持久化对象为XML文件
</pre><pre code_snippet_id="613717" snippet_file_name="blog_20150307_1_57950 ...
- 2636652995 揭秘骗子qq
3042952272636652995755610392020068008这是个骗子群526875508,群里都是群主的小号,付钱之后不给东西,还在群里维护骗子的利益,很明显了.都是骗子小号了,付完整 ...
- MySQL中的时态(日期/时间)数据类型
时态类型的取值范围 mysql> create table t (dt datetime,d date,t time); Query OK, 0 rows affected (0.30 sec) ...
- Python新手入门英文词汇笔记(转)
一.交互式环境与print输出 1.print:打印/输出2.coding:编码3.syntax:语法4.error:错误5.invalid:无效6.identifier:名称/标识符7.charac ...
- 多目标遗传算法 ------ NSGA-II (部分源码解析) 实数、二进制编码的变异操作 mutation.c
遗传算法的变异操作 /* Mutation routines */ # include <stdio.h> # include <stdlib.h> # include < ...
- JAVA记录-消息队列介绍
1.JMS概述 JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消 ...