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

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

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

image

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

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

ExpandedBlockStart.gif View Code
StudenDo stu =  new StudenDo() 
          { 
              ID =  1
              Name =  " test1 "
              Sex = Sex.女, 
              Address =  new Address() 
              { 
                  Country =  " 中国 "
                  Province =  " 四川 "
                  Street =  " 高新区 " 
              }, 
              CourseIds =  new List< string>() {  " 1 "" 2 "" 3 " }, 
              Propertys =  new List<KeyValuePair>() {  new KeyValuePair() { Key =  " 1 ", Value =  " 1 " } }, 
              ContactWay =  new ContactWay() 
              { 
                  Phone =  " 1111111111111111 "
                  Email =  " xxxx@12f "
                  QQ =  " 7889789999889 " 
              } 
          }; 
          Func<StudenDo, StudenDo,  bool> fun = (f, j) => f.ID == j.ID; 
           var s = fun.ToString(); 

           var mapper = ObjectMapperManager.Default.GetMapper<StudenDto, StudenDo>(); 
          mapper.AgileMapperTemplateStrategy.DefaultEqualExpression =  " {0}.ID == {1}.ID && {1}.ID != 0 ";                  
           var str1 = mapper.CodeGenerator(); 
          System.IO.File.Delete( @" E:\Project\OpenSource\AgileMapper\AgileMappper.Test\CodeTemplate.Test\1.cs "); 
          System.IO.File.AppendAllText( @" E:\Project\OpenSource\AgileMapper\AgileMappper.Test\CodeTemplate.Test\1.cs ", str1); 

           var mapper1 = ObjectMapperManager.Default.GetMapper<ContactWayDto, ContactWay>();                    
          str1 = mapper1.CodeGenerator(); 
          System.IO.File.Delete( @" E:\Project\OpenSource\AgileMapper\AgileMappper.Test\CodeTemplate.Test\2.cs "); 
          System.IO.File.AppendAllText( @" E:\Project\OpenSource\AgileMapper\AgileMappper.Test\CodeTemplate.Test\2.cs ", str1); 

           var mapper2 = ObjectMapperManager.Default.GetMapper<KeyValuePairDto, KeyValuePair>();            
          str1 = mapper2.CodeGenerator(); 
          System.IO.File.Delete( @" E:\Project\OpenSource\AgileMapper\AgileMappper.Test\CodeTemplate.Test\3.cs "); 
          System.IO.File.AppendAllText( @" E:\Project\OpenSource\AgileMapper\AgileMappper.Test\CodeTemplate.Test\3.cs ", str1);

最后的生成文件: 

1.cs:

ExpandedBlockStart.gif View Code
 using System;
using System.Linq;
using System.Data;
using System.Collections.Generic;

namespace Green.AgileMapper
{
     public  static  partial  class AgileMapperMapping
    {
         ///   <summary>
        
///  Green.AgileMapper.Test.StudenDto  Warp fromObj Green.AgileMapper.Test.StudenDo;
        
///   </summary>
        
///   <param name="domainObj"> Green.AgileMapper.Test.StudenDo </param>
        
///   <returns> Green.AgileMapper.Test.StudenDto </returns>
         public  static Green.AgileMapper.Test.StudenDto Warp( this Green.AgileMapper.Test.StudenDo domainObj)
        {
             var fromObj =  new Green.AgileMapper.Test.StudenDto();
            fromObj.ID = domainObj.ID;
            fromObj.Name = domainObj.Name;
            fromObj.Sex = domainObj.Sex;
             if (domainObj.Address !=  null)
            {
                fromObj.Country = domainObj.Address.Country;
                fromObj.Province = domainObj.Address.Province;
            }
            fromObj.Particular = domainObj.Address.Country +  "  国籍  " + domainObj.Address.Province +  "  省  ";
            fromObj.FirstPropertyKey = domainObj.Propertys[ 0].Key;
             if (domainObj.ContactWay !=  null)
            {
                fromObj.ContactWay = domainObj.ContactWay.Warp();
            }
             if (domainObj.CourseIds !=  null)
            {
                fromObj.CourseIds =  new List<System.String>();
                 foreach ( var item_CourseIds  in domainObj.CourseIds)
                {
                    fromObj.CourseIds.Add(item_CourseIds);
                }
            }
             if (domainObj.Propertys !=  null)
            {
                fromObj.Propertys = domainObj.Propertys.Warp();
            }
             return fromObj;
        }

