Open Source Entity Mapping Framework Emitmapper Introduction

Source: Internet
Author: User

Open Source Entity Mapping Framework Emitmapper Introduction

    • Review
Emitmapper is an open source entity mapping Framework, Address: http://emitmapper.codeplex.com/. Emitmapper mapping efficiency is relatively high, close to hard coding. Emitmapper uses emit mode to generate IL dynamically at runtime, while other mapping frameworks use reflection mechanisms.      In addition, Emitmapper minimizes the additional calls in the unboxing and mapping process.       Emitmapper supports all platforms for. NET: Framework 3.5, Microsoft Silverlight 3, Mono. The use of Emitmapper is very simple and does not require any mapping policy to be specified. The mapping operation is done using the default mapping configurator 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
public class Dest
11 {
public int? A
public decimal B;
Public DateTime C;
Public Inner D;
+ public string F;
17}
18
public class Inner
20 {
A public long D1;
Public Guid D2;
23}
24
Sourse src = new sourse
26 {
A = 1,
B = 0M,
C = "2011/9/21 0:00:00",
D = new Inner
31 {
D2 = Guid.NewGuid ()
33},
E = "Test"
35};
36
Panax Notoginseng objectsmapper<sourse, dest> mapper =
Objectmappermanager.defaultinstance.getmapper<sourse, dest> ();
Dest DST = mapper. MAP (SRC);
40
Assert.areequal<string> (DST. B.tostring (), Src. B);
Assert.areequal<long> (DST. C.C1, 0L);
Assert.areequal<guid> (DST. C.C2, Src. C.C2);
Assert.isnull (DST. E);
    • Default Mapping Configurator
The default mapping configurator can automatically convert the following types: any type to string type using the ToString () method; You can use the original type of the System.Convert class conversion; nullable types, enum types, various collection types, structs and classes ; complex nested types are converted recursively; if the default transformation does not meet the requirements, the default mapping configurator also allows specifying naming conventions, custom constructors, custom converters, ignoring members, and so on.

Supported methods

Describe

Convertusing

Provides custom conversion logic for the specified member

Convertgeneric

Provides custom conversion logic for a specified generic type member

Constructby

Overrides the default constructor with the specified constructor for the target object

Nullsubstitution

Assigns a value to a member of the target object when the member specified in the source object is null

Ignoremembers

Ignore mappings for specified members

PostProcess

Executes the specified method after the mapping is complete

Shallowmap

The specified member is mapped in a shallow copy manner

Deepmap

The specified member is mapped in deep copy mode

Matchmembers

If the mapping of member names does not take exact matches, you can specify specific mapping logic

A simple example of selecting several methods is as follows:
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
public class Dest
11 {
public int? A
public decimal B;
Public DateTime C;
Public Inner2 D;
+ public string F;
17}
18
public class Inner
20 {
A public long D1;
Public Guid D2;
23}
24
public class Inner2
26 {
public long D12;
-Public Guid D22;
29}
30
Objectsmapper<sourse, dest> mapper1 =
New Objectmappermanager (). Getmapper<sourse, Dest> (
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);
Dest DST = mapper1. MAP (SRC);
40
Assert.isnull (DST. A);
Assert.areequal<decimal> (DST. B, -1m);
Assert.areequal<guid> (DST. D.d22, Src. D.D2);
Assert.areequal<string> (DST. F, "nothing");
    • Custom Mapping Configurator
Of course emitmapper is a very flexible framework, and you can customize the mapping configurator for custom mapping operations.       A custom mapping configurator can inherit from Defaultmapconfig or Custommapconfig, implement custom mappings with some of the functionality of the base class, or inherit from the interface Imappingconfigurator, completely from scratch. For example, you can implement the mapping of form data submitted from HTTP via post to the specific business entity class, which is implemented by inheriting Imappingconfigurator. Custom mapping configurator go from http://www.cnblogs.com/wuhong/archive/2011/09/21/2184313.html

Open Source Entity Mapping Framework Emitmapper Introduction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.