- 综述
EmitMapper映射效率比较高,接近硬编码。EmitMapper采用emit方式在运行时动态生成IL,而其他映射框架多是采用反射机制。此外EmitMapper最大限度地减少了拆箱装箱操作和映射过程中的额外的调用。
EmitMapper支持.net的所有平台:Framework 3.5、Microsoft Silverlight 3、Mono。
1 public class Sourse 2 { 3 public int A; 4 public decimal? B; 5 public string C; 6 public Inner D; 7 public string E; 8 } 9 10 public class Dest 11 { 12 public int? A; 13 public decimal B; 14 public DateTime C; 15 public Inner D; 16 public string F; 17 } 18 19 public class Inner 20 { 21 public long D1; 22 public Guid D2; 23 } 24 25 Sourse src = new Sourse 26 { 27 A = 1,28 B = 0M,29 C = "2011/9/21 0:00:00",30 D = new Inner 31 { 32 D2 = Guid.NewGuid() 33 },34 E = "test" 35 }; 36 37 ObjectsMapper<Sourse,Dest> mapper = 38 ObjectMapperManager.DefaultInstance.GetMapper<Sourse,Dest>(); 39 Dest dst = mapper.Map(src); 40 41 Assert.AreEqual<string>(dst.B.ToString(),src.B); 42 Assert.AreEqual<long>(dst.C.C1,0L); 43 Assert.AreEqual<Guid>(dst.C.C2,src.C.C2); 44 Assert.IsNull(dst.E);
任何类型到string类型使用ToString()方法;
可以使用System.Convert类转换的原始类型;
可空类型、枚举类型、各种集合类型、结构与类;
复杂的嵌套类型采用递归方式转换;
描述 |
|
ConvertUsing |
为指定的成员提供自定义的转换逻辑 |
ConvertGeneric |
为指定的泛型类型成员提供自定义的转换逻辑 |
ConstructBy |
|
NullSubstitution |
当源对象中指定的成员在为null时,给目标对象的成员赋值 |
IgnoreMembers |
忽略指定成员的映射 |
PostProcess |
|
ShallowMap |
指定的成员采用浅拷贝方式映射 |
DeepMap |
指定的成员采用深拷贝方式映射 |
MatchMembers |
如果成员名称的映射不采用精确匹配,可以指定具体的映射逻辑 |
选择几个方法简单示例如下:
1 public class Sourse 2 { 3 public int A; 4 public decimal? B; 5 public string C; 6 public Inner D; 7 public string E; 8 } 9 10 public class Dest 11 { 12 public int? A; 13 public decimal B; 14 public DateTime C; 15 public Inner2 D; 16 public string F; 17 } 18 19 public class Inner 20 { 21 public long D1; 22 public Guid D2; 23 } 24 25 public class Inner2 26 { 27 public long D12; 28 public Guid D22; 29 } 30 31 ObjectsMapper<Sourse,Dest> mapper1 = 32 new ObjectMapperManager().GetMapper<Sourse,Dest>( 33 new DefaultMapConfig() 34 .IgnoreMembers<Sourse,Dest>(new string[] { "A" }) 35 .NullSubstitution<decimal?,decimal>((value) => -1M) 36 .ConvertUsing<Inner,Inner2>(value => new Inner2 { D12 = value.D1,D22 = value.D2 }) 37 .PostProcess<Dest>((value,state) => { value.F = "nothing"; return value; }) 38 ); 39 Dest dst = mapper1.Map(src); 40 41 Assert.IsNull(dst.A); 42 Assert.AreEqual<decimal>(dst.B,-1M); 43 Assert.AreEqual<Guid>(dst.D.D22,src.D.D2); 44 Assert.AreEqual<string>(dst.F,"nothing");
自定义的映射配置器可以继承自DefaultMapConfig或CustomMapConfig,利用基类的一些功能实现定制的映射,也可以继承自接口ImappingConfigurator,完全从头实现。
1 public class FormCollectionMapConfig : IMappingConfigurator 2 { 3 public static TPostData GetPostData<TPostData>() where TPostData : class,new() 4 { 5 ObjectsMapper<NameValueCollection,TPostData> mapper 6 = new ObjectMapperManager().GetMapper<NameValueCollection,TPostData>(new FormCollectionMapConfig()); 7 8 return mapper.Map(HttpContext.Current.Request.Form); 9 } 10 11 public IMappingOperation[] GetMappingOperations(Type from,Type to) 12 { 13 var members = ReflectionUtils.GetPublicFieldsAndProperties(to); 14 return members 15 .Select(m => new DestWriteOperation() 16 { 17 Destination = new MemberDescriptor(m),18 Getter = 19 (ValueGetter<object>) 20 ( 21 (form,status) => 22 { 23 FormCollection forms = new FormCollection((NameValueCollection)form); 24 IValueProvider valueProvider = forms.TovalueProvider(); 25 ValueProviderResult res = valueProvider.GetValue(m.Name); 26 if (res != null) 27 { 28 return ValuetoWrite<object>.ReturnValue(res.ConvertTo(ReflectionUtils.GetMemberType(m))); 29 } 30 else 31 { 32 return ValuetoWrite<object>.Skip(); 33 } 34 } 35 ) 36 } 37 ) 38 .ToArray(); 39 } 40 41 public string GetConfigurationName() 42 { 43 return null; 44 } 45 46 public IRootMappingOperation GetRootMappingOperation(Type from,Type to) 47 { 48 return null; 49 } 50 51 public StaticConvertersManager GetStaticConvertersManager() 52 { 53 return null; 54 } 55 }
- 综述
EmitMapper是一个开源实体映射框架,地址:http://emitmapper.codeplex.com/。
EmitMapper映射效率比较高,接近硬编码。
EmitMapper采用
emit方式在运行时动态生成
IL,而其他映射框架多是采用反射机制。此外
EmitMapper最大限度地减少了拆箱装箱操作和映射过程中的额外的调用。
1 public class Sourse
2 {
3 int A;
4 decimal? B;
5 string C;
6 public Inner D;
7 string E;
8 }
9
10 class Dest
11 {
12 int? A;
13 decimal B;
14 public DateTime C;
15 16 string F;
17 }
18
19 class Inner
20 {
21 long D1;
22 public Guid D2;
23 }
24
25 Sourse src = new Sourse
26 {
27 A = 1,
28 B = 0M,128)">29 C = "2011/9/21 0:00:00",128)">30 D = new Inner
31 {
32 D2 = Guid.NewGuid()
33 },128)">34 E = test"
35 };
36
37 ObjectsMapper<Sourse,Dest> mapper =
38 ObjectMapperManager.DefaultInstance.GetMapper<Sourse,Dest>();
39 Dest dst = mapper.Map(src);
40
41 Assert.AreEqual<string>(dst.B.ToString(),src.B);
42 Assert.AreEqual<long>(dst.C.C1,0L);
43 Assert.AreEqual<Guid>(dst.C.C2,src.C.C2);
44 Assert.IsNull(dst.E);
可以使用
System.Convert类转换的原始类型;
可空类型、枚举类型、各种集合类型、结构与类;
复杂的嵌套类型采用递归方式转换;
描述 |
|
ConvertUsing |
为指定的成员提供自定义的转换逻辑 |
ConvertGeneric |
为指定的泛型类型成员提供自定义的转换逻辑 |
ConstructBy |
|
NullSubstitution |
当源对象中指定的成员在为null时,给目标对象的成员赋值 |
IgnoreMembers |
忽略指定成员的映射 |
PostProcess |
|
ShallowMap |
指定的成员采用浅拷贝方式映射 |
DeepMap |
指定的成员采用深拷贝方式映射 |
MatchMembers |
如果成员名称的映射不采用精确匹配,可以指定具体的映射逻辑 |
选择几个方法简单示例如下:
public Inner2 D;
25 class Inner2
27 long D12;
28 public Guid D22;
29 }
30
31 ObjectsMapper<Sourse,Dest> mapper1 =
32 new ObjectMapperManager().GetMapper<Sourse,Dest>(
33 new DefaultMapConfig()
34 .IgnoreMembers<Sourse,Dest>(new string[] { A" })
35 .NullSubstitution<decimal?,decimal>((value) => -1M)
36 .ConvertUsing<Inner,Inner2>(value => new Inner2 { D12 = value.D1,D22 = value.D2 })
37 .PostProcess<Dest>((value,state) => { value.F = nothing"; return value; })
38 );
39 Dest dst = mapper1.Map(src);
41 Assert.IsNull(dst.A);
decimal>(dst.B,-1M);
43 Assert.AreEqual<Guid>(dst.D.D22,src.D.D2);
44 Assert.AreEqual<string>(dst.F,0)">");
自定义的映射配置器可以继承自
DefaultMapConfig或
CustomMapConfig,利用基类的一些功能实现定制的映射,也可以继承自接口
ImappingConfigurator,完全从头实现。
class FormCollectionMapConfig : IMappingConfigurator
2 {
static TPostData GetPostData<TPostData>() where TPostData : class,255)">new()
4 {
5 ObjectsMapper<NameValueCollection,TPostData> mapper
6 = new ObjectMapperManager().GetMapper<NameValueCollection,TPostData>(new FormCollectionMapConfig());
7
8 return mapper.Map(HttpContext.Current.Request.Form);
9 }
10
11 public IMappingOperation[] GetMappingOperations(Type from,Type to)
12 {
13 var members = ReflectionUtils.GetPublicFieldsAndProperties(to);
14 return members
15 .Select(m => new DestWriteOperation()
16 {
17 Destination = new MemberDescriptor(m),128)">18 Getter =
19 (ValueGetter<object>)
20 (
21 (form,status) =>
22 {
23 FormCollection forms = new FormCollection((NameValueCollection)form);
24 IValueProvider valueProvider = forms.TovalueProvider();
25 ValueProviderResult res = valueProvider.GetValue(m.Name);
26 if (res != null)
27 {
28 return ValuetoWrite<object>.ReturnValue(res.ConvertTo(ReflectionUtils.GetMemberType(m)));
29 }
30 else
31 {
32 object>.Skip();
33 }
34 }
35 )
36 }
37 )
38 .ToArray();
39 }
41 string GetConfigurationName()
42 {
43 return null;
44 }
45
46 public IRootMappingOperation GetRootMappingOperation(Type from,128)">47 {
48 49 }
50
51 public StaticConvertersManager GetStaticConvertersManager()
52 {
53 54 }
55 }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。