Sunday, August 8, 2010

Learn How To Use Automapper To Make Your Life So Much Easier!

I love it when a tool actually DOES make my life easier! Don't you? As programmers, we are faced with a plethora of various tools to use for development. Some of them can really leave you wondering if you are really as smart as you think you are when you try to figure them out. In an effort to refine various processes I have in place for my .NET MVC development and to see how much more activity I can squeeze out of my development time, I decided some months ago to really dig into various programs designed for .NET. Those programs included dependency injectors, mapping tools, coding tools, etc.

A few months ago, I came across one tool that looked interesting. However, the tool didn't have what I considered to be enough documentation for me to dig into it - or so I thought. Perhaps I was being lazy or maybe I was on tool overload. I looked at it for a few minutes and really pushed it aside because I thought I didn't have the time to go figure out how to make it work. Boy was I wrong! That tool is called AutoMapper. The tool is designed to be a bridge between your domain model and the various representations of that model that you might need for your views - among other things. If that comment flew right over your head, don't worry because I'm going to explain what I mean.

Say that you have a domain model called Homes. This model describes all of the different types of homes that your client is going to be selling. You craft a beautiful domain model and now it is time to display some data from it in your view. You only want a few items from the Home domain in one view and different things in another. At this point, you are faced with a few choices. You could have your controller send the data DIRECTLY from your domain model to your view. However, there are or items that you don't need to display in certain views and there may be a particular way you want the data to display in certain views. You could format the display of the data INSIDE of your view - which isn't optimal because views shouldn't go around making decisions. You could modify the data in your controller OR (drum roll please......) you could create a view model specifically designed for your views that displays the data formatted exactly how you want it to be displayed. You can also apply all types of juicy validation in any way you want without effecting the underlying domain model in any way. AutoMapper lets you do that last few things I said quickly and easily.

After deciding to revisit this tool and for more documentation on it, I came across some great stuff that had been added to the project. In fact, I came across something that gave me that good old AHHHH HAAAAA moment and made me feel all warm and tingly inside. I came across a Silverlight video that walks you through a demo of AutoMapper! This video is located here at When I saw that Url I had to laugh - if ever there was a need for some good old fashioned MVC Url routing, that is it! Anyway, the video is absolutely excellent. There is too much talking in the first few minutes of it, but once Jimmy Bogard starts explaining the tool he developed, you will be hooked (well only if you are geeky and nerdy like me :-)). I also discovered that the video had been added to the documentation on CodePlex,. I didn't see that when I first looked at AutoMapper! Reminds me of that contact lens commercial where the woman tells the guy to look with his special eyes (go look it up on YouTube). Anyway, I'm thankful for it because it offers an in-depth look at some of the many things that AutoMapper can do.

AutoMapper is lightweight, easy to set up and makes the process of creating various view models from your domain model an absolute breeze. If you are really into separation of concerns then you need to be into AutoMapper.

Check out AutoMapper here -

Check out more about Jimmy Bogard, the creator of AutoMapper here -

Check out the video here -


No comments: