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. SQL基本语句(1)

    利用select的结果创建表 可以通过选择一个表的全部内容(无 WHERE 子句)来拷贝一个表,或利用一个总是失败的 WHERE 子句来创建一个空表,如: mysql> CREATE TABLE ...

  2. ORA-00845: MEMORY_TARGET not supported on this system

    cd $ORACLE_HOME cd dbs cat init.ora cat spfilematedata.ora 查看MEMORY_TARGET设置的大小 修改系统,设置shm的大小大于MEMOR ...

  3. Software: MPEG-7 Feature Extraction Library

    Software MPEG-7 Feature Extraction Library : This library is adapted from MPEG-7 XM Reference Softwa ...

  4. 洛谷P1613 跑路

    P1613 跑路 176通过 539提交 题目提供者该用户不存在 标签倍增动态规划 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 这个题的数据.. 题意问题 表意 题目描述 小A的工作不仅繁 ...

  5. Java基础——IO流

    今天刚刚看完java的io流操作,把主要的脉络看了一遍,不能保证以后使用时都能得心应手,但是最起码用到时知道有这么一个功能可以实现,下面对学习进行一下简单的总结: IO流主要用于硬板.内存.键盘等处理 ...

  6. php接口post提交方法 。

    方法一,用 file_get_contents function send_post($url, $post_data) { //$postdata = http_build_query($post_ ...

  7. 使用Cookie保存商品浏览记录

    数据流程:页面上是商品列表,点击<a href="productServlet">商品名</a> ==>跳转到自定义的servlet中进行处理,先得到 ...

  8. 使用codeblock实现JNI开发-2016.01.31

    使用交叉编译工具实现andorid平台下的jni开发,记录codeblock配置过程,方便后续参考. 1 工具版本信息 NDK r8b Code::Blocks 10.05 2 配置过程 使用code ...

  9. jquery selector 使用方法

    <select class="selector"></select> 1 设置value为pxx的项选中 $(".selector"). ...

  10. js 全选全不选

     checkAll: function () { //全选         if ($("#chk_SelectAll").is(":checked")) { ...