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

简介:

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

    在我们的领域驱动开发中,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篇)。

 image

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

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

复制代码
[TestMethod] 
       public  void ObjectPickUper_GenCode_Test() 
      { 
          ObjectPickUperManager.Instance.IsSOAObject =  false
           var str = ObjectPickUperManager.Instance.PickUp<StudenDo>( " DTO "); 
           var str1 = ObjectPickUperManager.Instance.PickUp<ContactWay>( " DTO "); 
           var str2 = ObjectPickUperManager.Instance.PickUp<KeyValuePair>( " DTO "); 

          Assert.IsTrue(! string.IsNullOrEmpty(str)); 

           // 验证编译是否正确 
          CompilerParameters option =  new CompilerParameters(); 
          option.GenerateExecutable =  false
          option.GenerateInMemory =  true
          option.IncludeDebugInformation =  false
          option.ReferencedAssemblies.Add( " System.dll "); 
          option.ReferencedAssemblies.Add( typeof(System.Linq.IQueryable).Assembly.Location); 
          option.ReferencedAssemblies.Add( typeof(StudenDo).Assembly.Location); 
          option.ReferencedAssemblies.Add( typeof(Green.AgileMapper.CollectionMappingAttribute).Assembly.Location); 

           var result = CodeDomProvider.CreateProvider( " c# ").CompileAssemblyFromSource(option, str, str1, str2); 
           var assembly = result.CompiledAssembly; 
          Assert.IsFalse(result.Errors.HasErrors,  " 编译错误 "); 
      }
复制代码

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

生成dto:

ExpandedBlockStart.gif
复制代码
namespace Green.AgileMapper.Test 

     using System; 
     using System.Collections.Generic; 
     using System.Linq; 
     using System.Text; 
    [System.SerializableAttribute()] 
     public  class StudenDoDTO : System.ComponentModel.INotifyPropertyChanged, System.ICloneable 
    { 
         private  int _ID; 
         private Green.AgileMapper.Test.Sex _Sex; 
         private System.Collections.Generic.List<System.String> _CourseIds; 
         private  string _AddressCountry; 
         private  string _AddressProvince; 
         private  string _AddressStreet; 
         private  string _AddressParticular; 
         private Green.AgileMapper.Test.ContactWayDTO _ContactWay; 
         private System.Collections.Generic.List<Green.AgileMapper.Test.KeyValuePairDTO> _Propertys; 
         public  int ID 
        { 
             get 
            { 
                 return  this._ID; 
            } 
             set 
            { 
                 if (( this._ID != value)) 
                { 
                     this._ID = value; 
                     this.OnNotifyPropertyChanged( " ID "); 
                } 
            } 
        } 
         public Green.AgileMapper.Test.Sex Sex 
        { 
             get 
            { 
                 return  this._Sex; 
            } 
             set 
            { 
                 if (( this._Sex != value)) 
                { 
                     this._Sex = value; 
                     this.OnNotifyPropertyChanged( " Sex "); 
                } 
            } 
        } 
        [Green.AgileMapper.CollectionMappingAttribute(Name= " CourseIds ", IsConvertTo= true, IsConvertFrom= true, IsDeleteNotInFromItem= true)] 
         public System.Collections.Generic.List<System.String> CourseIds 
        { 
             get 
            { 
                 return  this._CourseIds; 
            } 
             set 
            { 
                 if (( this._CourseIds != value)) 
                { 
                     this._CourseIds = value; 
                     this.OnNotifyPropertyChanged( " CourseIds "); 
                } 
            } 
        } 
        [Green.AgileMapper.MappingAttribute(Name= " Address.Country ", IsConvertTo= true, IsConvertFrom= true)] 
         public  string AddressCountry 
        { 
             get 
            { 
                 return  this._AddressCountry; 
            } 
             set 
            { 
                 if (( this._AddressCountry != value)) 
                { 
                     this._AddressCountry = value; 
                     this.OnNotifyPropertyChanged( " AddressCountry "); 
                } 
            } 
        } 
        [Green.AgileMapper.MappingAttribute(Name= " Address.Province ", IsConvertTo= true, IsConvertFrom= true)] 
         public  string AddressProvince 
        { 
             get 
            { 
                 return  this._AddressProvince; 
            } 
             set 
            { 
                 if (( this._AddressProvince != value)) 
                { 
                     this._AddressProvince = value; 
                     this.OnNotifyPropertyChanged( " AddressProvince "); 
                } 
            } 
        } 
        [Green.AgileMapper.MappingAttribute(Name= " Address.Street ", IsConvertTo= true, IsConvertFrom= true)] 
         public  string AddressStreet 
        { 
             get 
            { 
                 return  this._AddressStreet; 
            } 
             set 
            { 
                 if (( this._AddressStreet != value)) 
                { 
                     this._AddressStreet = value; 
                     this.OnNotifyPropertyChanged( " AddressStreet "); 
                } 
            } 
        } 
        [Green.AgileMapper.MappingAttribute(Name= " Address.Particular ", IsConvertTo= true, IsConvertFrom= true)] 
         public  string AddressParticular 
        { 
             get 
            { 
                 return  this._AddressParticular; 
            } 
             set 
            { 
                 if (( this._AddressParticular != value)) 
                { 
                     this._AddressParticular = value; 
                     this.OnNotifyPropertyChanged( " AddressParticular "); 
                } 
            } 
        } 
        [Green.AgileMapper.ObjectMappingAttribute(Name= " ContactWay ")] 
         public Green.AgileMapper.Test.ContactWayDTO ContactWay 
        { 
             get 
            { 
                 return  this._ContactWay; 
            } 
             set 
            { 
                 if (( this._ContactWay != value)) 
                { 
                     this._ContactWay = value; 
                     this.OnNotifyPropertyChanged( " ContactWay "); 
                } 
            } 
        } 
        [Green.AgileMapper.CollectionMappingAttribute(Name= " Propertys ", IsConvertTo= true, IsConvertFrom= true, IsDeleteNotInFromItem= true, EqualExpression= null)] 
         public System.Collections.Generic.List<Green.AgileMapper.Test.KeyValuePairDTO> Propertys 
        { 
             get 
            { 
                 return  this._Propertys; 
            } 
             set 
            { 
                 if (( this._Propertys != value)) 
                { 
                     this._Propertys = value; 
                     this.OnNotifyPropertyChanged( " Propertys "); 
                } 
            } 
        } 
         public  event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; 
         public  void OnNotifyPropertyChanged( string property) 
        { 
             if (( this.PropertyChanged !=  null)) 
            { 
                 this.PropertyChanged( thisnew System.ComponentModel.PropertyChangedEventArgs(property)); 
            } 
        } 
         public  object Clone() 
        { 
            StudenDoDTO cloneObj; 
            cloneObj =  new StudenDoDTO(); 
            cloneObj._ID =  this.ID; 
            cloneObj._Sex =  this.Sex; 
            System.Collections.Generic.List<System.String> CourseIdsList; 
            CourseIdsList =  new System.Collections.Generic.List<System.String>(); 
             if (( this.CourseIds !=  null)) 
            { 
                 int i; 
                 for (i =  0; (i <  this.CourseIds.Count); i = (i +  1)) 
                { 
                    CourseIdsList.Add( this.CourseIds[i]); 
                } 
            } 
            cloneObj._CourseIds = CourseIdsList; 
            cloneObj._AddressCountry =  this.AddressCountry; 
            cloneObj._AddressProvince =  this.AddressProvince; 
            cloneObj._AddressStreet =  this.AddressStreet; 
            cloneObj._AddressParticular =  this.AddressParticular; 
            cloneObj._ContactWay = ((Green.AgileMapper.Test.ContactWayDTO)( this.ContactWay.Clone())); 
            System.Collections.Generic.List<Green.AgileMapper.Test.KeyValuePairDTO> PropertysList; 
            PropertysList =  new System.Collections.Generic.List<Green.AgileMapper.Test.KeyValuePairDTO>(); 
             if (( this.Propertys !=  null)) 
            { 
                 int i; 
                 for (i =  0; (i <  this.Propertys.Count); i = (i +  1)) 
                { 
                    PropertysList.Add(((Green.AgileMapper.Test.KeyValuePairDTO)( this.Propertys[i].Clone()))); 
                } 
            } 
            cloneObj._Propertys = PropertysList; 
             return cloneObj; 
        } 
    } 
}
复制代码

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