         ///   <summary>
        
///  Green.AgileMapper.Test.StudenDto  Warp domainObj Green.AgileMapper.Test.StudenDo;
        
///   </summary>
        
///   <param name="domainObj"> Green.AgileMapper.Test.StudenDo </param>
        
///   <returns> fromObj </returns>
         public  static  void Warp( this Green.AgileMapper.Test.StudenDto fromObj, Green.AgileMapper.Test.StudenDo domainObj)
        {
             if (fromObj ==  null)
            {
                 return;
            }
             if (domainObj ==  null)
            {
                domainObj =  new Green.AgileMapper.Test.StudenDo();
            }
            domainObj.ID = fromObj.ID;
            domainObj.Name = fromObj.Name;
            domainObj.Sex = fromObj.Sex;
             if (domainObj.Address ==  null)
            {
                domainObj.Address =  new Green.AgileMapper.Test.Address();
            }
            domainObj.Address.Country = fromObj.Country;
            domainObj.Address.Province = fromObj.Province;

             if (domainObj.ContactWay ==  null)
            {
                domainObj.ContactWay =  new Green.AgileMapper.Test.ContactWay();
            }
            fromObj.ContactWay.Warp(domainObj.ContactWay);
             if (fromObj.CourseIds !=  null)
            {
                 if (domainObj.CourseIds ==  null)
                {
                    domainObj.CourseIds =  new List<System.String>();
                }
                domainObj.CourseIds.Clear();
                 foreach ( var item_CourseIds  in fromObj.CourseIds)
                {
                    domainObj.CourseIds.Add(item_CourseIds);
                }
            }
             if (fromObj.Propertys !=  null)
            {
                 if (domainObj.Propertys ==  null)
                {
                    domainObj.Propertys =  new List<Green.AgileMapper.Test.KeyValuePair>();
                }
                fromObj.Propertys.Warp(domainObj.Propertys, (fromObjItem, domainObjItem) => fromObjItem.Key == domainObjItem.Key,  true);
            }

        }

         ///   <summary>
        
///  Green.AgileMapper.Test.StudenDto collection Warp fromObj Green.AgileMapper.Test.StudenDo collection;
        
///   </summary>
        
///   <param name="form"> Green.AgileMapper.Test.StudenDto collection </param>
        
///   <param name="domainObj"> Green.AgileMapper.Test.StudenDo collection </param>         
         public  static List<Green.AgileMapper.Test.StudenDto> Warp( this IList<Green.AgileMapper.Test.StudenDo> domainObj)
        {
            List<Green.AgileMapper.Test.StudenDto> froms =  new List<Green.AgileMapper.Test.StudenDto>();
            domainObj.ToList().ForEach(t =>
            {
                froms.Add(Warp(t));
            });
             return froms;
        }

         ///   <summary>
        
///  Green.AgileMapper.Test.StudenDto collection Warp domainObj Green.AgileMapper.Test.StudenDo collection;
        
///   </summary>
        
///   <param name="fromObj"> Green.AgileMapper.Test.StudenDto collection </param>
        
///   <param name="domainObj"> Green.AgileMapper.Test.StudenDo collection </param>       
         public  static  void Warp( this IList<Green.AgileMapper.Test.StudenDto> fromObj, IList<Green.AgileMapper.Test.StudenDo> domainObj)
        {
            fromObj.Warp(domainObj, (fromObjItem, domainObjItem) => fromObjItem.ID == domainObjItem.ID && domainObjItem.ID !=  0false);
        }

