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

摘要:
如果你不明白格林的目的。AgileMapper,请转到之前的绿色。对于AgileMaper开源项目的使用,如果您认为运行时Mapper的效率存在问题,则此版本已更新,并添加了C#直接代码的生成。此处的生成已在3.0后扩展方法中实现。你可以在一句代码中轻松地转换两者。以及多次重新加载以前版本的列表转换,主要是为了满足DTo到DO对象的特殊要求,因为当我们在域中开发和存储DTo时,我们需要首先取出DO对象,并根据DTo在DO对象的基础上对其进行修改,以便ORM域框架或UOW可以跟踪记录。

     如果你还不了解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/代码):

Green.AgileMapper项目(2)新增DO和DTO代码生成第2张Green.AgileMapper项目(2)新增DO和DTO代码生成第3张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:

Green.AgileMapper项目(2)新增DO和DTO代码生成第4张Green.AgileMapper项目(2)新增DO和DTO代码生成第5张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 

Green.AgileMapper项目(2)新增DO和DTO代码生成第6张Green.AgileMapper项目(2)新增DO和DTO代码生成第7张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: 

Green.AgileMapper项目(2)新增DO和DTO代码生成第8张Green.AgileMapper项目(2)新增DO和DTO代码生成第9张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/

免责声明:文章转载自《Green.AgileMapper项目(2)新增DO和DTO代码生成》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ORACLE中查询第n条到第m条的数据记录的方法Mahout学习系列之推荐算法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

java术语(PO/POJO/VO/BO/DAO/DTO)

PO(persistant object) 持久对象 在o/r 映射的时候出现的概念,如果没有o/r映射,就没有这个概念存在了.通常对应数据模型(数据库),本身还有部分业务逻辑的处理.可以看成是与数据库中的表相映射的java对象.最简单的PO就是对应数据库中某个表中的一条记录,多个记录可以用PO的集合.PO中应该不包含任何对数据库的操作.VO(value...

分布式中的DTO(转)

简介 数据传输对象(DTO),是一种设计模式之间传输数据的软件应用系统。数据传输目标往往是结合使用的数据访问对象从数据库中检索数据。   数据传输对象与数据交互对象或数据访问对象之间的差异是一个以不具有任何行为除了存储和检索的数据(访问和存取器)。   在传统的系统(企业JavaBeans)体系结构,数据传输目标服务的双重目的:第一,他们围 绕这个问题,p...

java中的vo 、dto 、dao--转

 java中的vo 、dto 、dao--转 原文地址:http://yinchunjian.iteye.com/blog/758196 O是跟数据库里表的映射,一个表对应一个VO DAO是用VO来访问真实的表,对数据库的操作都在DAO中完成 BO是业务层,做逻辑处理的 VO , PO , BO , QO, DAO ,POJO, O/R Mappin...

springboot2.1.3 + redisTemplate + Lock 操作 redis 3.0.5

近期在整合springboot + redis 的功能,本来想用原生的jedit api,最后想想有点 low,搜了一把,boot已经提供给我们操作的方法,那就是 使用 redisTemplate 或 StringRedisTemplate, 两者是有区别的,可以看下面的说明 1. 两者的关系是StringRedisTemplate继承RedisTempl...

DTO

DTO就是数据传输对象(Data Transfer Object)的缩写。 DTO模式或称VO模式,是指将数据封装成普通的JavaBeans,在J2EE多个层次之间传输。 DTO类似信使,是同步系统中的Message。 该JavaBeans可以是一个数据模型Model。 常见的方式就是为实体对象建立一个Model类,该类只包含这个对象所有...

MyBatis学习4---使用MyBatis_Generator生成Dto、Dao、Mapping

由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类、Dao接口类甚至Mapping映射文件。 一、建立表结构 CREATE TABLE `user` (`id` varchar(50) NOT NULL,`username` varch...