What is AutoMapper?
AutoMapper is an object-to-object mapper. Object-to-object mapping works by transforming one type of input object into a different type of output object. What makes AutoMapper interesting is that it provides some interesting conventions for the boring thing of mapping type A to type B. As long as type B adheres to the conventions already established by AutoMapper, in most cases, mapping two types of zero configurations will do.
Why use AutoMapper?
Mapping code is boring. Testing the mapping code is more boring. AutoMapper offers some simple configuration, as well as some simple mapping tests. The real question may be "why use object-to-object mapping"? Mapping can happen in many places in an application, but most often it happens at the boundaries between layers, such as between UI/Domain layers, or between Service/Domain layers. Focusing on one layer often conflicts with focusing on another, so object-to-object mapping isolates the model so that only the type of concern for each layer is affected.
An example I wrote myself that includes:
1: Conversion of int and string 2: Conversion of int and int 3: Conversion of enums and strings 4: Conversion of objects and strings Wait a minute
Source Object:
Target audience:
The conversion code is as follows:
Source code download:
Tourists, if you want to see the hidden content of this post, please Reply
|