EmitMapper系列之一:EmitMapper入门
2017-09-13 16:34 小白admin 阅读(3753) 评论(0) 编辑 收藏 举报EmitMapper的总结
EmitMapper简介
前言:
参考官网: http://emitmapper.codeplex.com/
Project Description
Powerful customisable tool for mapping entities to each other. Entities can be plain objects, DataReaders, SQL commands and anything you need. The tool uses run-time code
generation via the Emit library. It is usefull for dealing with DTO objects, data access layers an so on.
官方原文翻译:
项目描述
用于将实体映射到彼此的强大的可定制工具。实体可以是纯对象,DataReaders,SQL命令以及任何您需要的。该工具通过Emit库生成运行时代码。处理DTO对象,数据访问层等都是有用的。
优点:
我们看看官方怎么说???我想官方给了四点足以让你在相应的项目中使用它
- Extremely fast. It effectively uses the Emit library to generate mappers at run-time direct in IL as though these mappers are written by hand. Most other mappers use the Reflection library for mapping (or source code generation). Also EmitMapper minimizes boxing-unboxing operations and additional calls during mapping. For example it performs type conversion for value-types without boxing-unboxing and converts nested members without recursion (one-pass algorithm) when it is possible.
- Easy to use. Emit Mapper produces mappers at run time and there is no need for special complex build process unlike some other libraries which perform source code generation. Emit Mapper API is as simple as possible. You don't need to create any tedious XML document, or scatter strange attributes on your objects. Mapper can be created by one method call. But if you need fine-grained mapping configuration, you can easily reach it.
- Flexible. Emit Mapper doesn't have any hardcoded mapping strategy (special mapping attributes, XML documents or whatever) unlike most other mapping libraries. Instead, Emit Mapper has extremely flexible API and you can define any mapping yourself. For example you can define automatic mapping for DataReader to your business-objects. In general, Emit Mapper can be used as high performance tool for dynamic access to object properties and fields.
- Multi-Platform. The Emit Mapper can be run on Microsoft .NET Framework, Silverlight and Mono
官方给出四个优点
- 非常快 它有效地使用Emit库在IL中直接在运行时生成映射器,就好像这些映射器是用手写的。大多数其他映射器使用Reflection库进行映射(或源代码生成)。此外,EmitMapper还可以最大限度地减少打击拆箱操作和映射期间的其他调用。例如,对于没有打包装箱的值类型,它执行类型转换,并在可能时转换嵌套成员而不进行递归(一次通过算法)。
- 使用方便。Emit Mapper在运行时生成映射器,与执行源代码生成的其他库不同,不需要特殊的复杂构建过程。Emit Mapper API尽可能简单。您不需要创建任何繁琐的XML文档,或在对象上散布奇怪的属性。Mapper可以通过一种方法调用创建。但是,如果您需要细粒度的映射配置,您可以轻松实现。
- 灵活。与大多数其他映射库不同,Emit Mapper没有任何硬编码映射策略(特殊映射属性,XML文档或其他任何内容)。相反,Emit Mapper具有非常灵活的API,您可以自己定义任何映射。例如,您可以为DataReader定义自动映射到业务对象。通常,Emit Mapper可以用作动态访问对象属性和字段的高性能工具。
- 多平台。Emit Mapper可以在Microsoft .NET Framework,Silverlight和Mono上运行。
类型转换:
Emit Mapper can automatic convert the following types:
- Anything to string using the ToString() method.
- Primitive types using System.Convert class.
- Nullable types to value types and vice-versa.
- Enum to its underlying type and vice-versa.
- Enum to string and vice-versa.
- Collections of different types to each other (arrays, ArrayList, List<>, IEnumerable)
- classes to structures and vice-versa.
- Complex types with complex nested members using recursive mapping with shallow or deep copieng.
Emit Mapper可以自动转换以下类型:
- 任何使用ToString()方法进行字符串处理。
- 使用System.Convert类的基本类型。
- Nullable类型对值类型,反之亦然。
- 枚举为其底层类型,反之亦然。
- 枚举到字符串,反之亦然。
- 不同类型的集合(数组,ArrayList,List <>,IEnumerable)
- 类到结构,反之亦然。
- 具有复杂嵌套成员的复杂类型使用具有浅或深对应的递归映射。
总结:官方的描述,也可以在面试过程中进行简述,加分项。。。。。
- 博主是利用读书、参考、引用、抄袭、复制和粘贴等多种方式打造成自己的纯镀 24k 文章,请原谅博主成为一个无耻的文档搬运工!
- 小弟刚迈入博客编写,文中如有不对,欢迎用板砖扶正,希望给你有所帮助。