在我们的领域驱动开发中,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你可以自由抽取。
<a target="_blank" href="http://blog.51cto.com/attachment/201204/233012531.jpg"></a>
我们看看单元测试看看Green.ObjectPickUper的简洁书写:
[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:
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
return this._Sex;
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
return this._CourseIds;
if ((this._CourseIds != value))
this._CourseIds = value;
this.OnNotifyPropertyChanged("CourseIds");
[Green.AgileMapper.MappingAttribute(Name="Address.Country", IsConvertTo=true, IsConvertFrom=true)]
public string AddressCountry
return this._AddressCountry;
if ((this._AddressCountry != value))
this._AddressCountry = value;
this.OnNotifyPropertyChanged("AddressCountry");
[Green.AgileMapper.MappingAttribute(Name="Address.Province", IsConvertTo=true, IsConvertFrom=true)]
public string AddressProvince
return this._AddressProvince;
if ((this._AddressProvince != value))
this._AddressProvince = value;
this.OnNotifyPropertyChanged("AddressProvince");
[Green.AgileMapper.MappingAttribute(Name="Address.Street", IsConvertTo=true, IsConvertFrom=true)]
public string AddressStreet
return this._AddressStreet;
if ((this._AddressStreet != value))
this._AddressStreet = value;
this.OnNotifyPropertyChanged("AddressStreet");
[Green.AgileMapper.MappingAttribute(Name="Address.Particular", IsConvertTo=true, IsConvertFrom=true)]
public string AddressParticular
return this._AddressParticular;
if ((this._AddressParticular != value))
this._AddressParticular = value;
this.OnNotifyPropertyChanged("AddressParticular");
[Green.AgileMapper.ObjectMappingAttribute(Name="ContactWay")]
public Green.AgileMapper.Test.ContactWayDTO ContactWay
return this._ContactWay;
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
return this._Propertys;
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&lt;Green.AgileMapper.Test.KeyValuePairDTO> PropertysList;
PropertysList = new System.Collections.Generic.List<Green.AgileMapper.Test.KeyValuePairDTO>();
if ((this.Propertys != null))
for (i = 0; (i &lt; this.Propertys.Count); i = (i + 1))
PropertysList.Add(((Green.AgileMapper.Test.KeyValuePairDTO)(this.Propertys[i].Clone())));
cloneObj._Propertys = PropertysList;
return cloneObj;
}
}
其他相关博文:
本文转自 破狼 51CTO博客,原文链接:http://blog.51cto.com/whitewolfblog/827277,如需转载请自行联系原作者