Green.AgileMapper新增-Green.ObjectPickUper(do到dto对象的默认抽取)

     Green.AgileMapper意在处理领域驱动开发中对象之间的Mapper(如果你还不了解Green.AgileMapper,从这里开始Green.AgileMapper开源项目的使用(1) 和Green.AgileMapper项目(2)-新增DO和DTO代码生成,项目地址:CodePlex http://agilemapper.codeplex.com/),本项目在后期会针对领域建模提供设计时候支持,利用EF和NHibernate作为底层ORM框架产生自己的领域框架,在设计时才会采用这些组件。

我们提供的服务有:做网站、成都做网站、微信公众号开发、网站优化、网站认证、北流ssl等。为成百上千企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的北流网站制作公司

    在我们的领域驱动开发中,DomainObject(领域对象)是一个自然oo对象,存在许多现实世界的关系关联,在我们的View端一个View却可能需要数据并不是所有关联。经常除非特殊的UI模式,我们ViewObject往往都会被弱化,而利用Data Transfer Object代替。我们的dto从do抽取大多时候都是将do 单一Object平面化,对于级联删除更新的集合抽取,非级联集合放弃。Green.ObjectPickUper就是一种由do抽取dto的实现策略,产生符合Green.AgileMapper 映射规则的dto对象。对象抽取可能存在多样性,这里只是实现了默认抽取规则,可能不能满足你的需求,你不需要急,因为在这里采用了策略模式来满足不同抽取算法的需求(ObjectPickUperBase),Green.ObjectPickUper并不依赖Green.AgileMapper你可以自由抽取。

  在Green.ObjectPickUper中利用了CodeDom实现代码生成,所以可以支持多语言(如果你还不了解CodeDom可以看这里代码生成技术-目录中CodeDom篇)。

Green.AgileMapper新增-Green.ObjectPickUper(do到dto对象的默认抽取)

 

我们看看单元测试看看Green.ObjectPickUper的简洁书写:

测试do对象仍是原对象StudentDo,参考CodePlex http://agilemapper.codeplex.com/

  1. [TestMethod]   
  2.       public void ObjectPickUper_GenCode_Test()   
  3.       {   
  4.           ObjectPickUperManager.Instance.IsSOAObject = false;   
  5.           var str = ObjectPickUperManager.Instance.PickUp("DTO");   
  6.           var str1 = ObjectPickUperManager.Instance.PickUp("DTO");   
  7.           var str2 = ObjectPickUperManager.Instance.PickUp("DTO");   
  8.  
  9.           Assert.IsTrue(!string.IsNullOrEmpty(str));   
  10.  
  11.           //验证编译是否正确  
  12.           CompilerParameters option = new CompilerParameters();   
  13.           option.GenerateExecutable = false;   
  14.           option.GenerateInMemory = true;   
  15.           option.IncludeDebugInformation = false;   
  16.           option.ReferencedAssemblies.Add("System.dll");   
  17.           option.ReferencedAssemblies.Add(typeof(System.Linq.IQueryable).Assembly.Location);   
  18.           option.ReferencedAssemblies.Add(typeof(StudenDo).Assembly.Location);   
  19.           option.ReferencedAssemblies.Add(typeof(Green.AgileMapper.CollectionMappingAttribute).Assembly.Location);   
  20.  
  21.           var result = CodeDomProvider.CreateProvider("c#").CompileAssemblyFromSource(option, str, str1, str2);   
  22.           var assembly = result.CompiledAssembly;   
  23.           Assert.IsFalse(result.Errors.HasErrors, "编译错误");   
  24.       }  

这里采用CodeDom动态编译,查看是否存在编译错误。

生成dto:

  1. namespace Green.AgileMapper.Test   
  2. {   
  3.     using System;   
  4.     using System.Collections.Generic;   
  5.     using System.Linq;   
  6.     using System.Text;   
  7.     [System.SerializableAttribute()]   
  8.     public class StudenDoDTO : System.ComponentModel.INotifyPropertyChanged, System.ICloneable   
  9.     {   
  10.         private int _ID;   
  11.         private Green.AgileMapper.Test.Sex _Sex;   
  12.         private System.Collections.Generic.List _CourseIds;   
  13.         private string _AddressCountry;   
  14.         private string _AddressProvince;   
  15.         private string _AddressStreet;   
  16.         private string _AddressParticular;   
  17.         private Green.AgileMapper.Test.ContactWayDTO _ContactWay;   
  18.         private System.Collections.Generic.List _Propertys;   
  19.         public int ID   
  20.         {   
  21.             get   
  22.             {   
  23.                 return this._ID;   
  24.             }   
  25.             set   
  26.             {   
  27.                 if ((this._ID != value))   
  28.                 {   
  29.                     this._ID = value;   
  30.                     this.OnNotifyPropertyChanged("ID");   
  31.                 }   
  32.             }   
  33.         }   
  34.         public Green.AgileMapper.Test.Sex Sex   
  35.         {   
  36.             get   
  37.             {   
  38.                 return this._Sex;   
  39.             }   
  40.             set   
  41.             {   
  42.                 if ((this._Sex != value))   
  43.                 {   
  44.                     this._Sex = value;   
  45.                     this.OnNotifyPropertyChanged("Sex");   
  46.                 }   
  47.             }   
  48.         }   
  49.         [Green.AgileMapper.CollectionMappingAttribute(Name="CourseIds", IsConvertTo=true, IsConvertFrom=true, IsDeleteNotInFromItem=true)]   
  50.         public System.Collections.Generic.List CourseIds   
  51.         {   
  52.             get   
  53.             {   
  54.                 return this._CourseIds;   
  55.             }   
  56.             set   
  57.             {   
  58.                 if ((this._CourseIds != value))   
  59.                 {   
  60.                     this._CourseIds = value;   
  61.                     this.OnNotifyPropertyChanged("CourseIds");   
  62.                 }   
  63.             }   
  64.         }   
  65.         [Green.AgileMapper.MappingAttribute(Name="Address.Country", IsConvertTo=true, IsConvertFrom=true)]   
  66.         public string AddressCountry   
  67.         {   
  68.             get   
  69.             {   
  70.                 return this._AddressCountry;   
  71.             }   
  72.             set   
  73.             {   
  74.                 if ((this._AddressCountry != value))   
  75.                 {   
  76.                     this._AddressCountry = value;   
  77.                     this.OnNotifyPropertyChanged("AddressCountry");   
  78.                 }   
  79.             }   
  80.         }   
  81.         [Green.AgileMapper.MappingAttribute(Name="Address.Province", IsConvertTo=true, IsConvertFrom=true)]   
  82.         public string AddressProvince   
  83.         {   
  84.             get   
  85.             {   
  86.                 return this._AddressProvince;   
  87.             }   
  88.             set   
  89.             {   
  90.                 if ((this._AddressProvince != value))   
  91.                 {   
  92.                     this._AddressProvince = value;   
  93.                     this.OnNotifyPropertyChanged("AddressProvince");   
  94.                 }   
  95.             }   
  96.         }   
  97.         [Green.AgileMapper.MappingAttribute(Name="Address.Street", IsConvertTo=true, IsConvertFrom=true)]   
  98.         public string AddressStreet   
  99.         {   
  100.             get   
  101.             {   
  102.                 return this._AddressStreet;   
  103.             }   
  104.             set   
  105.             {   
  106.                 if ((this._AddressStreet != value))   
  107.                 {   
  108.                     this._AddressStreet = value;   
  109.                     this.OnNotifyPropertyChanged("AddressStreet");   
  110.                 }   
  111.             }   
  112.         }   
  113.         [Green.AgileMapper.MappingAttribute(Name="Address.Particular", IsConvertTo=true, IsConvertFrom=true)]   
  114.         public string AddressParticular   
  115.         {   
  116.             get   
  117.             {   
  118.                 return this._AddressParticular;   
  119.             }   
  120.             set   
  121.             {   
  122.                 if ((this._AddressParticular != value))   
  123.                 {   
  124.                     this._AddressParticular = value;   
  125.                     this.OnNotifyPropertyChanged("AddressParticular");   
  126.                 }   
  127.             }   
  128.         }   
  129.         [Green.AgileMapper.ObjectMappingAttribute(Name="ContactWay")]   
  130.         public Green.AgileMapper.Test.ContactWayDTO ContactWay   
  131.         {   
  132.             get   
  133.             {   
  134.                 return this._ContactWay;   
  135.             }   
  136.             set   
  137.             {   
  138.                 if ((this._ContactWay != value))   
  139.                 {   
  140.                     this._ContactWay = value;   
  141.                     this.OnNotifyPropertyChanged("ContactWay");   
  142.                 }   
  143.             }   
  144.         }   
  145.         [Green.AgileMapper.CollectionMappingAttribute(Name="Propertys", IsConvertTo=true, IsConvertFrom=true, IsDeleteNotInFromItem=true, EqualExpression=null)]   
  146.         public System.Collections.Generic.List Propertys   
  147.         {   
  148.             get   
  149.             {   
  150.                 return this._Propertys;   
  151.             }   
  152.             set   
  153.             {   
  154.                 if ((this._Propertys != value))   
  155.                 {   
  156.                     this._Propertys = value;   
  157.                     this.OnNotifyPropertyChanged("Propertys");   
  158.                 }   
  159.             }   
  160.         }   
  161.         public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;   
  162.         public void OnNotifyPropertyChanged(string property)   
  163.         {   
  164.             if ((this.PropertyChanged != null))   
  165.             {   
  166.                 this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(property));   
  167.             }   
  168.         }   
  169.         public object Clone()   
  170.         {   
  171.             StudenDoDTO cloneObj;   
  172.             cloneObj = new StudenDoDTO();   
  173.             cloneObj._ID = this.ID;   
  174.             cloneObj._Sex = this.Sex;   
  175.             System.Collections.Generic.List CourseIdsList;   
  176.             CourseIdsList = new System.Collections.Generic.List();   
  177.             if ((this.CourseIds != null))   
  178.             {   
  179.                 int i;   
  180.                 for (i = 0; (i < this.CourseIds.Count); i = (i + 1))   
  181.                 {   
  182.                     CourseIdsList.Add(this.CourseIds[i]);   
  183.                 }   
  184.             }   
  185.             cloneObj._CourseIds = CourseIdsList;   
  186.             cloneObj._AddressCountry = this.AddressCountry;   
  187.             cloneObj._AddressProvince = this.AddressProvince;   
  188.             cloneObj._AddressStreet = this.AddressStreet;   
  189.             cloneObj._AddressParticular = this.AddressParticular;   
  190.             cloneObj._ContactWay = ((Green.AgileMapper.Test.ContactWayDTO)(this.ContactWay.Clone()));   
  191.             System.Collections.Generic.List<Green.AgileMapper.Test.KeyValuePairDTO> PropertysList;   
  192.             PropertysList = new System.Collections.Generic.List();   
  193.             if ((this.Propertys != null))   
  194.             {   
  195.                 int i;   
  196.                 for (i = 0; (i < this.Propertys.Count); i = (i + 1))   
  197.                 {   
  198.                     PropertysList.Add(((Green.AgileMapper.Test.KeyValuePairDTO)(this.Propertys[i].Clone())));   
  199.                 }   
  200.             }   
  201.             cloneObj._Propertys = PropertysList;   
  202.             return cloneObj;   
  203.         }   
  204.     }   
  205. }  
  206.  

源代码参加:CodePlex http://agilemapper.codeplex.com/

其他相关博文:

1:Green.AgileMapper开源项目的使用(1)

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

3:代码生成技术-目录


网站名称:Green.AgileMapper新增-Green.ObjectPickUper(do到dto对象的默认抽取)
URL分享:http://pwwzsj.com/article/jggcoc.html