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 ...
随机推荐
- day24 新三级菜单
china = { "shandong":{ "linyi":["lanshan","nanfang","he ...
- java web项目406错误的解决
返回的消息头浏览器不能解释 这里我们使用了@ResponseBody,返回数据后缀是,.json,但是我们的映射器后缀又是.html.最后浏览器收到数据不知该以哪种类型数据来进行解析,所以就会报406 ...
- MT【47】求一道分式的最值
评:技巧性很大,需要敏锐的洞察力通过柯西不等式把分母变成一样.请记住这个变形$$(a+b+ab+1)=(a+1)(b+1)\le\sqrt{(a^2+1)(b^2+1)}$$
- 【Gym 100733D】Little thief Shi(取数,DP)
题 Shi realized that he was almost out of money, even renting Shitalian lands. Shi was walking on a s ...
- Canvas与javaScript特效笔记
第六章 Canvas与javaScript特效笔记 q <canvas>标签的用途 HTML5 canvas 提供了通过 JavaScript 绘制图形的方法,此方法使用简单但功能强 ...
- 【BZOJ2229】[ZJOI2011]最小割(网络流,最小割树)
[BZOJ2229][ZJOI2011]最小割(网络流,最小割树) 题面 BZOJ 洛谷 题解 戳这里 那么实现过程就是任选两点跑最小割更新答案,然后把点集划分为和\(S\)联通以及与\(T\)联通. ...
- bzoj1002/luogu2144 轮状病毒 (dp)
给周围的点编号1到n 我们设f[i]为(1到i和中间点)连成一个联通块的情况数,那么有$f[i]=\sum{f[i-j]*j}$,就是从i-j+1到i里选一个连到中心,然后再把i-j+1到i连成链 但 ...
- Java: md5 加密中文 & 注意编码
今天使用java的md5加密一个字符串(含有部分中文),遇到问题,百思不得其解. 后面网上搜索了一下,才知道是[编码]的问题. 以前在asp中用md5加密的时候,了解 gb2312和utf-8编码下, ...
- 装饰页面decorators.xml
WEB-INF/decorators.xml 这个配置可以增加页面的 装饰页面
- AtomicInteger和count++的比较
J2SE 5.0提供了一组atomic class来帮助我们简化同步处理.基本工作原理是使用了同步synchronized的方法实现了对一个long, integer, 对象的增.减.赋值(更新)操作 ...