EmitMapper的使用
1.普通的映射。
public class UserInfo
{
public int id { get; set; }
public string name { get; set; }
public string address { get; set; }
} public class UserInfoDTO
{
public string name { get; set; }
public string address { get; set; }
} var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserInfoDTO>();
UserInfoDTO userdto = mapper.Map(user);
2.有外键关联,需要映射出外键所带名字
public class UserInfo
{
public int id { get; set; }
public string name { get; set; }
public string address { get; set; } public Teacher teacher { get; set; }
}
public class Teacher
{
public int id { get; set; }
public string name { get; set; }
}
public class UserInfoDTO
{
public int id { get; set; }
public string name { get; set; }
public string teacher { get; set; }
} var user = new UserInfo {
id = 12,
name = "张三",
address = "北京",
teacher = new Teacher {
id = 11,
name = "王五"
}
}; var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserInfoDTO>(
new DefaultMapConfig()
.ConvertUsing<Teacher, string>(t => t.name)
);
UserInfoDTO userdto = mapper.Map(user);
3.两个实体之间名字不一致,需要映射。
public class UserInfo
{
public int id { get; set; }
public string name { get; set; }
public string address { get; set; }
}
public class UserInfoDTO
{
public int id { get; set; }
public string name { get; set; }
public string userAddress { get; set; }
}
var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserInfoDTO>(
new DefaultMapConfig()
.MatchMembers((x, y) =>
{
if (x == "address" && y == "userAddress")
{
return true;
}
return x == y;
})
);
UserInfoDTO userdto = mapper.Map(user);
4.需要对某一个字段进行特殊处理
public class UserInfo
{
public int id { get; set; }
public string name { get; set; }
public string address { get; set; }
}
public class UserInfoDTO
{
public string id { get; set; }
public string name { get; set; }
public string userAddress { get; set; }
public string userJson { get; set; }
}
var user = new UserInfo {
id = 12,
name = "张三",
address = "北京"
}; var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserInfoDTO>(
new DefaultMapConfig()
.PostProcess<UserInfoDTO>((value, state) =>
{
//在id编号前加上今年的年份
value.id = DateTime.Now.ToString("yyyy") + value.id;
//实体的json格式
value.userJson = "{\"id\":\"" + value.id + "\",\"name\":\"" + value.name + "\"}";
return value;
})
);
UserInfoDTO userdto = mapper.Map(user);
5.忽略掉某个字段的映射
public class UserInfo
{
public int id { get; set; }
public string name { get; set; }
public string address { get; set; }
}
public class UserInfoDTO
{
public string id { get; set; }
public string name { get; set; }
public string address { get; set; }
}
var user = new UserInfo {
id = 12,
name = "张三",
address = "北京"
}; var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserInfoDTO>(
new DefaultMapConfig()
.IgnoreMembers<UserInfo, UserInfoDTO>(new string[] { "name" })
);
UserInfoDTO userdto = mapper.Map(user);
6.给空元素赋默认值
public class UserInfo
{
public int id { get; set; }
public string name { get; set; }
public string address { get; set; }
public DateTime? godate { get; set; }
}
public class UserInfoDTO
{
public string id { get; set; }
public string name { get; set; }
public string address { get; set; }
public DateTime godate { get; set; }
}
var user = new UserInfo {
id = 12,
name = "张三",
address = null,
godate = null
}; var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserInfoDTO>(
new DefaultMapConfig()
//如果日期为空设置为当前时间
.NullSubstitution<DateTime?, DateTime>((value) => DateTime.Now)
//如果string类型为null赋值为“”
.NullSubstitution<string, string>((value) => "")
);
UserInfoDTO userdto = mapper.Map(user);
常用的就上面几点
private IMapper _mapper;
public IMapper Mapper
{
get
{
if (null == _mapper)
{
_mapper = AssemblerIoc.GetMapper();
}
return _mapper;
}
}
public class Mapper
{
public static IMapper _mapper;
public static IMapper GetMapper
{
get
{
if (_mapper == null)
{
_mapper = new MapperImpl();
}
return _mapper;
}
}
}
public interface IMapper
{
TDestination Map<TSource, TDestination>(TSource tSource); IEnumerable<TDestination> MapperGeneric<TSource, TDestination>(IEnumerable<TSource> tSources);
IList<TDestination> MapperGeneric<TSource, TDestination>(IList<TSource> tSources);
}
public class MapperImpl : IMapper
{
public TDestination Map<TSource, TDestination>(TSource tSource)
{
if (tSource == null)
return default(TDestination); var mapper = ObjectMapperManager.DefaultInstance.GetMapper<TSource, TDestination>();
return mapper.Map(tSource);
} public IEnumerable<TDestination> MapperGeneric<TSource, TDestination>(IEnumerable<TSource> tSources)
{
if (tSources == null)
return null; IList<TDestination> tDestinations = new List<TDestination>();
foreach (var tSource in tSources)
{
tDestinations.Add(Map<TSource, TDestination>(tSource));
}
return tDestinations;
} public IList<TDestination> MapperGeneric<TSource, TDestination>(IList<TSource> tSources)
{
if (tSources == null)
return null; IList<TDestination> tDestinations = new List<TDestination>();
foreach (var tSource in tSources)
{
tDestinations.Add(Map<TSource, TDestination>(tSource));
}
return tDestinations;
}
}
EmitMapper的使用的更多相关文章
- Asp.net 面向接口可扩展框架之使用“类型转化基础服务”测试四种Mapper(AutoMapper、EmitMapper、NLiteMapper及TinyMapper)
Asp.net 面向接口可扩展框架的“类型转化基础服务”是我认为除了“核心容器”之外最为重要的组成部分 但是前面博文一出,争议很多,为此我再写一篇类型转化基础服务和各种Mapper结合的例子,顺便对各 ...
- 开源实体映射框架EmitMapper介绍
开源实体映射框架EmitMapper介绍 综述 EmitMapper是一个开源实体映射框架,地址:http://emitmapper.codeplex.com/. Emit ...
- EmitMapper 和TinyMapper 两者简单对比
EmitMapper 和TinyMapper 两者的性能都是很高,相比autoMapper 速度不知道快了多少倍,因为使用的最多EmitMapper,所在业余时间做了一下测试对比. 测试数据:10万条 ...
- EmitMapper的使用小结
最近公司开发项目前端使用一个js框架,后端使用ef,js前台读取的json采用实体的dto来进行生成. 在网上看到了EmitMapper相对其他映射框架处理速度可以更快,就拿来用了.下面是代码中常用的 ...
- 关于EmitMapper,映射配置
public static T Snapshoot<T>(this XtraForm form, T obj) { var config = new DefaultMapConfig(); ...
- EmitMapper自动映射工具
在实体与DTO之间,我们一般都需要进行映射.如果手动的来进行转换,实在是太麻烦.所以就产生了很多映射工具,比如AutoMapper,EmitMapper.而经过一些对比,EmitMa ...
- 对象关系映射 EmitMapper 及Tuple的使用
public TDestination Map<TSource, TDestination>(TSource tSource) { if (tSource == null) return ...
- EmitMapper系列之二:EmitMapper的使用小结
EmitMapper的入门 EmitMapper引用 EmitMapper案例 最近公司开发项目前端使用一个js框架,后端使用ef,js前台读取的json采用实体的dto来进行生成. 在网上看到了Em ...
- EmitMapper系列之一:EmitMapper入门
EmitMapper的总结 EmitMapper简介 前言: 参考官网: http://emitmapper.codeplex.com/ Project Description Powerful cu ...
随机推荐
- 安装 gcc-c++ 时报错和原有 gcc 版本冲突
Centos 6.7 安装 gcc-c++时报下面的错误: Resolving Dependencies --> Running transaction check ---> :-.el6 ...
- windows下使用mysql双机热备功能
一. 准备工作 1. 准备两台服务器(电脑),接入局域网中,使互相ping得通对方 2. 两台服务器都安装mysql-server-5.1,必须保证mysql的版本一致 3. 假设,服务器A:192. ...
- C语言 时间函数的学习和总结
一直都是以简单的time_t t,time(&t),ctime(&t)来表示时间,后来要以时间为日志文件的名字时,就有点蒙逼了.学习一下. tm结构: struct tm { ...
- android中ADT和SDK的关系
ADT(Android Development Tools): 目前Android开发所用的开发工具是Eclipse,在Eclipse编译IDE环境中,安装ADT,为Android开发提供开发工具的升 ...
- glibc-2.15编译error: linker with -z relro support required
./configure --prefix=/usr/local/glibc-2.15 configure: error: you must configure in a separate build ...
- 关于 RTL8723BS 同时开启 STA/AP 模式
最近接到一个调试 wifi 驱动的任务,使用的是 rtl8723bs 芯片组.要求是让无线设备工作在 station 模式的时候同时开启一个 ap 热点.简单来讲就是连接其他 wifi 的同时发出一个 ...
- CSS3魔法堂:CSS3滤镜及Canvas、SVG和IE滤镜替代方案详解[转]
一.前言 IE特有的滤镜常常作为CSS3各种新特性的降级处理补充,而Adobe转向HTML5后与Chrome合作推出CSS3的Filter特性,因此当前仅Webkit内核的浏览器支持CSS3 F ...
- PCA原理与实践
在对数据进行预处理时,我们经常会遇到数据的维数非常之大,如果不进行相应的特征处理,那么算法的资源开销会很大,这在很多场景下是我们不能接受的.而对于数据的若干维度之间往往会存在较大的相关性,如果能将数据 ...
- 【原创Android游戏】--猜数字游戏V1.1 --数据存储,Intent,SimpleAdapter的学习与应用
--------------------------------------------------------------- V0.1版本 上次做完第一个版本后,发现还有一些漏洞,并且还有一些可以添 ...
- 一个优秀的Unity3d开发者必备的几种设计模式
Unity脚本编程 众所周知,unity的编程属于脚本化,脚本没有一个具体的概念跟架构, 导致在项目过程中,经常出现哪里需要实现什么功能,就随便添加脚本, 结果,就造成了一片混乱,不好管理. 更有甚者 ...