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. x01.Lab.OpenCV: 计算机视觉

    横看成岭侧成峰,计算视觉大不同.观看的角度不同,成像自然不同,这对计算机视觉来说,是个大麻烦.但计算机视觉应用如此广泛,却又有不得不研究的理由.指纹机大家都用过吧,这不过是冰山之一角.产品检测,机器人 ...

  2. PlaceHolder的两种实现方式

    placeholder属性是HTML5 中为input添加的.在input上提供一个占位符,文字形式展示输入字段预期值的提示信息(hint),该字段会在输入为空时显示. 如 <input typ ...

  3. Windows Azure Storage图形界面管理工具

    上一篇我们介绍了用PowerShell将Windows Azure的存储服务当网盘来使用.如果感觉还不够简单,那么这次我们来看看还有哪些使用起来更方便的图形界面管理工具吧.当然,这些工具必要支持中国版 ...

  4. KSFramework:集成U3D热重载框架 - README

    KSFramework KEngine + SLua+ Framework = KSFramework KSFramework是一个整合KEngine.SLua的Unity 5开发框架,并为程序.美术 ...

  5. 经典DOS游戏皇帝攻略(曾经的回忆)

    最完美攻略>>>>> -------------------------------------------------------------------------- ...

  6. HQL基础查询语句

    HQL基础查询语句 1.使用hql语句检索出Student表中的所有列 //核心代码 @Test public void oneTest() { Query query=session.createQ ...

  7. zoj 1610

    Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on ...

  8. 一个简单的Android富文本TextView实现

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Helvetica; color: #555555 } p.p2 { margin: 0.0p ...

  9. linux下安装安装pcre-8.32 configure: error: You need a C++ compiler for C++ support

    linux下安装安装pcre-8.32./configure --prefix=/usr/local/pcre 出现以下错误configure: error: You need a C++ compi ...

  10. 两个经典的Oracle触发器示例(轉)

    [案例一] 题目:--触发器:--添加员工信息,流水号作为自动编号(通过序列生成),--并且判断如果工资小于0,则改为0;如果大于10000,则改为10000. CREATE TABLE emp2(e ...