Green.AgileMapper项目-新增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/代码):

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) =&gt; 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:

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) =&gt; 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 =&gt; 
            { 
                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) =&gt; fromObjItem.ID == domainObjItem.ID && domainObjItem.ID != 0, 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="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) =&gt; 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>(); 
            }

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

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

 

2.cs

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 =&gt; 
            { 
                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) =&gt; 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) =&gt; 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>(); 
            }

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

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

 

3.cs:

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 =&gt; 
            { 
                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) =&gt; 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) =&gt; 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>(); 
            }

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

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

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

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






 本文转自 破狼 51CTO博客,原文链接:http://blog.51cto.com/whitewolfblog/822652,如需转载请自行联系原作者


MyBatis-Plus 是一个 MyBatis 的增强工具,提供了许多方便使用的功能。其中之一就是 MyBatis-Plus Generator,它可以根据数据库表结构自动生成基础的 Mapper、Model、Service、Controller 层代码,省去了很多重复的工作。 不过,MyBatis-Plus Generator 生成的代码可能不太符合我们的需求,比如不支持生成 DTO、VO、Convertor 等类。这时候我们就需要自定义模板来生成这些类。下面我来介绍一下如何自定义模板生成 DTO、VO、Convertor。 1. 新建模板文件 在 MyBatis-Plus Generator 的资源目录下,找到模板文件所在的目录。一般是在 `templates/entity` 目录下。在这个目录下新建三个文件: - dto.java.vm - vo.java.vm - convertor.java.vm 这三个文件分别对应生成 DTO、VO、Convertor 的模板。 2. 编写模板文件 以 `dto.java.vm` 为例,下面是一个简单的模板代码: ``` package ${package}.dto; import lombok.Data; @Data public class ${className}Dto { #foreach($column in $table.columns) /** * ${column.columnComment} */ private ${column.javaType} ${column.propertyName}; #end } ``` 这个模板会生成一个名为 `${className}Dto` 的 DTO 类,其中 `${className}` 是表对应的类名。模板通过 `#foreach` 循环遍历表的所有列,生成对应的属性。 `vo.java.vm` 和 `convertor.java.vm` 的模板代码类似,这里就不再赘述了。 3. 修改配置文件 在 MyBatis-Plus Generator 的配置文件 `generatorConfig.xml` 中,找到 `table` 标签下的 `property` 标签。在这个标签下添加三个属性: ``` <property name="dtoTargetPackage" value="com.example.dto"/> <property name="voTargetPackage" value="com.example.vo"/> <property name="convertorTargetPackage" value="com.example.convertor"/> ``` 这三个属性分别指定生成的 DTO、VO、Convertor 类所在的包名。 接着,在 `table` 标签下的 `generatedKey` 标签后面添加以下代码: ``` <property name="dtoTemplate" value="templates/entity/dto.java.vm"/> <property name="voTemplate" value="templates/entity/vo.java.vm"/> <property name="convertorTemplate" value="templates/entity/convertor.java.vm"/> ``` 这些代码分别指定生成 DTO、VO、Convertor 的模板文件路径。 4. 运行 MyBatis-Plus Generator 最后,运行 MyBatis-Plus Generator,它会自动根据模板文件生成 DTO、VO、Convertor 等类。生成的类会放在指定的包中,可以随时使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值