         ///   <summary>
        
///  Green.AgileMapper.Test.StudenDto collection Warp domainObj Green.AgileMapper.Test.StudenDo collection;
        
///   </summary>
        
///   <param name="fromObj"> Green.AgileMapper.Test.StudenDto collection </param>
        
///   <param name="domainObj"> Green.AgileMapper.Test.StudenDo collection </param>
        
///   <param name="isDeleteNotInFromItem"> Delete the item that not in From collection </param>            
         public  static  void Warp( this IList<Green.AgileMapper.Test.StudenDto> fromObj, IList<Green.AgileMapper.Test.StudenDo> domainObj,  bool isDeleteNotInFromItem)
        {
            fromObj.Warp(domainObj, (fromObjItem, domainObjItem) => fromObjItem.ID == domainObjItem.ID && domainObjItem.ID !=  0, isDeleteNotInFromItem);
        }

         ///   <summary>
        
///  Green.AgileMapper.Test.StudenDto collection Warp domainObj Green.AgileMapper.Test.StudenDo collection;
        
///   </summary>
        
///   <param name="fromObj"> Green.AgileMapper.Test.StudenDto collection </param>
        
///   <param name="domainObj"> Green.AgileMapper.Test.StudenDo collection </param>
        
///   <param name="equalPredicate"> the from item equal to item expression </param>            
         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)
        {
            fromObj.Warp(domainObj, equalPredicate,  false);
        }

         ///   <summary>
        
///  Green.AgileMapper.Test.StudenDto collection Warp domainObj Green.AgileMapper.Test.StudenDo collection;
        
///   </summary>
        
///   <param name="fromObj"> Green.AgileMapper.Test.StudenDto collection </param>
        
///   <param name="domainObj"> Green.AgileMapper.Test.StudenDo collection </param>  
        
///   <param name="equalPredicate"> the from item equal to item expression </param>       
        
///   <param name="isDeleteNotInFromItem"> Delete the item that not in From collection </param>      
         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)
        {
             if (fromObj ==  null)
            {
                 return;
            }

             if (domainObj ==  null)
            {
                domainObj =  new List<Green.AgileMapper.Test.StudenDo>();
            }

             // 如果需要删除不存在于 from的list集合,先删除后修改
             if (isDeleteNotInFromItem)
            {
                domainObj.Where(domainObjItem => fromObj.FirstOrDefault(fromObjItem => equalPredicate(fromObjItem, domainObjItem)) ==  null)
                    .ToList().ForEach(t =>
                    {
                        domainObj.Remove(t);
                    });
            }

            fromObj.ToList().ForEach(fromObjItem =>
            {
                Green.AgileMapper.Test.StudenDo toItem =  default(Green.AgileMapper.Test.StudenDo);
                 if (equalPredicate !=  null)
                {
                    toItem = domainObj.SingleOrDefault(domainObjItem => equalPredicate(fromObjItem, domainObjItem));
                }
                 if (toItem ==  null)
                {
                    toItem =  new Green.AgileMapper.Test.StudenDo();
                    domainObj.Add(toItem);
                }
                Warp(fromObjItem, toItem);
            });

        }
    }
}

2.cs 

ExpandedBlockStart.gif View Code
using System.Collections.Generic;
using System;
using System.Linq;
using System.Data;
using System.Collections.Generic;

namespace Green.AgileMapper
{
     public  static  partial  class AgileMapperMapping
    {
         ///   <summary>
        
///  Green.AgileMapper.Test.ContactWayDto  Warp fromObj Green.AgileMapper.Test.ContactWay;
        
///   </summary>
        
///   <param name="domainObj"> Green.AgileMapper.Test.ContactWay </param>
        
///   <returns> Green.AgileMapper.Test.ContactWayDto </returns>
         public  static Green.AgileMapper.Test.ContactWayDto Warp( this Green.AgileMapper.Test.ContactWay domainObj)
        {
             var fromObj =  new Green.AgileMapper.Test.ContactWayDto();
            fromObj.Phone = domainObj.Phone;
            fromObj.Email = domainObj.Email;
            fromObj.QQ = domainObj.QQ;
             return fromObj;
        }

