AutoMapper library in and how to use it in Asp.Net MVC application with C# for object-to-object mapping with configuration and other helper methods for complex objects
2. What is AutoMapper?
AutoMapper is a simple little library built to solve a deceptively complex
problem - getting rid of code that mapped one object to another.
AutoMapper is an object-object mapper. Object-object mapping works by
transforming an input object of one type into an output object of a
different type.
What makes AutoMapper interesting is that it provides some interesting
conventions to take the dirty work out of figuring out how to map type A
to type B.
As long as type B follows AutoMapper's established convention, almost
zero configuration is needed to map two types.
3. Why use AutoMapper?
Mapping code is boring. Testing mapping code is even more boring.
AutoMapper provides simple configuration of types, as well as simple
testing of mappings.
The real question may be "why use object-object mapping?" Mapping can
occur in many places in an application, but mostly in the boundaries
between layers, such as between the UI/Domain layers, or Service/Domain
layers.
The mapper is needed to map properties of source object (from DB or web
service) to those of the destination object (which will supplied to view).
Concerns of one layer often conflict with concerns in another, so object-
object mapping leads to segregated models, where concerns for each layer
can affect only types in that layer.
4. Why use AutoMapper?
Challenge: Sometimes while interacting with real time (database) entities
and binding our model to them, we end up in a situation like:
5. Why use AutoMapper?
Resolution: What makes AutoMapper interesting is that it provides some
interesting conventions to take the dirty work out of figuring out how to
map type A to type B. As long as type B follows AutoMapper's established
conventions, almost zero configuration is needed to map two types."
Therefore, it provides the solution for our mapping issue.
6. How do I get started?
Firstly install the NuGet Package Manager in your Visual Studio IDE. Once
done, go to:
Tools -> Library Packet Manager -> Packet manager Console
Then in the console window opened at the bottom of Visual Studio, type:
PM> Install-Package AutoMapper
7. How do I use AutoMapper?
AutoMapper works best as long as the names of the members match up to
the source type's members. If you have a source member called
"FirstName", this will automatically be mapped to a destination member
with the name "FirstName".
AutoMapper will ignore null reference exceptions when mapping your
source to your target. This is by design. If you don't like this approach, you
can combine AutoMapper's approach with Custom-value-resolvers if
needed.
8. How do I use AutoMapper?
Once you have your types you can create a map for the two types using
Mapper.Initialize and CreateMap. Mapper.Initialize should be called only
once during startup.
The type on the left is the source type, and the type on the right is the
destination type. To perform a mapping, use the Map method.
AutoMapper also has non-generic versions of these methods, for those
cases where you might not know the type at compile time.
10. Where will AutoMapper fit in here?
It will be used to replace the property mapping done one by one in the
code, therefore, at the first line of code, define an AutoMap. To create the
default mapping, callMapper.CreateMap<T1, T2>() with proper types.
LearningMVC.User -> DTO Object Class
LearningMVC.Models.User -> Model Class to bind the View
here we define a mapping between DTO and the Model class with the help
of the AutoMapper class.
11. Where will AutoMapper fit in here?
Now inside the foreach loop, replace the whole code by:
Finally call the Mapper.Map<T1, T2>(obj1) to get the mapped object of T2.
13. General Features
Before and after map actions
Containers
Mapping Inheritance
Queryable Extensions (LINQ)
Configuration
Conditional Mapping
Open Generics
Flattening
Projection
Configuration Validation
Lists and Arrays
Nested Mappings
Custom Type Converters
Custom Value Resolvers
Null Substitution
14. Projection
Projection transforms a source to a destination beyond flattening the
object model. Without extra configuration, AutoMapper requires a
flattened destination to match the source type's naming structure.
When you want to project source values into a destination that does not
exactly match the source structure, you must specify custom member
mapping definitions.
15. Projection
For example, we might want to turn this source structure:
Into something that works better for an input form on a web page:
16. Projection
Because the names of the destination properties do not exactly match up to
the source property (CalendarEvent.Date would need to be
CalendarEventForm.EventDate), we need to specify custom member
mappings in our type map configuration:
17. Projection
ForMember() and MapFrom() in AutoMapper Two important functions in
AutoMapper play an important role in object mapping.
Suppose our model/viewmodel class has a property FullName, and from
the DTO we want to add the First Name and Last Name of the user to
make it a full name and bind it to the model. For these kinds of scenarios
ForMember() andMapFrom() come in handy.
Here we are saying that ForMember FullName in our model class maps
properties from FirstName andLastName of User DTO.
Hinweis der Redaktion
We see here that there are eight properties that are similar to each other but each set lies in a separate class, one in the Model, and one in the Users class. And what we do is one by one we bind these properties to our model and pass it to the View. Now the problem is what if we have 100 column records coming from the database, and also our model has the same number of properties, and the code has to be repeated 6-7 times at different scenarios? Now do we still follow such a strategy of binding each property from the database to the model? Trust me, the code will be 100 pages large, and will be charging five times the effort just to bind the model from the domain entities.
To overcome this tedious situation AutoMapper is introduced. It not only reduces the effort, but also limits the execution time that has been taken by such a large number of lines to execute.
We see here that there are eight properties that are similar to each other but each set lies in a separate class, one in the Model, and one in the Users class. And what we do is one by one we bind these properties to our model and pass it to the View. Now the problem is what if we have 100 column records coming from the database, and also our model has the same number of properties, and the code has to be repeated 6-7 times at different scenarios? Now do we still follow such a strategy of binding each property from the database to the model? Trust me, the code will be 100 pages large, and will be charging five times the effort just to bind the model from the domain entities.
To overcome this tedious situation AutoMapper is introduced. It not only reduces the effort, but also limits the execution time that has been taken by such a large number of lines to execute.