网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Green.AgileMapper项目-新增DO和DTO代码生成

     如果你还不了解Green.AgileMapper的用意,作用请先一步到上篇Green.AgileMapper开源项目的使用,如果你觉得运行时(Runtime)的Mapper效率存在问题,在这个版本中有了更新,新增了C#直接代码的生成,这里的生成都已3.0后的扩展方法实现,你可以很方便的一句代码实现两者的转化。

目前创新互联公司已为上千的企业提供了网站建设、域名、网络空间、绵阳服务器托管、企业网站设计、赣州网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

    代码生成我本想利用T4模板,但是由于我们的项目IDE版本是VS2008,对于T4的参数传递支持还不是很友好,你可能说用AppDomain.SetData,或者CallContext.LogicalSetData,但是可惜我们的饿MappingRule是不可序列化的,所以最后我只得采用了字符串拼接最笨的办法,为了应对生成策略不同,在这里我们加入了策略模式来应付,来看看代码结构吧:

Green.AgileMapper项目-新增DO和DTO代码生成

   在这里只支持对do的二级属性映射为dto的平面属性,全部针对IMappingRule生成代码,在这里为了生成合法的代码而非表达式,对以前的表达式进行了重新的标准约定,在代码注释。以及对上个版本的List转换进行了多次重载,主要为了满足DTo到DO对象的特殊要求,因为我们在领域开发存储DTO的时候都是需要先取出DO对象在根据DTO在DO对象的基础上进行修改,以便ORM领域框架或者UOW的跟踪记录。

