[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 ...
随机推荐
- 百度富文本编辑器UEditor安装配置全过程
网站开发时富文本编辑器是必不可少的,他可以让用户自行编辑内容的样式然后上传到后台!下面我们来介绍如何安装使用百度富文本编辑器 一.下载并且设置百度富文本编辑器的样式 你可以去百度UEditor ...
- Adobe AIR and Flex - 实现堆栈容器
1.需求描述: 在对云平台的监控中,我们经常需要在一张图上可视化的描述集群下的物理机所虚拟的虚拟机使用情况,以及超卖情况.那么传统的chart图就不满足我们的需求了,那么要实现这样一个定制化的char ...
- nginx 配置文件参数说明
#运行用户 user www-data; #启动进程,通常设置成和cpu的数量相等 worker_processes 1; #全局错误日志及PID文件 error_log /var/log ...
- 洛谷P1211 [USACO1.3]牛式 Prime Cryptarithm
P1211 [USACO1.3]牛式 Prime Cryptarithm 187通过 234提交 题目提供者该用户不存在 标签USACO 难度普及- 提交 讨论 题解 最新讨论 题面错误 题目描述 ...
- CHARINDEX用法
CHARINDEX返回字符串中指定表达式的起始位置. 语法CHARINDEX ( expression1 , expression2 [ , start_location ] ) 参数expre ...
- 解决jQuery插件重名问题
jQuery第三方插件命名冲突: 1.以某种方法为自己创建的jQuery插件添加命名空间,以免名称冲突.比如:在自己的插件名之前添加某类名称前缀. 2.避免影响全局命名空间.将自己的所有函数调用和变量 ...
- 开源项目:libbpg
1 ubuntu下编译libbpg(编译机器64bit) 安装cmake,libpng,yasm,gcc,g++ cmake版本最低为2.8.8,安装完毕后使用cmake --version查看是否安 ...
- 监控系统 - check_mk_agent
系统级监控 cpu (system, user) memory (cache, buffer, use)(MB) load (cpu core) diskspace (used, inode)(GB) ...
- JS与JQ倒计时的写法
页面需要制作一个倒计时的功能:然后度娘了一遍,找到两种写法,原生JS与JQ 的,经过测试原生JS在IE可能会有不刷新的现象所以结合了一个大神的JQ写法修改好了一个. 原生JS写法: HTML: < ...
- SWFUpload使用指南
SWFUpload是一个flash和js相结合而成的文件上传插件,其功能非常强大. SWFUpload的特点: 1.用flash进行上传,页面无刷新,且可自定义Flash按钮的样式; 2.可以在浏览器 ...