架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 17922|回复: 0

[资料] .NET最快的OOM对象映射组件Tiny Mapper之项目实践

[复制链接]
发表于 2018-5-31 13:08:02 | 显示全部楼层 |阅读模式
第一步:了解类库方法:TinyMapper 主要有两个函数:

  1. TinyMapper.Bind<T1, T2>();//绑定映射关系

  2. TinyMapper.Map<T>(obj);//从对象获取想要的对象
复制代码


第二步:初始化Mapping设置

说明:类似的Mapping设置无非两种模式:代码静态对象初始化模式,xml配置模式,很荣幸TinyMapper支持的是静态字典。

  1. public static class TinyMapContext
  2.     {
  3.         public static void InitMapping()
  4.         {
  5.             TinyMapper.Bind<Person, PersonDto>();
  6.             TinyMapper.Bind<Person, PersonDto>(config =>
  7.             {
  8.                 config.Ignore(x => x.Id);//忽略ID字段
  9.                 config.Bind(x => x.Name, y => y.UserName);//将源类型和目标类型的字段对应绑定起来
  10.                 config.Bind(x => x.Age, y => y.Age);//将源类型和目标类型的字段对应绑定起来
  11.             });

  12.             TinyMapper.Bind<Person, PersonDto>(config =>
  13.             {
  14.                 config.Ignore(x => x.Id);//忽略ID字段
  15.                 //将源类型和目标类型的字段对应绑定起来
  16.                 config.Bind(x => x.Name, y => y.UserName);
  17.                 config.Bind(x => x.Age, y => y.Age);
  18.                 config.Bind(x => x.Address, y => y.Address);
  19.                 config.Bind(x => x.Emails, y => y.Emails);
  20.             });

  21.             TinyMapper.Bind<PersonDto,Person>();
  22.             TinyMapper.Bind<PersonDto,Person>(config =>
  23.             {
  24.                 config.Bind(x => x.Id,y=>y.Id);
  25.                 config.Bind(x => x.UserName, y => y.Name);
  26.                 config.Bind(x => x.Age, y => y.Age);
  27.             });

  28.             TinyMapper.Bind<PersonDto,Person>(config =>
  29.             {
  30.                 config.Bind(x => x.Id,y=>y.Id);//忽略ID字段
  31.                 //将源类型和目标类型的字段对应绑定起来
  32.                 config.Bind(x =>x.UserName,y=> y.Name);
  33.                 config.Bind(x => x.Age, y => y.Age);
  34.                 config.Bind(x => x.Address, y => y.Address);
  35.                 config.Bind(x => x.Emails, y => y.Emails);
  36.             });

  37.         }
  38.         public static T GetMapObject<T>(object obj) where T:class
  39.         {
  40.            return TinyMapper.Map<T>(obj);
  41.         }
  42.     }
复制代码
说明:以上mapping映射中,针对于原作者的代码,额外添加了:由PersonDto=》Person的映射关系。

T GetMapObject<T>(object obj) where T:class 的作用会在后面的代码中体现出来。 简单一个方法,威力不可小嘘~~

第三步:DtoModel -》Model

从数据库模型映射到领域模型:

  1. var p = TinyMapContext.GetMapObject<Person>(personDto);
复制代码


第四步:Model-》DtoModel

从领域模型到数据库模型:

  1. var personDto = TinyMapContext.GetMapObject<PersonDto>(person);
复制代码


第五步:List<Model>=>List<DtoModel> 或者List<DtoModel>=>List<Model>

  1. /// <summary>
  2.         /// 测试列表对象。
  3.         /// </summary>
  4.         static void Test4()
  5.         {
  6.             List<Person> personList = new List<Person>(){
  7.             new Person
  8.             {
  9.                 Id = Guid.NewGuid().ToString(),
  10.                 Name = "John1",
  11.                 Age = 22,
  12.                 Address = new Address() { Phone = "1880393", Street = "Shanghai", ZipCode = "121212" },
  13.                 Emails = new List<string>() { "aaa@bb.com", "acx@cc.com" }
  14.             },
  15.             new Person
  16.             {
  17.                 Id = Guid.NewGuid().ToString(),
  18.                 Name = "John2",
  19.                 Age = 22,
  20.                 Address = new Address() { Phone = "1880393", Street = "Shanghai", ZipCode = "121212" },
  21.                 Emails = new List<string>() { "aaa@bb.com", "acx@cc.com" }
  22.             },
  23.               new Person
  24.             {
  25.                 Id = Guid.NewGuid().ToString(),
  26.                 Name = "John3",
  27.                 Age = 22,
  28.                 Address = new Address() { Phone = "1880393", Street = "Shanghai", ZipCode = "121212" },
  29.                 Emails = new List<string>() { "aaa@bb.com", "acx@cc.com" }
  30.             },
  31.               new Person
  32.             {
  33.                 Id = Guid.NewGuid().ToString(),
  34.                 Name = "John4",
  35.                 Age = 22,
  36.                 Address = new Address() { Phone = "1880393", Street = "Shanghai", ZipCode = "121212" },
  37.                 Emails = new List<string>() { "aaa@bb.com", "acx@cc.com" }
  38.             }
  39.             };


  40.             var personDtoList = TinyMapContext.GetMapObject<List<PersonDto>>(personList);
  41.             foreach (var item in personDtoList)
  42.             {
  43.                 DebugOutputUtil.DebugOutput(item.UserName);
  44.             }


  45.             var pList = TinyMapContext.GetMapObject<List<Person>>(personDtoList);
  46.             foreach (var item in pList)
  47.             {
  48.                 DebugOutputUtil.DebugOutput(item.Name);
  49.             }
  50.         }
复制代码

特别说明:对于获取列表类型的对象,不需要在静态初始化映射中额外添加 IList类型的对象。 TinyMapper会自动按照"映射过的基础类型"动态的帮你获取想要的数据。

参考链接:

.NET平台开源项目速览(14)最快的对象映射组件Tiny Mapper http://www.cnblogs.com/asxinyu/p ... ect_TinyMapper.html
EmitMapper 和TinyMapper 两者简单对比 https://blog.csdn.net/umke888/article/details/54880670





上一篇:Newtonsoft.Json忽略DataContract特性
下一篇:c#线程学习之ManualResetEvent和AutoResetEvent的区别
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2025-1-17 02:17

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表