其他相关博文:

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

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

3:代码生成技术-目录


作者:破  狼 
出处:http://www.cnblogs.com/whitewolf/ 
本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。该文章也同时发布在我的独立博客中-个人独立博客博客园--破狼51CTO--破狼。http://www.cnblogs.com/whitewolf/archive/2012/04/05/ObjectPickUper.html


相关文章
AcGiGeometry::text 自画实体输出文字实例
AcGiGeometry::text 自画实体输出文字实例
请不要再把String或Style直接传递给自定义的组件了!
经常在各大社群中看到关于“如何自定义一个组件”这样的问题讨论。在探讨的过程中,经常会涉及到“如何更好的设置自定义组件中的文本”的问题。那么这篇文章让我们一起来聊聊。
|
10月前
|
存储 JSON 数据可视化
3.Cesium中实体Entity创建(超详细)
本文中,我将介绍 Cesium 中创建实体的方法,并对其进行分类,帮助读者快速理解 Cesium 中实体的类别,创建代码以及具体效果;
739 0
|
10月前
|
移动开发 小程序 前端开发
html5自定义数据属性?rgba()和 opacity的透明效果有什么不同?数组的方法(一)
html5允许给元素指定非标准的属性,但要以`data-`开头,获取时通过元素的dataset属性来访问。如果做过小程序的同学会发现,小程序已经经常使用这个属性了。
|
12月前
JSONModel 设置所有属性为可选
JSONModel 设置所有属性为可选
38 0
|
Web App开发 前端开发 开发者
1 分钟认识新属性 accent-color —— 修改 input 默认控件颜色
1 分钟认识新属性 accent-color —— 修改 input 默认控件颜色
cesium根据id选中实体
cesium根据id选中实体
446 0
|
JavaScript
自定义v-color指令
自定义v-color指令
|
Web App开发
<span style="font-weight:normal;">其他请<a href="http://blog.csdn.net/odailidong/article/details/72943877">参考: App统计指标定义</a></span>
(一)流量统计   1. 独立用户数:在当前计算周期内,访问统计对象的不重复用户数。万瑞数据系统通过对用户所使用的浏览器赋予唯一标识来识别用户的身份。
784 0