AutoMapper_snippetWhen you use external libraries you are forced to use their classes inside your own code. To make yourself more confident, it is wise to make your own classes simpler than external ones. Usually, in order to do this you have to do some manual code behind. AutoMapper helps you in that process by automatic mapping fields between classes and rewriting their fields. How does it work?

To start your mapping using AutoMapper you have to do two things

Firstly, you have to define the way how mapping should be carried out. The simplest example is:

Next, you pass a class instance of Source class to a mapper in order to convert it into Destination class instance:

This code snippet will create the new instance of Destination class with fields rewritten from a Source class. It is important to note that the fields are mapped by their names. If the name is different, current configuration will not handle these fields and will assign a null value.

More complex usage of AutoMapper

You can imagine how many different scenarios you may experience while converting one object instance to another. The whole list of AutoMapper features is available here. Below I will show the neatest snippets. I have found AutoMapper extremely useful in changing the collection objects from one to another. Imagine, you have LLBLGEN self servicing class that reprents the collection of products – ProductsCollection class. This class implements IEnumerable and contains products – ProductEntity class. You want to map it to IList<IProduct> object. This way you hide from the external code your LLBLGEN entity classes and you limit the amount of properties in IProduct interface.

To convert ProductEntity to IProduct you don’t need any additional classes. By the correct AutoMapper configuration you can perform your conversion pretty smoothly.

Now, you can convert your ProductEntity object to IProduct. In our example, we have ProductCollection class which we want to convert to IList<IProduct> object instance. AutoMapper is able to perform this conversion directly without defining additional mapping for our collection classes. Our conversion code is:

How AutoMapper converts classes to interfaces?

In the above example, you are probably curiouse what kind of class is used to create the instance of object that is behind IProduct interface. This class is called DynamicProxy and is created by use of LinFu library. This way, we don’t have to create our own classes to map ProductEntity to IProduct interface. AutoMapper creates a custom proxy class for you.

AutoMapper is a useful library that speeds up your coding

Thanks to AutoMapper I avoided creating special proxy classes to map my domain object to more simple ones. This for sure can speed up your coding a little and makes your components less vulnerable to external libraries’ changes. Share your opinion and experience with us below or meet us on Twitter: @GOYELLO.

IT guy with head full of ideas, strongly oriented on achieving great goals in life. Currently working for GOYELLO, place where I grow.
  • Pingback: Reflective Perspective - Chris Alcock » The Morning Brew #450()

  • Pingback: Weekly Web Nuggets #78 : Code Monkey Labs()

  • i have posted your blog on my site

    respect
    james smith
    ______________________________________________

  • What I then want to do, rather than writing code to populate each BLL property from its DAL … it's better to configure your type maps just once per application lifetime

  • maciejgren

    Hi,
    yes, the configuration should be stored once per life time. To do so it should be moved to for instance Global.asax or to Bootstrapper if you use IoC Container.

  • Well , the view of the passage is totally correct ,your details is really reasonable and you guy give us valuable informative post, I totally agree the standpoint of upstairs. I often surfing on this forum when I m free and I find there are so much good information we can learn in this forum! http://less-accurate.com/

  • Here elaborates the matter not only extensively but also detailly .I support the
    write's unique point.It is useful and benefit to your daily life.You can go those thefer.net Like-Acer tiny let-lands iamateacherithink
    sits to know more relate things.They are strongly recommended by friends.Personally
    I feel quite well.

  • I am also a XX fan who really like this! I also like XX, and purchase lots of it every time, like-minded friends can have a look ,we can communicate by the way~~

  • Pingback: [EN] AutoMapper is trying to be too smart ? « Marcin Dembowski()

  • don’t you ever use IProduct as a class. It’s always an interface in people’s minds)