         ///   <summary>
        
///  Green.AgileMapper.Test.ContactWayDto  Warp domainObj Green.AgileMapper.Test.ContactWay;
        
///   </summary>
        
///   <param name="domainObj"> Green.AgileMapper.Test.ContactWay </param>
        
///   <returns> fromObj </returns>
         public  static  void Warp( this Green.AgileMapper.Test.ContactWayDto fromObj, Green.AgileMapper.Test.ContactWay domainObj)
        {
             if (fromObj ==  null)
            {
                 return;
            }
             if (domainObj ==  null)
            {
                domainObj =  new Green.AgileMapper.Test.ContactWay();
            }
            domainObj.Phone = fromObj.Phone;
            domainObj.Email = fromObj.Email;
            domainObj.QQ = fromObj.QQ;

        }

         ///   <summary>
        
///  Green.AgileMapper.Test.ContactWayDto collection Warp fromObj Green.AgileMapper.Test.ContactWay collection;
        
///   </summary>
        
///   <param name="form"> Green.AgileMapper.Test.ContactWayDto collection </param>
        
///   <param name="domainObj"> Green.AgileMapper.Test.ContactWay collection </param>         
         public  static List<Green.AgileMapper.Test.ContactWayDto> Warp( this IList<Green.AgileMapper.Test.ContactWay> domainObj)
        {
            List<Green.AgileMapper.Test.ContactWayDto> froms =  new List<Green.AgileMapper.Test.ContactWayDto>();
            domainObj.ToList().ForEach(t =>
            {
                froms.Add(Warp(t));
            });
             return froms;
        }

         ///   <summary>
        
///  Green.AgileMapper.Test.ContactWayDto collection Warp domainObj Green.AgileMapper.Test.ContactWay collection;
        
///   </summary>
        
///   <param name="fromObj"> Green.AgileMapper.Test.ContactWayDto collection </param>
        
///   <param name="domainObj"> Green.AgileMapper.Test.ContactWay collection </param>       
         public  static  void Warp( this IList<Green.AgileMapper.Test.ContactWayDto> fromObj, IList<Green.AgileMapper.Test.ContactWay> domainObj)
        {
            fromObj.Warp(domainObj, (fromObjItem, domainObjItem) => fromObjItem.Equals(domainObjItem),  false);
        }

         ///   <summary>
        
///  Green.AgileMapper.Test.ContactWayDto collection Warp domainObj Green.AgileMapper.Test.ContactWay collection;
        
///   </summary>
        
///   <param name="fromObj"> Green.AgileMapper.Test.ContactWayDto collection </param>
        
///   <param name="domainObj"> Green.AgileMapper.Test.ContactWay collection </param>
        
///   <param name="isDeleteNotInFromItem"> Delete the item that not in From collection </param>            
         public  static  void Warp( this IList<Green.AgileMapper.Test.ContactWayDto> fromObj, IList<Green.AgileMapper.Test.ContactWay> domainObj,  bool isDeleteNotInFromItem)
        {
            fromObj.Warp(domainObj, (fromObjItem, domainObjItem) => fromObjItem.Equals(domainObjItem), isDeleteNotInFromItem);
        }

         ///   <summary>
        
///  Green.AgileMapper.Test.ContactWayDto collection Warp domainObj Green.AgileMapper.Test.ContactWay collection;
        
///   </summary>
        
///   <param name="fromObj"> Green.AgileMapper.Test.ContactWayDto collection </param>
        
///   <param name="domainObj"> Green.AgileMapper.Test.ContactWay collection </param>
        
///   <param name="equalPredicate"> the from item equal to item expression </param>            
         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)
        {
            fromObj.Warp(domainObj, equalPredicate,  false);
        }

