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 ...
随机推荐
- Swift完整项目打包Framework,嵌入OC项目使用
场景说明: -之前做的App,使用Swift框架语言,混合编程,内涵少部分OC代码. -需要App整体功能打包成静态库,完整移植到另一个App使用,该App使用OC. -所以涉及到一个语言互转的处理, ...
- redis 下载启动,设置、查询超时时间
1.定义 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted ...
- java报表工具FineReport使用中遇到的常见报错及解决办法(三)
这里写点抛砖引玉,希望大家能把自己整理的问题及解决方法晾出来,Mark一下,利人利己. 出现问题先搜一下文档上有没有,再看看度娘有没有,再看看论坛有没有.有报错要看日志.下面简单罗列下常见的问题,大多 ...
- Loadrunner将字符串存为参数
直接代码: Action() { //定义一个字符串 char *URL= "http://linux.cn"; /******************************** ...
- ural Cipher Message
Cipher Message Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Desc ...
- 奇葩啊,HOLOLENS里模拟器截不了图,后台DEVIE PORTAL可以截
奇葩啊,HOLOLENS里模拟器截不了图,后台DEVIE PORTAL可以截 截屏 https://developer.microsoft.com/en-us/windows/holographic/ ...
- JavaScript打开窗口与关闭页面操作大全
JavaScript新开窗口 onClick="javascript:window.location='http://www.sowsoy.com'" JavaScript新开一个 ...
- HTML 学习笔记 CSS3 (边框)
CSS3边框 通过CSS3边框 你能够创建远角边框 向矩形边框添加阴影 使用图片来绘制边框 . CSS3的边框属性 主要包含以下几种 border-radius 边框圆角 box-shadow 边框阴 ...
- 如何用javac 和java 编译运行整个Java工程 (转载)【转】在Linux下编译与执行Java程序
如何用javac 和java 编译运行整个Java工程 (转载) http://blog.csdn.net/huagong_adu/article/details/6929817 [转]在Linux ...
- JavaScript继承与原型链
对于那些熟悉基于类的面向对象语言(Java 或者 C++)的开发者来说,JavaScript 的语法是比较怪异的,这是由于 JavaScript 是一门动态语言,而且它没有类的概念( ES6 新增了c ...