TinyMapper - a tiny and quick object mapper for .Net.

The main advantage is performance. TinyMapper allows easily map object to object, i.e. properties or fields from one object to another, for instance.

  • How to install TinyMapper

To install TinyMapper, run the following command in the Package Manager Console

Install-Package TinyMapper

  • How To Use TinyMapper

Code Example1

Person.cs

    public class Person
{
public string Address { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public Guid Id { get; set; }
public string LastName { get; set; }
}

PersonDto.cs

    public class PersonDto
{
public string Email { get; set; }
public string FirstName { get; set; }
public Guid Id { get; set; }
public string LastName { get; set; }
}

Main

        static void Main(string[] args)
{
//First of all Bind Person type to PersonDto type and we don't want map Email property. So it has been ignored.
TinyMapper.Bind<Person, PersonDto>(config => {
config.Ignore(x => x.Email);
}); var person = new Person {
Id = Guid.NewGuid(),
FirstName = "Luke",
LastName = "Chen",
Email = "xiaoyong6906@126.com"
}; //Now TinyMapper knows how to map Person object to PersonDto object. Finally, call
PersonDto personDto = TinyMapper.Map<PersonDto>(person);
Console.WriteLine("personDto:" + personDto.Id + personDto.FirstName + personDto.LastName + personDto.Email);
Console.ReadLine();
}

Code Example2

PersonComplex.cs

    public class PersonComplex
{
public Address Address { get; set; }
public DateTime CreateTime { get; set; }
public List<string> Emails { get; set; }
public string FirstName { get; set; }
public Guid Id { get; set; }
public string LastName { get; set; }
public string Nickname { get; set; }
}

PersonDtoComplex.cs

public class PersonDtoComplex
{
public Address Address { get; set; }
public DateTime CreateTime { get; set; }
public List<string> Emails { get; set; }
public string FirstName { get; set; }
public Guid Id { get; set; }
public string LastName { get; set; }
public string Nickname { get; set; }
}

Main

static void Main(string[] args)
{ TinyMapper.Bind<PersonComplex, PersonDtoComplex>(config => {
config.Ignore(x => x.CreateTime);
config.Ignore(x => x.Nickname);
config.Bind(x => x.FirstName, y => y.FirstName);//order property name
}); var person = new PersonComplex
{
Id = Guid.NewGuid(),
FirstName = "Luke",
LastName = "Chen",
Address = new Address
{
Phone = "XXXX",
Street = "IT Street",
ZipCode = ""
},
CreateTime = DateTime.Now,
Nickname = "Yong",
Emails = new List<string> {
"xiaoyong6906@126.com",
"xiaoyong6907@126.com"
}
}; var personDto = TinyMapper.Map<PersonDtoComplex>(person);
Console.WriteLine(personDto.Nickname == null);
Console.WriteLine(personDto.FirstName);
Console.ReadLine();
}

Code Example3

TargetClass.cs

    public class TargetClass
{
public string FullName { get; set; }
}

SourceClass.cs

    public class SourceClass
{
public string FirstName { get; set; }
public string LastName { get; set; }
}

SourceClassConverter.cs

    public class SourceClassonverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(TargetClass);
} public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
var concreteValue = (SourceClass)value;
var result = new TargetClass
{
FullName = string.Format("{0} {1}", concreteValue.FirstName, concreteValue.LastName)
};
return result;
}
}

Main

        static void Main(string[] args)
{
TinyMapper.Bind<SourceClass, TargetClass>();
var source = new SourceClass {
FirstName = "Luke",
LastName = "Chen"
}; var result = TinyMapper.Map<TargetClass>(source);
Console.WriteLine(result.FullName);
Console.ReadLine();
}