         ///   <summary>
        
///  Green.AgileMapper.Test.ContactWayDto collection Warp domainObj Green.AgileMapper.Test.ContactWay collection;
        
///   </summary>
        
///   <param name="fromObj"> Green.AgileMapper.Test.ContactWayDto collection </param>
        
///   <param name="domainObj"> Green.AgileMapper.Test.ContactWay collection </param>  
        
///   <param name="equalPredicate"> the from item equal to item expression </param>       
        
///   <param name="isDeleteNotInFromItem"> Delete the item that not in From collection </param>      
         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)
        {
             if (fromObj ==  null)
            {
                 return;
            }

             if (domainObj ==  null)
            {
                domainObj =  new List<Green.AgileMapper.Test.ContactWay>();
            }

            
             if (isDeleteNotInFromItem)
            {
                domainObj.Where(domainObjItem => fromObj.FirstOrDefault(fromObjItem => equalPredicate(fromObjItem, domainObjItem)) ==  null)
                    .ToList().ForEach(t =>
                    {
                        domainObj.Remove(t);
                    });
            }

            fromObj.ToList().ForEach(fromObjItem =>
            {
                Green.AgileMapper.Test.ContactWay toItem =  default(Green.AgileMapper.Test.ContactWay);
                 if (equalPredicate !=  null)
                {
                    toItem = domainObj.SingleOrDefault(domainObjItem => equalPredicate(fromObjItem, domainObjItem));
                }
                 if (toItem ==  null)
                {
                    toItem =  new Green.AgileMapper.Test.ContactWay();
                    domainObj.Add(toItem);
                }
                Warp(fromObjItem, toItem);
            });

        }
    }
}

3.cs: 

ExpandedBlockStart.gif View Code
using System;
using System.Linq;
using System.Data;
using System.Collections.Generic;

namespace Green.AgileMapper
{
     public  static  partial  class AgileMapperMapping
    {
         ///   <summary>
        
///  Green.AgileMapper.Test.KeyValuePairDto  Warp fromObj Green.AgileMapper.Test.KeyValuePair;
        
///   </summary>
        
///   <param name="domainObj"> Green.AgileMapper.Test.KeyValuePair </param>
        
///   <returns> Green.AgileMapper.Test.KeyValuePairDto </returns>
         public  static Green.AgileMapper.Test.KeyValuePairDto Warp( this Green.AgileMapper.Test.KeyValuePair domainObj)
        {
             var fromObj =  new Green.AgileMapper.Test.KeyValuePairDto();
            fromObj.Key = domainObj.Key;
            fromObj.Value = domainObj.Value;
             return fromObj;
        }

         ///   <summary>
        
///  Green.AgileMapper.Test.KeyValuePairDto  Warp domainObj Green.AgileMapper.Test.KeyValuePair;
        
///   </summary>
        
///   <param name="domainObj"> Green.AgileMapper.Test.KeyValuePair </param>
        
///   <returns> fromObj </returns>
         public  static  void Warp( this Green.AgileMapper.Test.KeyValuePairDto fromObj, Green.AgileMapper.Test.KeyValuePair domainObj)
        {
             if (fromObj ==  null)
            {
                 return;
            }
             if (domainObj ==  null)
            {
                domainObj =  new Green.AgileMapper.Test.KeyValuePair();
            }
            domainObj.Key = fromObj.Key;
            domainObj.Value = fromObj.Value;

        }

         ///   <summary>
        
///  Green.AgileMapper.Test.KeyValuePairDto collection Warp fromObj Green.AgileMapper.Test.KeyValuePair collection;
        
///   </summary>
        
///   <param name="form"> Green.AgileMapper.Test.KeyValuePairDto collection </param>
        
///   <param name="domainObj"> Green.AgileMapper.Test.KeyValuePair collection </param>         
         public  static List<Green.AgileMapper.Test.KeyValuePairDto> Warp( this IList<Green.AgileMapper.Test.KeyValuePair> domainObj)
        {
            List<Green.AgileMapper.Test.KeyValuePairDto> froms =  new List<Green.AgileMapper.Test.KeyValuePairDto>();
            domainObj.ToList().ForEach(t =>
            {
                froms.Add(Warp(t));
            });
             return froms;
        }

