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的使用的更多相关文章

  1. Asp.net 面向接口可扩展框架之使用“类型转化基础服务”测试四种Mapper(AutoMapper、EmitMapper、NLiteMapper及TinyMapper)

    Asp.net 面向接口可扩展框架的“类型转化基础服务”是我认为除了“核心容器”之外最为重要的组成部分 但是前面博文一出,争议很多,为此我再写一篇类型转化基础服务和各种Mapper结合的例子,顺便对各 ...

  2. 开源实体映射框架EmitMapper介绍

    开源实体映射框架EmitMapper介绍   综述       EmitMapper是一个开源实体映射框架,地址:http://emitmapper.codeplex.com/.       Emit ...

  3. EmitMapper 和TinyMapper 两者简单对比

    EmitMapper 和TinyMapper 两者的性能都是很高,相比autoMapper 速度不知道快了多少倍,因为使用的最多EmitMapper,所在业余时间做了一下测试对比. 测试数据:10万条 ...

  4. EmitMapper的使用小结

    最近公司开发项目前端使用一个js框架,后端使用ef,js前台读取的json采用实体的dto来进行生成. 在网上看到了EmitMapper相对其他映射框架处理速度可以更快,就拿来用了.下面是代码中常用的 ...

  5. 关于EmitMapper,映射配置

    public static T Snapshoot<T>(this XtraForm form, T obj) { var config = new DefaultMapConfig(); ...

  6. EmitMapper自动映射工具

             在实体与DTO之间,我们一般都需要进行映射.如果手动的来进行转换,实在是太麻烦.所以就产生了很多映射工具,比如AutoMapper,EmitMapper.而经过一些对比,EmitMa ...

  7. 对象关系映射 EmitMapper 及Tuple的使用

    public TDestination Map<TSource, TDestination>(TSource tSource) { if (tSource == null) return ...

  8. EmitMapper系列之二:EmitMapper的使用小结

    EmitMapper的入门 EmitMapper引用 EmitMapper案例 最近公司开发项目前端使用一个js框架,后端使用ef,js前台读取的json采用实体的dto来进行生成. 在网上看到了Em ...

  9. EmitMapper系列之一:EmitMapper入门

    EmitMapper的总结 EmitMapper简介 前言: 参考官网: http://emitmapper.codeplex.com/ Project Description Powerful cu ...

随机推荐

  1. c# 请求api获得json数据

    public static string HttpGet(string Url) { HttpWebRequest request = (HttpWebRequest)WebRequest.Creat ...

  2. [转]ion-slide-box

    本文转自:http://ionicframework.com/docs/api/directive/ionSlideBox/ The Slide Box is a multi-page contain ...

  3. 【温故而知新-Javascript】使用 Ajax

    Ajax 是现代Web 应用程序开发的一项关键工具.它让你能向服务器异步发送和接收数据,然后用 Javascript 解析. Ajax 是 Asynchronous JavaScript and XM ...

  4. POJ1384Piggy-Bank[完全背包]

    Piggy-Bank Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10787   Accepted: 5258 Descr ...

  5. POJ3187Backward Digit Sums[杨辉三角]

    Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6350   Accepted: 36 ...

  6. openjudge6047分蛋糕[DP]

    描述 有一块矩形大蛋糕,长和宽分别是整数w .h.现要将其切成m块小蛋糕,每个小蛋糕都必须是矩形.且长和宽均为整数.切蛋糕时,每次切一块蛋糕,将其分成两个矩形蛋糕.请计算:最后得到的m块小蛋糕中,最大 ...

  7. NOIP2002矩形覆盖[几何DFS]

    题目描述 在平面上有 n 个点(n <= 50),每个点用一对整数坐标表示.例如:当 n=4 时,4个点的坐标分另为:p1(1,1),p2(2,2),p3(3,6),P4(0,7),见图一. 这 ...

  8. 调用newtonsoft.json反序列出错

    调用newtonsoft.json反序列出错: Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current J ...

  9. java中使用二重循环打印图形

    如图所示:打印沙漏图形 1:因为外层循环控制图形行数,所以首先判断这四个选项能否循环五次 2:以上四个循环的表达式都能循环五次,我们从内层循环入手. A:int i=0;i<5;i++ 当i=1 ...

  10. Oracle中把一个DateTime的字符串转化成date类型。to_date('2016/12/8 18:55:43','yyyy/MM/dd hh24:mi:ss'),

    Oracle中把一个DateTime或者该形态字符串转化成date类型. to_date('2016/12/8 18:55:43','yyyy/MM/dd hh24:mi:ss'), 或者: sele ...