下面看看任然是上个测试类模型的代码生成(在这里测试DO,DTO类进行了重构为了更全面的测试,具体请看CodePlex http://agilemapper.codeplex.com/代码):

 

  1. StudenDo stu = new StudenDo()   
  2.           {   
  3.               ID = 1,   
  4.               Name = "test1",   
  5.               Sex = Sex.女,   
  6.               Address = new Address()   
  7.               {   
  8.                   Country = "中国",   
  9.                   Province = "四川",   
  10.                   Street = "高新区"   
  11.               },   
  12.               CourseIds = new List() { "1", "2", "3" },   
  13.               Propertys = new List() { new KeyValuePair() { Key = "1", Value = "1" } },   
  14.               ContactWay = new ContactWay()   
  15.               {   
  16.                   Phone = "1111111111111111",   
  17.                   Email = "xxxx@12f",   
  18.                   QQ = "7889789999889"   
  19.               }   
  20.           };   
  21.           Func fun = (f, j) => f.ID == j.ID;   
  22.           var s = fun.ToString();   
  23.  
  24.           var mapper = ObjectMapperManager.Default.GetMapper();   
  25.           mapper.AgileMapperTemplateStrategy.DefaultEqualExpression = "{0}.ID == {1}.ID && {1}.ID != 0";                    
  26.           var str1 = mapper.CodeGenerator();   
  27.           System.IO.File.Delete(@"E:\Project\OpenSource\AgileMapper\AgileMappper.Test\CodeTemplate.Test\1.cs");   
  28.           System.IO.File.AppendAllText(@"E:\Project\OpenSource\AgileMapper\AgileMappper.Test\CodeTemplate.Test\1.cs", str1);   
  29.  
  30.           var mapper1 = ObjectMapperManager.Default.GetMapper();                      
  31.           str1 = mapper1.CodeGenerator();   
  32.           System.IO.File.Delete(@"E:\Project\OpenSource\AgileMapper\AgileMappper.Test\CodeTemplate.Test\2.cs");   
  33.           System.IO.File.AppendAllText(@"E:\Project\OpenSource\AgileMapper\AgileMappper.Test\CodeTemplate.Test\2.cs", str1);   
  34.  
  35.           var mapper2 = ObjectMapperManager.Default.GetMapper();              
  36.           str1 = mapper2.CodeGenerator();   
  37.           System.IO.File.Delete(@"E:\Project\OpenSource\AgileMapper\AgileMappper.Test\CodeTemplate.Test\3.cs");   
  38.           System.IO.File.AppendAllText(@"E:\Project\OpenSource\AgileMapper\AgileMappper.Test\CodeTemplate.Test\3.cs", str1);  
  39.  

最后的生成文件:

  1. 1.cs:  
  2.  
  3. using System;   
  4. using System.Linq;   
  5. using System.Data;   
  6. using System.Collections.Generic;   
  7.  
  8. namespace Green.AgileMapper   
  9. {   
  10.     public static partial class AgileMapperMapping   
  11.     {   
  12.         ///   
  13.         /// Green.AgileMapper.Test.StudenDto  Warp fromObj Green.AgileMapper.Test.StudenDo;  
  14.         ///   
  15.         /// Green.AgileMapper.Test.StudenDo  
  16.         /// Green.AgileMapper.Test.StudenDto  
  17.         public static Green.AgileMapper.Test.StudenDto Warp(this Green.AgileMapper.Test.StudenDo domainObj)   
  18.         {   
  19.             var fromObj = new Green.AgileMapper.Test.StudenDto();   
  20.             fromObj.ID = domainObj.ID;   
  21.             fromObj.Name = domainObj.Name;   
  22.             fromObj.Sex = domainObj.Sex;   
  23.             if (domainObj.Address != null)   
  24.             {   
  25.                 fromObj.Country = domainObj.Address.Country;   
  26.                 fromObj.Province = domainObj.Address.Province;   
  27.             }   
  28.             fromObj.Particular = domainObj.Address.Country + " 国籍 " + domainObj.Address.Province + " 省 ";   
  29.             fromObj.FirstPropertyKey = domainObj.Propertys[0].Key;   
  30.             if (domainObj.ContactWay != null)   
  31.             {   
  32.                 fromObj.ContactWay = domainObj.ContactWay.Warp();   
  33.             }   
  34.             if (domainObj.CourseIds != null)   
  35.             {   
  36.                 fromObj.CourseIds = new List();   
  37.                 foreach (var item_CourseIds in domainObj.CourseIds)   
  38.                 {   
  39.                     fromObj.CourseIds.Add(item_CourseIds);   
  40.                 }   
  41.             }   
  42.             if (domainObj.Propertys != null)   
  43.             {   
  44.                 fromObj.Propertys = domainObj.Propertys.Warp();   
  45.             }   
  46.             return fromObj;   
  47.         }   
  48.  
  49.         ///   
  50.         /// Green.AgileMapper.Test.StudenDto  Warp domainObj Green.AgileMapper.Test.StudenDo;  
  51.         ///   
  52.         /// Green.AgileMapper.Test.StudenDo  
  53.         /// fromObj  
  54.         public static void Warp(this Green.AgileMapper.Test.StudenDto fromObj, Green.AgileMapper.Test.StudenDo domainObj)   
  55.         {   
  56.             if (fromObj == null)   
  57.             {   
  58.                 return;   
  59.             }   
  60.             if (domainObj == null)   
  61.             {   
  62.                 domainObj = new Green.AgileMapper.Test.StudenDo();   
  63.             }   
  64.             domainObj.ID = fromObj.ID;   
  65.             domainObj.Name = fromObj.Name;   
  66.             domainObj.Sex = fromObj.Sex;   
  67.             if (domainObj.Address == null)   
  68.             {   
  69.                 domainObj.Address = new Green.AgileMapper.Test.Address();   
  70.             }   
  71.             domainObj.Address.Country = fromObj.Country;   
  72.             domainObj.Address.Province = fromObj.Province;   
  73.  
  74.             if (domainObj.ContactWay == null)   
  75.             {   
  76.                 domainObj.ContactWay = new Green.AgileMapper.Test.ContactWay();   
  77.             }   
  78.             fromObj.ContactWay.Warp(domainObj.ContactWay);   
  79.             if (fromObj.CourseIds != null)   
  80.             {   
  81.                 if (domainObj.CourseIds == null)   
  82.                 {   
  83.                     domainObj.CourseIds = new List();   
  84.                 }   
  85.                 domainObj.CourseIds.Clear();   
  86.                 foreach (var item_CourseIds in fromObj.CourseIds)   
  87.                 {   
  88.                     domainObj.CourseIds.Add(item_CourseIds);   
  89.                 }   
  90.             }   
  91.             if (fromObj.Propertys != null)   
  92.             {   
  93.                 if (domainObj.Propertys == null)   
  94.                 {   
  95.                     domainObj.Propertys = new List();   
  96.                 }   
  97.                 fromObj.Propertys.Warp(domainObj.Propertys, (fromObjItem, domainObjItem) => fromObjItem.Key == domainObjItem.Key, true);   
  98.             }   
  99.  
  100.         }   
  101.  
  102.         ///   
  103.         /// Green.AgileMapper.Test.StudenDto collection Warp fromObj Green.AgileMapper.Test.StudenDo collection;  
  104.         ///   
  105.         /// Green.AgileMapper.Test.StudenDto collection  
  106.         /// Green.AgileMapper.Test.StudenDo collection         
  107.         public static List Warp(this IList domainObj)   
  108.         {   
  109.             List froms = new List();   
  110.             domainObj.ToList().ForEach(t =>   
  111.             {   
  112.                 froms.Add(Warp(t));   
  113.             });   
  114.             return froms;   
  115.         }   
  116.  
  117.         ///   
  118.         /// Green.AgileMapper.Test.StudenDto collection Warp domainObj Green.AgileMapper.Test.StudenDo collection;  
  119.         ///   
  120.         /// Green.AgileMapper.Test.StudenDto collection  
  121.         /// Green.AgileMapper.Test.StudenDo collection       
  122.         public static void Warp(this IList fromObj, IList domainObj)   
  123.         {   
  124.             fromObj.Warp(domainObj, (fromObjItem, domainObjItem) => fromObjItem.ID == domainObjItem.ID && domainObjItem.ID != 0, false);   
  125.         }   
  126.  
  127.         ///   
  128.         /// Green.AgileMapper.Test.StudenDto collection Warp domainObj Green.AgileMapper.Test.StudenDo collection;  
  129.         ///   
  130.         /// Green.AgileMapper.Test.StudenDto collection  
  131.         /// Green.AgileMapper.Test.StudenDo collection  
  132.         /// Delete the item that not in From collection            
  133.         public static void Warp(this IList fromObj, IList domainObj, bool isDeleteNotInFromItem)   
  134.         {   
  135.             fromObj.Warp(domainObj, (fromObjItem, domainObjItem) => fromObjItem.ID == domainObjItem.ID && domainObjItem.ID != 0, isDeleteNotInFromItem);   
  136.         }   
  137.  
  138.         ///   
  139.         /// Green.AgileMapper.Test.StudenDto collection Warp domainObj Green.AgileMapper.Test.StudenDo collection;  
  140.         ///   
  141.         /// Green.AgileMapper.Test.StudenDto collection  
  142.         /// Green.AgileMapper.Test.StudenDo collection  
  143.         /// the from item equal to item expression            
  144.         public static void Warp(this IList fromObj, IList domainObj, Func equalPredicate)   
  145.         {   
  146.             fromObj.Warp(domainObj, equalPredicate, false);   
  147.         }   
  148.  
  149.         ///   
  150.         /// Green.AgileMapper.Test.StudenDto collection Warp domainObj Green.AgileMapper.Test.StudenDo collection;  
  151.         ///   
  152.         /// Green.AgileMapper.Test.StudenDto collection  
  153.         /// Green.AgileMapper.Test.StudenDo collection  
  154.         /// the from item equal to item expression       
  155.         /// Delete the item that not in From collection      
  156.         public static void Warp(this IList fromObj, IList domainObj, Func equalPredicate, bool isDeleteNotInFromItem)   
  157.         {   
  158.             if (fromObj == null)   
  159.             {   
  160.                 return;   
  161.             }   
  162.  
  163.             if (domainObj == null)   
  164.             {   
  165.                 domainObj = new List();   
  166.             }   
  167.  
  168.             fromObj.ToList().ForEach(fromObjItem =>   
  169.             {   
  170.                 Green.AgileMapper.Test.StudenDo toItem = default(Green.AgileMapper.Test.StudenDo);   
  171.                 if (equalPredicate != null)   
  172.                 {   
  173.                     toItem = domainObj.SingleOrDefault(domainObjItem => equalPredicate(fromObjItem, domainObjItem));   
  174.                 }   
  175.                 if (toItem == null)   
  176.                 {   
  177.                     toItem = new Green.AgileMapper.Test.StudenDo();   
  178.                     domainObj.Add(toItem);   
  179.                 }   
  180.                 Warp(fromObjItem, toItem);   
  181.             });   
  182.  
  183.             if (isDeleteNotInFromItem)   
  184.             {   
  185.                 domainObj.Where(domainObjItem => fromObj.FirstOrDefault(fromObjItem => equalPredicate(fromObjItem, domainObjItem)) == null)   
  186.                     .ToList().ForEach(t =>   
  187.                     {   
  188.                         domainObj.Remove(t);   
  189.                     });   
  190.             }   
  191.         }   
  192.     }   
  193. }  
  194.  
  195.    
  196.  
  197. 2.cs  
  198.  
  199. using System;   
  200. using System.Linq;   
  201. using System.Data;   
  202. using System.Collections.Generic;   
  203.  
  204. namespace Green.AgileMapper   
  205. {   
  206.     public static partial class AgileMapperMapping   
  207.     {   
  208.         ///   
  209.         /// Green.AgileMapper.Test.ContactWayDto  Warp fromObj Green.AgileMapper.Test.ContactWay;  
  210.         ///   
  211.         /// Green.AgileMapper.Test.ContactWay  
  212.         /// Green.AgileMapper.Test.ContactWayDto  
  213.         public static Green.AgileMapper.Test.ContactWayDto Warp(this Green.AgileMapper.Test.ContactWay domainObj)   
  214.         {   
  215.             var fromObj = new Green.AgileMapper.Test.ContactWayDto();   
  216.             fromObj.Phone = domainObj.Phone;   
  217.             fromObj.Email = domainObj.Email;   
  218.             fromObj.QQ = domainObj.QQ;   
  219.             return fromObj;   
  220.         }   
  221.  
  222.         ///   
  223.         /// Green.AgileMapper.Test.ContactWayDto  Warp domainObj Green.AgileMapper.Test.ContactWay;  
  224.         ///   
  225.         /// Green.AgileMapper.Test.ContactWay  
  226.         /// fromObj  
  227.         public static void Warp(this Green.AgileMapper.Test.ContactWayDto fromObj, Green.AgileMapper.Test.ContactWay domainObj)   
  228.         {   
  229.             if (fromObj == null)   
  230.             {   
  231.                 return;   
  232.             }   
  233.             if (domainObj == null)   
  234.             {   
  235.                 domainObj = new Green.AgileMapper.Test.ContactWay();   
  236.             }   
  237.             domainObj.Phone = fromObj.Phone;   
  238.             domainObj.Email = fromObj.Email;   
  239.             domainObj.QQ = fromObj.QQ;   
  240.  
  241.         }   
  242.  
  243.         ///   
  244.         /// Green.AgileMapper.Test.ContactWayDto collection Warp fromObj Green.AgileMapper.Test.ContactWay collection;  
  245.         ///   
  246.         /// Green.AgileMapper.Test.ContactWayDto collection  
  247.         /// Green.AgileMapper.Test.ContactWay collection         
  248.         public static List Warp(this IList domainObj)   
  249.         {   
  250.             List froms = new List();   
  251.             domainObj.ToList().ForEach(t =>   
  252.             {   
  253.                 froms.Add(Warp(t));   
  254.             });   
  255.             return froms;   
  256.         }   
  257.  
  258.         ///   
  259.         /// Green.AgileMapper.Test.ContactWayDto collection Warp domainObj Green.AgileMapper.Test.ContactWay collection;  
  260.         ///   
  261.         /// Green.AgileMapper.Test.ContactWayDto collection  
  262.         /// Green.AgileMapper.Test.ContactWay collection       
  263.         public static void Warp(this IList fromObj, IList domainObj)   
  264.         {   
  265.             fromObj.Warp(domainObj, (fromObjItem, domainObjItem) => fromObjItem.Equals(domainObjItem), false);   
  266.         }   
  267.  
  268.         ///   
  269.         /// Green.AgileMapper.Test.ContactWayDto collection Warp domainObj Green.AgileMapper.Test.ContactWay collection;  
  270.         ///   
  271.         /// Green.AgileMapper.Test.ContactWayDto collection  
  272.         /// Green.AgileMapper.Test.ContactWay collection  
  273.         /// Delete the item that not in From collection            
  274.         public static void Warp(this IList fromObj, IList domainObj, bool isDeleteNotInFromItem)   
  275.         {   
  276.             fromObj.Warp(domainObj, (fromObjItem, domainObjItem) => fromObjItem.Equals(domainObjItem), isDeleteNotInFromItem);   
  277.         }   
  278.  
  279.         ///   
  280.         /// Green.AgileMapper.Test.ContactWayDto collection Warp domainObj Green.AgileMapper.Test.ContactWay collection;  
  281.         ///   
  282.         /// Green.AgileMapper.Test.ContactWayDto collection  
  283.         /// Green.AgileMapper.Test.ContactWay collection  
  284.         /// the from item equal to item expression            
  285.         public static void Warp(this IList fromObj, IList domainObj, Func equalPredicate)   
  286.         {   
  287.             fromObj.Warp(domainObj, equalPredicate, false);   
  288.         }   
  289.  
  290.         ///   
  291.         /// Green.AgileMapper.Test.ContactWayDto collection Warp domainObj Green.AgileMapper.Test.ContactWay collection;  
  292.         ///   
  293.         /// Green.AgileMapper.Test.ContactWayDto collection  
  294.         /// Green.AgileMapper.Test.ContactWay collection  
  295.         /// the from item equal to item expression       
  296.         /// Delete the item that not in From collection      
  297.         public static void Warp(this IList fromObj, IList domainObj, Func equalPredicate, bool isDeleteNotInFromItem)   
  298.         {   
  299.             if (fromObj == null)   
  300.             {   
  301.                 return;   
  302.             }   
  303.  
  304.             if (domainObj == null)   
  305.             {   
  306.                 domainObj = new List();   
  307.             }   
  308.  
  309.             fromObj.ToList().ForEach(fromObjItem =>   
  310.             {   
  311.                 Green.AgileMapper.Test.ContactWay toItem = default(Green.AgileMapper.Test.ContactWay);   
  312.   &nb
    新闻标题:Green.AgileMapper项目-新增DO和DTO代码生成
    文章出自:http://bjjierui.cn/article/gpddsj.html

其他资讯