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. Ajax.BeginForm 上传文件

    在 Mvc 中上传文件时通常使用 Html.BeginForm 标签,同时对Form 添加属性 enctype = "multipart/form-data",前端代码如下: @H ...

  2. ionic cordova plugin simple demo

    要用cordova plugin 的话还是需要设置一下的 1. 下载 ng-cordova.js download the zip file here 2. 在index.html 中引用 (cord ...

  3. WF4 常用类<第二篇>

    一.WorkflowInvoker 常用方法如下: 方法 说明 BeginInvoke() 使用指定的 AsyncCallback 和用户提供的状态以异步方式调用工作流 EndInvoke() 返回使 ...

  4. 【MySQL】MySQL索引背后的之使用策略及优化【转】

    转自:http://database.ctocio.com.cn/353/11664853.shtml 另外很不错的对于索引及索引优化的文章: http://www.cnblogs.com/magia ...

  5. iOS之UIAlertView的使用

    UIAlertView: 1.普通使用: //普通的alert UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"title&quo ...

  6. 【Framework】深入研究Asp.net页面的生命周期

    介绍 Asp.net是微软.Net战略的一个组成部分.它相对以前的Asp有了很大的发展,引入了许多的新机制.本文就Asp.net页面的生命周期向大家做一个初步的介绍,以期能起到指导大家更好.更灵活地操 ...

  7. What is the behavior of lnk files?

    I access a files which name is "abc.doc", no doubt a lnk file "abc.doc.lnk" show ...

  8. leetcode 38

    38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows:1, 11, ...

  9. 1.异步消息Jms及其JmsTemplate的源代码分析,消息代理ActiveMQ

    一. 介绍 借助Spring,有多种异步消息的可选方案,本章使用Jms.Jms的消息模型有两种,点对点消息模型(队列实现)和发布-订阅消息模型(主题). 图1.点对点消息模型(一对一) 图2.发布-订 ...

  10. kickstart简介 20140707

    kickstart是红帽发行版中的一种安装方式,它通过以配置文件的方式来记录linux系统安装是的各项参数和想要安装的软件.只要配置正确, 整个安装过程中无需人工交互参与,达到无人值守安装的目的,因而 ...