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

发布时间:2020-06-26 03:34:00 作者:破狼
来源:网络 阅读:373

     如果你还不了解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<string>() { "1""2""3" },   
  13.               Propertys = new List<KeyValuePair>() { 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<StudenDo, StudenDo, bool> fun = (f, j) =&gt; f.ID == j.ID;   
  22.           var s = fun.ToString();   
  23.  
  24.           var mapper = ObjectMapperManager.Default.GetMapper<StudenDto, StudenDo>();   
  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<ContactWayDto, ContactWay>();                      
  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<KeyValuePairDto, KeyValuePair>();              
  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.         /// <summary>   
  13.         /// Green.AgileMapper.Test.StudenDto  Warp fromObj Green.AgileMapper.Test.StudenDo;   
  14.         /// </summary>   
  15.         /// <param name="domainObj">Green.AgileMapper.Test.StudenDo</param>   
  16.         /// <returns>Green.AgileMapper.Test.StudenDto</returns>   
  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<System.String>();   
  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.         /// <summary>   
  50.         /// Green.AgileMapper.Test.StudenDto  Warp domainObj Green.AgileMapper.Test.StudenDo;   
  51.         /// </summary>   
  52.         /// <param name="domainObj">Green.AgileMapper.Test.StudenDo</param>   
  53.         /// <returns>fromObj</returns>   
  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<System.String>();   
  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<Green.AgileMapper.Test.KeyValuePair>();   
  96.                 }   
  97.                 fromObj.Propertys.Warp(domainObj.Propertys, (fromObjItem, domainObjItem) =&gt; fromObjItem.Key == domainObjItem.Key, true);   
  98.             }   
  99.  
  100.         }   
  101.  
  102.         /// <summary>   
  103.         /// Green.AgileMapper.Test.StudenDto collection Warp fromObj Green.AgileMapper.Test.StudenDo collection;   
  104.         /// </summary>   
  105.         /// <param name="form">Green.AgileMapper.Test.StudenDto collection</param>   
  106.         /// <param name="domainObj">Green.AgileMapper.Test.StudenDo collection</param>          
  107.         public static List<Green.AgileMapper.Test.StudenDto> Warp(this IList<Green.AgileMapper.Test.StudenDo> domainObj)   
  108.         {   
  109.             List<Green.AgileMapper.Test.StudenDto> froms = new List<Green.AgileMapper.Test.StudenDto>();   
  110.             domainObj.ToList().ForEach(t =&gt;   
  111.             {   
  112.                 froms.Add(Warp(t));   
  113.             });   
  114.             return froms;   
  115.         }   
  116.  
  117.         /// <summary>   
  118.         /// Green.AgileMapper.Test.StudenDto collection Warp domainObj Green.AgileMapper.Test.StudenDo collection;   
  119.         /// </summary>   
  120.         /// <param name="fromObj">Green.AgileMapper.Test.StudenDto collection</param>   
  121.         /// <param name="domainObj">Green.AgileMapper.Test.StudenDo collection</param>        
  122.         public static void Warp(this IList<Green.AgileMapper.Test.StudenDto> fromObj, IList<Green.AgileMapper.Test.StudenDo> domainObj)   
  123.         {   
  124.             fromObj.Warp(domainObj, (fromObjItem, domainObjItem) =&gt; fromObjItem.ID == domainObjItem.ID && domainObjItem.ID != 0, false);   
  125.         }   
  126.  
  127.         /// <summary>   
  128.         /// Green.AgileMapper.Test.StudenDto collection Warp domainObj Green.AgileMapper.Test.StudenDo collection;   
  129.         /// </summary>   
  130.         /// <param name="fromObj">Green.AgileMapper.Test.StudenDto collection</param>   
  131.         /// <param name="domainObj">Green.AgileMapper.Test.StudenDo collection</param>   
  132.         /// <param name="isDeleteNotInFromItem">Delete the item that not in From collection</param>             
  133.         public static void Warp(this IList<Green.AgileMapper.Test.StudenDto> fromObj, IList<Green.AgileMapper.Test.StudenDo> domainObj, bool isDeleteNotInFromItem)   
  134.         {   
  135.             fromObj.Warp(domainObj, (fromObjItem, domainObjItem) =&gt; fromObjItem.ID == domainObjItem.ID && domainObjItem.ID != 0, isDeleteNotInFromItem);   
  136.         }   
  137.  
  138.         /// <summary>   
  139.         /// Green.AgileMapper.Test.StudenDto collection Warp domainObj Green.AgileMapper.Test.StudenDo collection;   
  140.         /// </summary>   
  141.         /// <param name="fromObj">Green.AgileMapper.Test.StudenDto collection</param>   
  142.         /// <param name="domainObj">Green.AgileMapper.Test.StudenDo collection</param>   
  143.         /// <param name="equalPredicate">the from item equal to item expression</param>             
  144.         public static void Warp(this IList<Green.AgileMapper.Test.StudenDto> fromObj, IList<Green.AgileMapper.Test.StudenDo> domainObj, Func<Green.AgileMapper.Test.StudenDto, Green.AgileMapper.Test.StudenDo, bool> equalPredicate)   
  145.         {   
  146.             fromObj.Warp(domainObj, equalPredicate, false);   
  147.         }   
  148.  
  149.         /// <summary>   
  150.         /// Green.AgileMapper.Test.StudenDto collection Warp domainObj Green.AgileMapper.Test.StudenDo collection;   
  151.         /// </summary>   
  152.         /// <param name="fromObj">Green.AgileMapper.Test.StudenDto collection</param>   
  153.         /// <param name="domainObj">Green.AgileMapper.Test.StudenDo collection</param>   
  154.         /// <param name="equalPredicate">the from item equal to item expression</param>        
  155.         /// <param name="isDeleteNotInFromItem">Delete the item that not in From collection</param>       
  156.         public static void Warp(this IList<Green.AgileMapper.Test.StudenDto> fromObj, IList<Green.AgileMapper.Test.StudenDo> domainObj, Func<Green.AgileMapper.Test.StudenDto, Green.AgileMapper.Test.StudenDo, bool> equalPredicate, bool isDeleteNotInFromItem)   
  157.         {   
  158.             if (fromObj == null)   
  159.             {   
  160.                 return;   
  161.             }   
  162.  
  163.             if (domainObj == null)   
  164.             {   
  165.                 domainObj = new List<Green.AgileMapper.Test.StudenDo>();   
  166.             }   
  167.  
  168.             fromObj.ToList().ForEach(fromObjItem =&gt;   
  169.             {   
  170.                 Green.AgileMapper.Test.StudenDo toItem = default(Green.AgileMapper.Test.StudenDo);   
  171.                 if (equalPredicate != null)   
  172.                 {   
  173.                     toItem = domainObj.SingleOrDefault(domainObjItem =&gt; 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 =&gt; fromObj.FirstOrDefault(fromObjItem =&gt; equalPredicate(fromObjItem, domainObjItem)) == null)   
  186.                     .ToList().ForEach(t =&gt;   
  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.         /// <summary>   
  209.         /// Green.AgileMapper.Test.ContactWayDto  Warp fromObj Green.AgileMapper.Test.ContactWay;   
  210.         /// </summary>   
  211.         /// <param name="domainObj">Green.AgileMapper.Test.ContactWay</param>   
  212.         /// <returns>Green.AgileMapper.Test.ContactWayDto</returns>   
  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.         /// <summary>   
  223.         /// Green.AgileMapper.Test.ContactWayDto  Warp domainObj Green.AgileMapper.Test.ContactWay;   
  224.         /// </summary>   
  225.         /// <param name="domainObj">Green.AgileMapper.Test.ContactWay</param>   
  226.         /// <returns>fromObj</returns>   
  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.         /// <summary>   
  244.         /// Green.AgileMapper.Test.ContactWayDto collection Warp fromObj Green.AgileMapper.Test.ContactWay collection;   
  245.         /// </summary>   
  246.         /// <param name="form">Green.AgileMapper.Test.ContactWayDto collection</param>   
  247.         /// <param name="domainObj">Green.AgileMapper.Test.ContactWay collection</param>          
  248.         public static List<Green.AgileMapper.Test.ContactWayDto> Warp(this IList<Green.AgileMapper.Test.ContactWay> domainObj)   
  249.         {   
  250.             List<Green.AgileMapper.Test.ContactWayDto> froms = new List<Green.AgileMapper.Test.ContactWayDto>();   
  251.             domainObj.ToList().ForEach(t =&gt;   
  252.             {   
  253.                 froms.Add(Warp(t));   
  254.             });   
  255.             return froms;   
  256.         }   
  257.  
  258.         /// <summary>   
  259.         /// Green.AgileMapper.Test.ContactWayDto collection Warp domainObj Green.AgileMapper.Test.ContactWay collection;   
  260.         /// </summary>   
  261.         /// <param name="fromObj">Green.AgileMapper.Test.ContactWayDto collection</param>   
  262.         /// <param name="domainObj">Green.AgileMapper.Test.ContactWay collection</param>        
  263.         public static void Warp(this IList<Green.AgileMapper.Test.ContactWayDto> fromObj, IList<Green.AgileMapper.Test.ContactWay> domainObj)   
  264.         {   
  265.             fromObj.Warp(domainObj, (fromObjItem, domainObjItem) =&gt; fromObjItem.Equals(domainObjItem), false);   
  266.         }   
  267.  
  268.         /// <summary>   
  269.         /// Green.AgileMapper.Test.ContactWayDto collection Warp domainObj Green.AgileMapper.Test.ContactWay collection;   
  270.         /// </summary>   
  271.         /// <param name="fromObj">Green.AgileMapper.Test.ContactWayDto collection</param>   
  272.         /// <param name="domainObj">Green.AgileMapper.Test.ContactWay collection</param>   
  273.         /// <param name="isDeleteNotInFromItem">Delete the item that not in From collection</param>             
  274.         public static void Warp(this IList<Green.AgileMapper.Test.ContactWayDto> fromObj, IList<Green.AgileMapper.Test.ContactWay> domainObj, bool isDeleteNotInFromItem)   
  275.         {   
  276.             fromObj.Warp(domainObj, (fromObjItem, domainObjItem) =&gt; fromObjItem.Equals(domainObjItem), isDeleteNotInFromItem);   
  277.         }   
  278.  
  279.         /// <summary>   
  280.         /// Green.AgileMapper.Test.ContactWayDto collection Warp domainObj Green.AgileMapper.Test.ContactWay collection;   
  281.         /// </summary>   
  282.         /// <param name="fromObj">Green.AgileMapper.Test.ContactWayDto collection</param>   
  283.         /// <param name="domainObj">Green.AgileMapper.Test.ContactWay collection</param>   
  284.         /// <param name="equalPredicate">the from item equal to item expression</param>             
  285.         public static void Warp(this IList<Green.AgileMapper.Test.ContactWayDto> fromObj, IList<Green.AgileMapper.Test.ContactWay> domainObj, Func<Green.AgileMapper.Test.ContactWayDto, Green.AgileMapper.Test.ContactWay, bool> equalPredicate)   
  286.         {   
  287.             fromObj.Warp(domainObj, equalPredicate, false);   
  288.         }   
  289.  
  290.         /// <summary>   
  291.         /// Green.AgileMapper.Test.ContactWayDto collection Warp domainObj Green.AgileMapper.Test.ContactWay collection;   
  292.         /// </summary>   
  293.         /// <param name="fromObj">Green.AgileMapper.Test.ContactWayDto collection</param>   
  294.         /// <param name="domainObj">Green.AgileMapper.Test.ContactWay collection</param>   
  295.         /// <param name="equalPredicate">the from item equal to item expression</param>        
  296.         /// <param name="isDeleteNotInFromItem">Delete the item that not in From collection</param>       
  297.         public static void Warp(this IList<Green.AgileMapper.Test.ContactWayDto> fromObj, IList<Green.AgileMapper.Test.ContactWay> domainObj, Func<Green.AgileMapper.Test.ContactWayDto, Green.AgileMapper.Test.ContactWay, bool> equalPredicate, bool isDeleteNotInFromItem)   
  298.         {   
  299.             if (fromObj == null)   
  300.             {   
  301.                 return;   
  302.             }   
  303.  
  304.             if (domainObj == null)   
  305.             {   
  306.                 domainObj = new List<Green.AgileMapper.Test.ContactWay>();   
  307.             }   
  308.  
  309.             fromObj.ToList().ForEach(fromObjItem =&gt;   
  310.             {   
  311.                 Green.AgileMapper.Test.ContactWay toItem = default(Green.AgileMapper.Test.ContactWay);   
  312.                 if (equalPredicate != null)   
  313.                 {   
  314.                     toItem = domainObj.SingleOrDefault(domainObjItem =&gt; equalPredicate(fromObjItem, domainObjItem));   
  315.                 }   
  316.                 if (toItem == null)   
  317.                 {   
  318.                     toItem = new Green.AgileMapper.Test.ContactWay();   
  319.                     domainObj.Add(toItem);   
  320.                 }   
  321.                 Warp(fromObjItem, toItem);   
  322.             });   
  323.  
  324.             if (isDeleteNotInFromItem)   
  325.             {   
  326.                 domainObj.Where(domainObjItem =&gt; fromObj.FirstOrDefault(fromObjItem =&gt; equalPredicate(fromObjItem, domainObjItem)) == null)   
  327.                     .ToList().ForEach(t =&gt;   
  328.                     {   
  329.                         domainObj.Remove(t);   
  330.                     });   
  331.             }   
  332.         }   
  333.     }   
  334. }  
  335.  
  336.    
  337.  
  338. 3.cs:  
  339.  
  340. using System;   
  341. using System.Linq;   
  342. using System.Data;   
  343. using System.Collections.Generic;   
  344.  
  345. namespace Green.AgileMapper   
  346. {   
  347.     public static partial class AgileMapperMapping   
  348.     {   
  349.         /// <summary>   
  350.         /// Green.AgileMapper.Test.KeyValuePairDto  Warp fromObj Green.AgileMapper.Test.KeyValuePair;   
  351.         /// </summary>   
  352.         /// <param name="domainObj">Green.AgileMapper.Test.KeyValuePair</param>   
  353.         /// <returns>Green.AgileMapper.Test.KeyValuePairDto</returns>   
  354.         public static Green.AgileMapper.Test.KeyValuePairDto Warp(this Green.AgileMapper.Test.KeyValuePair domainObj)   
  355.         {   
  356.             var fromObj = new Green.AgileMapper.Test.KeyValuePairDto();   
  357.             fromObj.Key = domainObj.Key;   
  358.             fromObj.Value = domainObj.Value;   
  359.             return fromObj;   
  360.         }   
  361.  
  362.         /// <summary>   
  363.         /// Green.AgileMapper.Test.KeyValuePairDto  Warp domainObj Green.AgileMapper.Test.KeyValuePair;   
  364.         /// </summary>   
  365.         /// <param name="domainObj">Green.AgileMapper.Test.KeyValuePair</param>   
  366.         /// <returns>fromObj</returns>   
  367.         public static void Warp(this Green.AgileMapper.Test.KeyValuePairDto fromObj, Green.AgileMapper.Test.KeyValuePair domainObj)   
  368.         {   
  369.             if (fromObj == null)   
  370.             {   
  371.                 return;   
  372.             }   
  373.             if (domainObj == null)   
  374.             {   
  375.                 domainObj = new Green.AgileMapper.Test.KeyValuePair();   
  376.             }   
  377.             domainObj.Key = fromObj.Key;   
  378.             domainObj.Value = fromObj.Value;   
  379.  
  380.         }   
  381.  
  382.         /// <summary>   
  383.         /// Green.AgileMapper.Test.KeyValuePairDto collection Warp fromObj Green.AgileMapper.Test.KeyValuePair collection;   
  384.         /// </summary>   
  385.         /// <param name="form">Green.AgileMapper.Test.KeyValuePairDto collection</param>   
  386.         /// <param name="domainObj">Green.AgileMapper.Test.KeyValuePair collection</param>          
  387.         public static List<Green.AgileMapper.Test.KeyValuePairDto> Warp(this IList<Green.AgileMapper.Test.KeyValuePair> domainObj)   
  388.         {   
  389.             List<Green.AgileMapper.Test.KeyValuePairDto> froms = new List<Green.AgileMapper.Test.KeyValuePairDto>();   
  390.             domainObj.ToList().ForEach(t =&gt;   
  391.             {   
  392.                 froms.Add(Warp(t));   
  393.             });   
  394.             return froms;   
  395.         }   
  396.  
  397.         /// <summary>   
  398.         /// Green.AgileMapper.Test.KeyValuePairDto collection Warp domainObj Green.AgileMapper.Test.KeyValuePair collection;   
  399.         /// </summary>   
  400.         /// <param name="fromObj">Green.AgileMapper.Test.KeyValuePairDto collection</param>   
  401.         /// <param name="domainObj">Green.AgileMapper.Test.KeyValuePair collection</param>        
  402.         public static void Warp(this IList<Green.AgileMapper.Test.KeyValuePairDto> fromObj, IList<Green.AgileMapper.Test.KeyValuePair> domainObj)   
  403.         {   
  404.             fromObj.Warp(domainObj, (fromObjItem, domainObjItem) =&gt; fromObjItem.Equals(domainObjItem), false);   
  405.         }   
  406.  
  407.         /// <summary>   
  408.         /// Green.AgileMapper.Test.KeyValuePairDto collection Warp domainObj Green.AgileMapper.Test.KeyValuePair collection;   
  409.         /// </summary>   
  410.         /// <param name="fromObj">Green.AgileMapper.Test.KeyValuePairDto collection</param>   
  411.         /// <param name="domainObj">Green.AgileMapper.Test.KeyValuePair collection</param>   
  412.         /// <param name="isDeleteNotInFromItem">Delete the item that not in From collection</param>             
  413.         public static void Warp(this IList<Green.AgileMapper.Test.KeyValuePairDto> fromObj, IList<Green.AgileMapper.Test.KeyValuePair> domainObj, bool isDeleteNotInFromItem)   
  414.         {   
  415.             fromObj.Warp(domainObj, (fromObjItem, domainObjItem) =&gt; fromObjItem.Equals(domainObjItem), isDeleteNotInFromItem);   
  416.         }   
  417.  
  418.         /// <summary>   
  419.         /// Green.AgileMapper.Test.KeyValuePairDto collection Warp domainObj Green.AgileMapper.Test.KeyValuePair collection;   
  420.         /// </summary>   
  421.         /// <param name="fromObj">Green.AgileMapper.Test.KeyValuePairDto collection</param>   
  422.         /// <param name="domainObj">Green.AgileMapper.Test.KeyValuePair collection</param>   
  423.         /// <param name="equalPredicate">the from item equal to item expression</param>             
  424.         public static void Warp(this IList<Green.AgileMapper.Test.KeyValuePairDto> fromObj, IList<Green.AgileMapper.Test.KeyValuePair> domainObj, Func<Green.AgileMapper.Test.KeyValuePairDto, Green.AgileMapper.Test.KeyValuePair, bool> equalPredicate)   
  425.         {   
  426.             fromObj.Warp(domainObj, equalPredicate, false);   
  427.         }   
  428.  
  429.         /// <summary>   
  430.         /// Green.AgileMapper.Test.KeyValuePairDto collection Warp domainObj Green.AgileMapper.Test.KeyValuePair collection;   
  431.         /// </summary>   
  432.         /// <param name="fromObj">Green.AgileMapper.Test.KeyValuePairDto collection</param>   
  433.         /// <param name="domainObj">Green.AgileMapper.Test.KeyValuePair collection</param>   
  434.         /// <param name="equalPredicate">the from item equal to item expression</param>        
  435.         /// <param name="isDeleteNotInFromItem">Delete the item that not in From collection</param>       
  436.         public static void Warp(this IList<Green.AgileMapper.Test.KeyValuePairDto> fromObj, IList<Green.AgileMapper.Test.KeyValuePair> domainObj, Func<Green.AgileMapper.Test.KeyValuePairDto, Green.AgileMapper.Test.KeyValuePair, bool> equalPredicate, bool isDeleteNotInFromItem)   
  437.         {   
  438.             if (fromObj == null)   
  439.             {   
  440.                 return;   
  441.             }   
  442.  
  443.             if (domainObj == null)   
  444.             {   
  445.                 domainObj = new List<Green.AgileMapper.Test.KeyValuePair>();   
  446.             }   
  447.  
  448.             fromObj.ToList().ForEach(fromObjItem =&gt;   
  449.             {   
  450.                 Green.AgileMapper.Test.KeyValuePair toItem = default(Green.AgileMapper.Test.KeyValuePair);   
  451.                 if (equalPredicate != null)   
  452.                 {   
  453.                     toItem = domainObj.SingleOrDefault(domainObjItem =&gt; equalPredicate(fromObjItem, domainObjItem));   
  454.                 }   
  455.                 if (toItem == null)   
  456.                 {   
  457.                     toItem = new Green.AgileMapper.Test.KeyValuePair();   
  458.                     domainObj.Add(toItem);   
  459.                 }   
  460.                 Warp(fromObjItem, toItem);   
  461.             });   
  462.  
  463.             if (isDeleteNotInFromItem)   
  464.             {   
  465.                 domainObj.Where(domainObjItem =&gt; fromObj.FirstOrDefault(fromObjItem =&gt; equalPredicate(fromObjItem, domainObjItem)) == null)   
  466.                     .ToList().ForEach(t =&gt;   
  467.                     {   
  468.                         domainObj.Remove(t);   
  469.                     });   
  470.             }   
  471.         }   
  472.     }   
  473. }   
  474.  

  在这里的代码生产后很乱并未格式化处理,需要我们在VS中自动Ctrl+K+D格式,如果你希望生成时候就帮助格式化了的话,你可以参见工具CoolFormat源代码格式化,安装工具利用CMD命令批量格式化(可以参见百度百科:http://baike.baidu.com/view/4367725.htm)。在项目暂不会考虑这块,如果你有兴趣的可以先帮助完成,谢谢。

具体请参见项目,存在任何问题或者bug可以随时给我留言,谢谢。

推荐阅读:
  1. 一遍文章搞清楚VO、DTO、DO、PO的概念、区别
  2. entity、bo、vo、po、dto、pojo如何理解和区分?

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

项目 休闲 target

上一篇:Exchange Server 2019 部署文档(全网首发)

下一篇:Java描述设计模式(19):模板方法模式

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》