- 综述
EmitMapper是一个开源实体映射框架,地址:
http://emitmapper.codeplex.com/。
EmitMapper映射效率比较高,接近硬编码。EmitMapper采用emit方式在运行时动态生成IL,而其他映射框架多是采用反射机制。此外EmitMapper最大限度地减少了拆箱装箱操作和映射过程中的额外的调用。
EmitMapper支持.net的所有平台:Framework 3.5、Microsoft Silverlight 3、Mono。
EmitMapper的使用非常简单,不需要指定任何的映射策略。系统会采用默认的映射配置器DefaultMapConfig完成映射操作。

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");
- 自定义映射配置器
当然EmitMapper是个非常灵活的框架,也可以自定义映射配置器,实现定制的映射操作。
自定义的映射配置器可以继承自DefaultMapConfig或CustomMapConfig,利用基类的一些功能实现定制的映射,也可以继承自接口ImappingConfigurator,完全从头实现。
比如可以实现从HTTP中通过Post方式提交的Form数据到具体业务实体类的映射,下面通过继承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 }