         ///   <summary>
        
///  Green.AgileMapper.Test.KeyValuePairDto collection Warp domainObj Green.AgileMapper.Test.KeyValuePair collection;
        
///   </summary>
        
///   <param name="fromObj"> Green.AgileMapper.Test.KeyValuePairDto collection </param>
        
///   <param name="domainObj"> Green.AgileMapper.Test.KeyValuePair collection </param>       
         public  static  void Warp( this IList<Green.AgileMapper.Test.KeyValuePairDto> fromObj, IList<Green.AgileMapper.Test.KeyValuePair> domainObj)
        {
            fromObj.Warp(domainObj, (fromObjItem, domainObjItem) => fromObjItem.Equals(domainObjItem),  false);
        }

         ///   <summary>
        
///  Green.AgileMapper.Test.KeyValuePairDto collection Warp domainObj Green.AgileMapper.Test.KeyValuePair collection;
        
///   </summary>
        
///   <param name="fromObj"> Green.AgileMapper.Test.KeyValuePairDto collection </param>
        
///   <param name="domainObj"> Green.AgileMapper.Test.KeyValuePair collection </param>
        
///   <param name="isDeleteNotInFromItem"> Delete the item that not in From collection </param>            
         public  static  void Warp( this IList<Green.AgileMapper.Test.KeyValuePairDto> fromObj, IList<Green.AgileMapper.Test.KeyValuePair> domainObj,  bool isDeleteNotInFromItem)
        {
            fromObj.Warp(domainObj, (fromObjItem, domainObjItem) => fromObjItem.Equals(domainObjItem), isDeleteNotInFromItem);
        }

         ///   <summary>
        
///  Green.AgileMapper.Test.KeyValuePairDto collection Warp domainObj Green.AgileMapper.Test.KeyValuePair collection;
        
///   </summary>
        
///   <param name="fromObj"> Green.AgileMapper.Test.KeyValuePairDto collection </param>
        
///   <param name="domainObj"> Green.AgileMapper.Test.KeyValuePair collection </param>
        
///   <param name="equalPredicate"> the from item equal to item expression </param>            
         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)
        {
            fromObj.Warp(domainObj, equalPredicate,  false);
        }

         ///   <summary>
        
///  Green.AgileMapper.Test.KeyValuePairDto collection Warp domainObj Green.AgileMapper.Test.KeyValuePair collection;
        
///   </summary>
        
///   <param name="fromObj"> Green.AgileMapper.Test.KeyValuePairDto collection </param>
        
///   <param name="domainObj"> Green.AgileMapper.Test.KeyValuePair collection </param>  
        
///   <param name="equalPredicate"> the from item equal to item expression </param>       
        
///   <param name="isDeleteNotInFromItem"> Delete the item that not in From collection </param>      
         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)
        {
             if (fromObj ==  null)
            {
                 return;
            }

             if (domainObj ==  null)
            {
                domainObj =  new List<Green.AgileMapper.Test.KeyValuePair>();
            }


             if (isDeleteNotInFromItem)
            {
                domainObj.Where(domainObjItem => fromObj.FirstOrDefault(fromObjItem => equalPredicate(fromObjItem, domainObjItem)) ==  null)
                    .ToList().ForEach(t =>
                    {
                        domainObj.Remove(t);
                    });
            }

            fromObj.ToList().ForEach(fromObjItem =>
            {
                Green.AgileMapper.Test.KeyValuePair toItem =  default(Green.AgileMapper.Test.KeyValuePair);
                 if (equalPredicate !=  null)
                {
                    toItem = domainObj.SingleOrDefault(domainObjItem => equalPredicate(fromObjItem, domainObjItem));
                }
                 if (toItem ==  null)
                {
                    toItem =  new Green.AgileMapper.Test.KeyValuePair();
                    domainObj.Add(toItem);
                }
                Warp(fromObjItem, toItem);
            });

        }
    }
}

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

  具体请参见项目,存在任何问题或者bug可以随时给我留言,谢谢。项目地址CodePlex http://agilemapper.codeplex.com/

转载于:https://www.cnblogs.com/whitewolf/archive/2012/03/31/AgileMapper2.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值