[C#] 记-TinyMapper使用
- What is TinyMapper
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使用的更多相关文章
- Spark踩坑记——Spark Streaming+Kafka
[TOC] 前言 在WeTest舆情项目中,需要对每天千万级的游戏评论信息进行词频统计,在生产者一端,我们将数据按照每天的拉取时间存入了Kafka当中,而在消费者一端,我们利用了spark strea ...
- Spark踩坑记——数据库(Hbase+Mysql)
[TOC] 前言 在使用Spark Streaming的过程中对于计算产生结果的进行持久化时,我们往往需要操作数据库,去统计或者改变一些值.最近一个实时消费者处理任务,在使用spark streami ...
- 这些年一直记不住的 Java I/O
参考资料 该文中的内容来源于 Oracle 的官方文档.Oracle 在 Java 方面的文档是非常完善的.对 Java 8 感兴趣的朋友,可以从这个总入口 Java SE 8 Documentati ...
- 千回百折:百度Java研发offer斩获记和经验分享
起因 面试过程 等待offer的过程中悟道 Java面试常考知识点个人总结 过程 百度——作为国内互联网的巨头之一,最近的一些风波对其褒贬不一,但是类似事件不是第一次发生,也绝对不是最后一次,对于真的 ...
- 记一次nginx部署yii2项目时502 bad gateway错误的排查
周六闲来无事,就试着安装和部署下yii2,安装过程没什么问题,但部署到nginx上时遇到了502 bad gatewary问题,折腾了半天才搞定.这个问题是我以前在部署yii2时没有遇到过的,因此记在 ...
- 原生JS实战:写了个一边玩游戏,一边记JS的API的游戏
本文是苏福的原创文章,转载请注明出处:苏福CNblog:http://www.cnblogs.com/susufufu/p/5878913.html 本程序[一边玩游戏,一边记JS的API]是本人的个 ...
- ArcGIS中的标注和注记
在ArcMap中可以使用标注和注记来识别要素,选择标注或注记取决于你需要如何控制文本显示以及在ArcMap中如何存储文本. 1.标注只是临时显示相关数据或字段 2.标注用于长时间保存数据以及显示方式. ...
- 记处理线上记录垃圾日志 The view 'Error' or its master was not found
最近监控线上日志,网站是ASP.NET MVC 开发的,发现不少错误日志都记录同样的内容: The view 'Error' or its master was not found or no vie ...
- 算法是什么我记不住,But i do it my way. 解一道滴滴出行秋招编程题。
只因在今日头条刷到一篇文章,我就这样伤害我自己,手贱. 刷头条看到一篇文章写的滴滴出行2017秋招编程题,后来发现原文在这里http://www.cnblogs.com/SHERO-Vae/p/588 ...
随机推荐
- 【翻译习作】 Windows Workflow Foundation程序开发-第一章03
1.2.2.Visual Studio 2005扩展包 微软也为Windows Workflow开发者提供了Visual Studio 2005扩展包.扩展包将许多功能集成到Visual Studio ...
- Nodejs文件服务器
最近一直在忙于一个比较大的项目,在项目中需要有个文件服务器来支持.老鸟们建议我去用NodeJs来实现,我在接手这个项目之前其实并不了解NodeJs,但是一直想去了解.借着这个机会好好去学习一下.下面是 ...
- Flash图表控件FusionCharts如何定制图表中的趋势线和趋势区
FusionCharts中的趋势线是什么 趋势线是横跨图标的水平/垂直线条,用来表示一些预订数据值. 在图表中展示趋势线 用户可以使用<chart>元素中的trendlines属性来显示图 ...
- 再看.net本质
1.[资源的地址-通用资源标识符] 我们在地址栏中输入的内容称为通用资源标识符(Universal Resource Identifier,URI),它有很多种形式,在Web中我们通常使用称为统一资源 ...
- ASP.NET MVC 开源项目学习之ProDinner (一)
首先在github上面将ProDinner项目通过 Git Bash 克隆到本地,接下来我们开始分析这个项目吧~ 系统采用.Net 4.5, Asp.net Mvc 5,VS2012,Sql serv ...
- html5相关
1.语义化标签: header\section\aside\footer 2.音视频播放: video\audio 其中video支持ogg\mpeg4\webm格式的视频: 3.canvas 一.画 ...
- ln: creating hard link 问题
其实很简单: chattr -R -i xxxxx #xxxxx为你需要修改的文件,这样就可以了... 下面全不是重点,扯淡.... ln: creating hard link ...
- centOS 6.4下安装中文输入法
1.用root登录 ,或su root 2.yum install "@Chinese Support" 3.exit 4.回到桌面,system->preferences- ...
- 打包新版本上传到AppStore时报错 ERROR ITMS-90034:
今天打包新版本上传到AppStore时报错 ERROR ITMS-90034:"Missing or invalid signature.The bundle'com.xxx.xxx' at ...
- Ubuntu 14.04 – How to install xrdp in Ubuntu 14.04
http://c-nergy.be/blog/?p=5305 Hello World, Ubuntu 14.04 has been released on April 17th 2014 and we ...