[C#] 记-TinyMapper使用的更多相关文章

  1. Spark踩坑记——Spark Streaming+Kafka

    [TOC] 前言 在WeTest舆情项目中,需要对每天千万级的游戏评论信息进行词频统计,在生产者一端,我们将数据按照每天的拉取时间存入了Kafka当中,而在消费者一端,我们利用了spark strea ...

  2. Spark踩坑记——数据库(Hbase+Mysql)

    [TOC] 前言 在使用Spark Streaming的过程中对于计算产生结果的进行持久化时,我们往往需要操作数据库,去统计或者改变一些值.最近一个实时消费者处理任务,在使用spark streami ...

  3. 这些年一直记不住的 Java I/O

    参考资料 该文中的内容来源于 Oracle 的官方文档.Oracle 在 Java 方面的文档是非常完善的.对 Java 8 感兴趣的朋友,可以从这个总入口 Java SE 8 Documentati ...

  4. 千回百折:百度Java研发offer斩获记和经验分享

    起因 面试过程 等待offer的过程中悟道 Java面试常考知识点个人总结 过程 百度——作为国内互联网的巨头之一,最近的一些风波对其褒贬不一,但是类似事件不是第一次发生,也绝对不是最后一次,对于真的 ...

  5. 记一次nginx部署yii2项目时502 bad gateway错误的排查

    周六闲来无事,就试着安装和部署下yii2,安装过程没什么问题,但部署到nginx上时遇到了502 bad gatewary问题,折腾了半天才搞定.这个问题是我以前在部署yii2时没有遇到过的,因此记在 ...

  6. 原生JS实战:写了个一边玩游戏,一边记JS的API的游戏

    本文是苏福的原创文章,转载请注明出处:苏福CNblog:http://www.cnblogs.com/susufufu/p/5878913.html 本程序[一边玩游戏,一边记JS的API]是本人的个 ...

  7. ArcGIS中的标注和注记

    在ArcMap中可以使用标注和注记来识别要素,选择标注或注记取决于你需要如何控制文本显示以及在ArcMap中如何存储文本. 1.标注只是临时显示相关数据或字段 2.标注用于长时间保存数据以及显示方式. ...

  8. 记处理线上记录垃圾日志 The view 'Error' or its master was not found

    最近监控线上日志,网站是ASP.NET MVC 开发的,发现不少错误日志都记录同样的内容: The view 'Error' or its master was not found or no vie ...

  9. 算法是什么我记不住,But i do it my way. 解一道滴滴出行秋招编程题。

    只因在今日头条刷到一篇文章,我就这样伤害我自己,手贱. 刷头条看到一篇文章写的滴滴出行2017秋招编程题,后来发现原文在这里http://www.cnblogs.com/SHERO-Vae/p/588 ...

随机推荐

  1. .NET MVC4.0与CA对接

    1.改web.confog 2.引用CA提供的 dll 3.在controller层加个方法,记得加上授权认证的特性,获取信息 [Authorize] publi void calogin() { H ...

  2. 第1章 shell编程概述

    1.shell简介 shell是一种具备特殊功能的程序,它提供了用户与内核交互操作的一种接口.它用于接收用户输入的命令,并把它送入到内核去执行. shell是一种应用程序,当用户登录Linux系统时, ...

  3. MySQL学习笔记(二)

    二.SQL基本知识 SQL 是一种典型的非过程化程序设计语言,这种语言的特点是:只指定哪些数据被操纵,至于对这些数据要执行哪些操作,以及这些操作是如何执行的,则未被指定.非过程化程序设计语言的优点在于 ...

  4. h5 吸顶效果 顶部悬浮

    window.onscroll = function(){ var scrollTop = document.documentElement.scrollTop || document.body.sc ...

  5. grep使用

    grep常用的使用方法 grep –rns “match_content”filename 查看匹配内容的行 find /path –name “*.h” –o –name “*.cpp” | xar ...

  6. Android:控件布局(表格布局)TableLayout

    TableLayout继承LinearLayout 实例:用表格布局实现计算机布局>>>>>>>>>>>> 有多少个TableR ...

  7. win7启动文件修复

    1:在xp下运行bcdedit.exe(这软件在windows 7的系统盘下 就在你的c:\windows\system32\这里2:将这软件复制到c盘根目录下3:进入命令提示符 输入 c:4:输入 ...

  8. SQL 常用方法

    EXCEPT :返回两个结果集的差(即从左查询中返回右查询没有找到的所有非重复值). INTERSECT :返回 两个结果集的交集(即两个查询都返回的所有非重复值). UNION :返回两个结果集的并 ...

  9. 设计模式-外观模式(Facade)

    简介 外观模式(Facade),将外部与内部子系统的通信通过一个统一的门面对象进行. 由两部分组成: 门面角色:供外部调用,内部可能组装了多个子系统,多个方法. 子系统角色:子系统的方法也可以直接供外 ...

  10. wp仿系统截图

    wp上直接调用CameraCaptureTask,并没有参数设置截图大小,刚好项目需求要截成正方型,于是写了个仿系统截图的demo出来. 截图如下:     需要做的逻辑运算为: 1.初始化照片时:判 ...