Green.AgileMapper意在處理領(lǐng)域驅(qū)動開發(fā)中對象之間的Mapper(如果你還不了解Green.AgileMapper,從這里開始Green.AgileMapper開源項目的使用(1) 和Green.AgileMapper項目(2)-新增DO和DTO代碼生成,項目地址:CodePlex http://agilemapper.codeplex.com/),本項目在后期會針對領(lǐng)域建模提供設(shè)計時候支持,利用EF和NHibernate作為底層ORM框架產(chǎn)生自己的領(lǐng)域框架,在設(shè)計時才會采用這些組件。
我們提供的服務(wù)有:做網(wǎng)站、成都做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、北流ssl等。為成百上千企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的北流網(wǎng)站制作公司
在我們的領(lǐng)域驅(qū)動開發(fā)中,DomainObject(領(lǐng)域?qū)ο螅┦且粋€自然oo對象,存在許多現(xiàn)實世界的關(guān)系關(guān)聯(lián),在我們的View端一個View卻可能需要數(shù)據(jù)并不是所有關(guān)聯(lián)。經(jīng)常除非特殊的UI模式,我們ViewObject往往都會被弱化,而利用Data Transfer Object代替。我們的dto從do抽取大多時候都是將do 單一Object平面化,對于級聯(lián)刪除更新的集合抽取,非級聯(lián)集合放棄。Green.ObjectPickUper就是一種由do抽取dto的實現(xiàn)策略,產(chǎn)生符合Green.AgileMapper 映射規(guī)則的dto對象。對象抽取可能存在多樣性,這里只是實現(xiàn)了默認抽取規(guī)則,可能不能滿足你的需求,你不需要急,因為在這里采用了策略模式來滿足不同抽取算法的需求(ObjectPickUperBase),Green.ObjectPickUper并不依賴Green.AgileMapper你可以自由抽取。
在Green.ObjectPickUper中利用了CodeDom實現(xiàn)代碼生成,所以可以支持多語言(如果你還不了解CodeDom可以看這里代碼生成技術(shù)-目錄中CodeDom篇)。

我們看看單元測試看看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動態(tài)編譯,查看是否存在編譯錯誤。
生成dto:
- 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(this, new 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/
其他相關(guān)博文:
1:Green.AgileMapper開源項目的使用(1)
2:Green.AgileMapper項目(2)-新增DO和DTO代碼生成
3:代碼生成技術(shù)-目錄
文章題目:Green.AgileMapper新增-Green.ObjectPickUper(do到dto對象的默認抽取)
文章網(wǎng)址:http://chinadenli.net/article2/jggcoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、品牌網(wǎng)站建設(shè)、網(wǎng)站維護、Google、服務(wù)器托管、品牌網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)