Home > Asp.net MVC > AutoMapper in MVC

AutoMapper in MVC

What is AutoMapper?

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.

Why AutoMapper?

Suppose if our input object contains 20 properties then we need to write 20 lines of code for converting input object to destination object. Using AutoMapper, it will be done in less lines.

e.g.

Without AutoMapper.

With Mapper:

Note: Input and Destination object should have same property names.

The ForMember() method

Another important and useful method the AutoMapper provides is a ForMember() method. Suppose our dbContext class has two properties called FirstName and LastName whereas our model/viewmodel class has a property called FullName. Then we don’t need to create two extra properties in our model/viewmodel. We can use the ForMember() method for the mapping.

The Ignore() method

This method is useful when we have extra properties inside our destination object. This scenario is useful in ViewModel cases where we usually have extra properties. Then we can use the following:

If you have any questions. please drop a comment 🙂

This Article is TAGGED in . BOOKMARK THE permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">