如果你还不了解Green.AgileMapper的用意,作用请先一步到上篇Green.AgileMapper开源项目的使用,如果你觉得运行时(Runtime)的Mapper效率存在问题,在这个版本中有了更新,新增了C#直接代码的生成,这里的生成都已3.0后的扩展方法实现,你可以很方便的一句代码实现两者的转化。
目前创新互联公司已为上千的企业提供了网站建设、域名、网络空间、绵阳服务器托管、企业网站设计、赣州网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
代码生成我本想利用T4模板,但是由于我们的项目IDE版本是VS2008,对于T4的参数传递支持还不是很友好,你可能说用AppDomain.SetData,或者CallContext.LogicalSetData,但是可惜我们的饿MappingRule是不可序列化的,所以最后我只得采用了字符串拼接最笨的办法,为了应对生成策略不同,在这里我们加入了策略模式来应付,来看看代码结构吧:
在这里只支持对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
() { "1", "2", "3" }, - Propertys = new List
() { new KeyValuePair() { Key = "1", Value = "1" } }, - ContactWay = new ContactWay()
- {
- Phone = "1111111111111111",
- Email = "xxxx@12f",
- QQ = "7889789999889"
- }
- };
- Func
fun = (f, j) => f.ID == j.ID; - var s = fun.ToString();
- var mapper = ObjectMapperManager.Default.GetMapper
(); - 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
(); - 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
(); - 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
- {
- ///
- /// Green.AgileMapper.Test.StudenDto Warp fromObj Green.AgileMapper.Test.StudenDo;
- ///
- /// Green.AgileMapper.Test.StudenDo
- ///
Green.AgileMapper.Test.StudenDto - 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
(); - foreach (var item_CourseIds in domainObj.CourseIds)
- {
- fromObj.CourseIds.Add(item_CourseIds);
- }
- }
- if (domainObj.Propertys != null)
- {
- fromObj.Propertys = domainObj.Propertys.Warp();
- }
- return fromObj;
- }
- ///
- /// Green.AgileMapper.Test.StudenDto Warp domainObj Green.AgileMapper.Test.StudenDo;
- ///
- /// Green.AgileMapper.Test.StudenDo
- ///
fromObj - 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
(); - }
- 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
(); - }
- fromObj.Propertys.Warp(domainObj.Propertys, (fromObjItem, domainObjItem) => fromObjItem.Key == domainObjItem.Key, true);
- }
- }
- ///
- /// Green.AgileMapper.Test.StudenDto collection Warp fromObj Green.AgileMapper.Test.StudenDo collection;
- ///
- /// Green.AgileMapper.Test.StudenDto collection
- /// Green.AgileMapper.Test.StudenDo collection
- public static List
Warp(this IList domainObj) - {
- List
froms = new List (); - domainObj.ToList().ForEach(t =>
- {
- froms.Add(Warp(t));
- });
- return froms;
- }
- ///
- /// Green.AgileMapper.Test.StudenDto collection Warp domainObj Green.AgileMapper.Test.StudenDo collection;
- ///
- /// Green.AgileMapper.Test.StudenDto collection
- /// Green.AgileMapper.Test.StudenDo collection
- public static void Warp(this IList
fromObj, IList domainObj) - {
- fromObj.Warp(domainObj, (fromObjItem, domainObjItem) => fromObjItem.ID == domainObjItem.ID && domainObjItem.ID != 0, false);
- }
- ///
- /// Green.AgileMapper.Test.StudenDto collection Warp domainObj Green.AgileMapper.Test.StudenDo collection;
- ///
- /// Green.AgileMapper.Test.StudenDto collection
- /// Green.AgileMapper.Test.StudenDo collection
- /// Delete the item that not in From collection
- public static void Warp(this IList
fromObj, IList domainObj, bool isDeleteNotInFromItem) - {
- fromObj.Warp(domainObj, (fromObjItem, domainObjItem) => fromObjItem.ID == domainObjItem.ID && domainObjItem.ID != 0, isDeleteNotInFromItem);
- }
- ///
- /// Green.AgileMapper.Test.StudenDto collection Warp domainObj Green.AgileMapper.Test.StudenDo collection;
- ///
- /// Green.AgileMapper.Test.StudenDto collection
- /// Green.AgileMapper.Test.StudenDo collection
- /// the from item equal to item expression
- public static void Warp(this IList
fromObj, IList domainObj, Func equalPredicate) - {
- fromObj.Warp(domainObj, equalPredicate, false);
- }
- ///
- /// Green.AgileMapper.Test.StudenDto collection Warp domainObj Green.AgileMapper.Test.StudenDo collection;
- ///
- /// Green.AgileMapper.Test.StudenDto collection
- /// Green.AgileMapper.Test.StudenDo collection
- /// the from item equal to item expression
- /// Delete the item that not in From collection
- public static void Warp(this IList
fromObj, IList domainObj, Func equalPredicate, bool isDeleteNotInFromItem) - {
- if (fromObj == null)
- {
- return;
- }
- if (domainObj == null)
- {
- domainObj = new List
(); - }
- 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);
- });
- if (isDeleteNotInFromItem)
- {
- domainObj.Where(domainObjItem => fromObj.FirstOrDefault(fromObjItem => equalPredicate(fromObjItem, domainObjItem)) == null)
- .ToList().ForEach(t =>
- {
- 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
- {
- ///
- /// Green.AgileMapper.Test.ContactWayDto Warp fromObj Green.AgileMapper.Test.ContactWay;
- ///
- /// Green.AgileMapper.Test.ContactWay
- ///
Green.AgileMapper.Test.ContactWayDto - 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;
- }
- ///
- /// Green.AgileMapper.Test.ContactWayDto Warp domainObj Green.AgileMapper.Test.ContactWay;
- ///
- /// Green.AgileMapper.Test.ContactWay
- ///
fromObj - 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;
- }
- ///
- /// Green.AgileMapper.Test.ContactWayDto collection Warp fromObj Green.AgileMapper.Test.ContactWay collection;
- ///
- /// Green.AgileMapper.Test.ContactWayDto collection
- /// Green.AgileMapper.Test.ContactWay collection
- public static List
Warp(this IList domainObj) - {
- List
froms = new List (); - domainObj.ToList().ForEach(t =>
- {
- froms.Add(Warp(t));
- });
- return froms;
- }
- ///
- /// Green.AgileMapper.Test.ContactWayDto collection Warp domainObj Green.AgileMapper.Test.ContactWay collection;
- ///
- /// Green.AgileMapper.Test.ContactWayDto collection
- /// Green.AgileMapper.Test.ContactWay collection
- public static void Warp(this IList
fromObj, IList domainObj) - {
- fromObj.Warp(domainObj, (fromObjItem, domainObjItem) => fromObjItem.Equals(domainObjItem), false);
- }
- ///
- /// Green.AgileMapper.Test.ContactWayDto collection Warp domainObj Green.AgileMapper.Test.ContactWay collection;
- ///
- /// Green.AgileMapper.Test.ContactWayDto collection
- /// Green.AgileMapper.Test.ContactWay collection
- /// Delete the item that not in From collection
- public static void Warp(this IList
fromObj, IList domainObj, bool isDeleteNotInFromItem) - {
- fromObj.Warp(domainObj, (fromObjItem, domainObjItem) => fromObjItem.Equals(domainObjItem), isDeleteNotInFromItem);
- }
- ///
- /// Green.AgileMapper.Test.ContactWayDto collection Warp domainObj Green.AgileMapper.Test.ContactWay collection;
- ///
- /// Green.AgileMapper.Test.ContactWayDto collection
- /// Green.AgileMapper.Test.ContactWay collection
- /// the from item equal to item expression
- public static void Warp(this IList
fromObj, IList domainObj, Func equalPredicate) - {
- fromObj.Warp(domainObj, equalPredicate, false);
- }
- ///
- /// Green.AgileMapper.Test.ContactWayDto collection Warp domainObj Green.AgileMapper.Test.ContactWay collection;
- ///
- /// Green.AgileMapper.Test.ContactWayDto collection
- /// Green.AgileMapper.Test.ContactWay collection
- /// the from item equal to item expression
- /// Delete the item that not in From collection
- public static void Warp(this IList
fromObj, IList domainObj, Func equalPredicate, bool isDeleteNotInFromItem) - {
- if (fromObj == null)
- {
- return;
- }
- if (domainObj == null)
- {
- domainObj = new List
(); - }
- fromObj.ToList().ForEach(fromObjItem =>
- {
- Green.AgileMapper.Test.ContactWay toItem = default(Green.AgileMapper.Test.ContactWay);
- &nb
分享题目:Green.AgileMapper项目-新增DO和DTO代码生成
当前URL:http://www.cdkjz.cn/article/gpddsj.html