Linq and Automapper, A View Model and Data Model binding solution. Part – 1

Hi there, Hope you are doing great, I have another topic I would like to discuss with you! It’s Automapper. You may have heard of it and you may have already used it but I think there will be a little take away for everyone in this post. This will…

Hi there,

Hope you are doing great, I have another topic I would like to discuss with you! It’s Automapper. You may have heard of it and you may have already used it but I think there will be a little take away for everyone in this post. This will be the first part of the post

Today we are going to talk about:

1) How we can use Visual studio Nuget to get Automapper in our MVC 3 solution.

2) How we can map out LINQ data classes ( I will call them Data Model classes ) and bind them using Automapper.

3) Discuss how efficient and beneficial Automapper is than manual mapping.

However this first part will only consist of basic scenario. A basic mapping of Data Model classes to View Model which will be used by our views. The idea is to familiarize and know Automapper before we start unleashing all it’s powers.

Let’s start with Nuget. Nuget is a God send to me. I have had the pain of manually adding references, dll, scripts and css, setting dependencies to .Net project for a simple solution that is provided by a third party tool. Nuget makes all that as simple as few clicks. It’s a “Package Manager” that now ships with Visual studio 2010. You can add thousands of third party tool without all the hassle by searching in it’s console, It will automatically set references, dependencies and what not. You can learn more about Nuget here.

So I used Nuget to find Automapper. Let’s discuss a little bit about Automapper.

Automapper is a object to object mapper. You can potentially map any object to any other object easily via this Automapper library. I personally use to map my Data Model to View Model and vice versa.

Why? Because Automapper takes care of my unproductive coding where I just write redundant code of mapping View and Data Models, It automatically does it for me at fairly low cost. Yes! A few configurations and we are done! Just give Automapper your input model and gives you your desired output model. For complete details see this.

I like to use LINQ to query my SQL database in my MVC solution. So for testing purposes I created this database dragged tables to my LINQ file which automatically created Data Classes for me. These classes look fairly like my model but I don’t want to use them as my View Model ofcourse… So Let’s create a basic Customer Model (View Model). Here is how it looks.

37272671-Customermodel
37272672-DBMLCapture

Fairly simple. Fairly similar to my Data Model Class. For part 1 let’s keep it that way for now.

Now I have installed Automapper but haven’t done anything with it yet. Let’s do it.

I have created a Folder “Data Context” where I keep my .dbml file and I have created a static class called “Mapping“. Mapping because it’s going to perform mapping via Automapper and static because it will be common for all the sessions.

Here is how it looks.

37272689-MAPPIING

There are three methods,

Customer_ViewModelToDataModel() -> It calls Mapper (Autmapper’s class) .CreateMap takes in two template types, since it’s View Model-> Data Model binding the first argument is Input(View Model) and second argument is desired output type which is DataContent.Customer our LINQ Customer class. Based on relationships in Database, LINQ created a Transcation member in Customer class which is not present in out View Model “Customer ” class hence we perform an “Ignore” to leave that member out of the mapping.

Thats it. Automapper created a map for us. We don’t even have to specify which members map to which in each models.. Cool isn’t it.

Similarly we do Data Model -> View Model, since View Model’s Customer class does not have a “Transaction” member there is nothing to ignore in it. We have created another map.

The third function PerformMapping just wraps these methods so we can call them together.

Now, the application needs to know about this mapping before anyone tries to map one object to another. Since this is Asp.net web project the best place to call our “PerformMapping” function will be Global.asax. Here is what I do there.

37272706-global

This is it. Your application knows that there is a map and these objects can be converted into one another via Automapper, but wait how to actually do that?

Here is how. I have created a view strongly typed with my View Model Customer. I will let MVC generate fields for me and this is what it gave me. Now we will perform simple insertion in DB for this posted View Model, we will convert this into Data Model Customer obect that LINQ recognizes and perform and insertion into a DB. So here are the snapshot for these steps

37272888-createcust
37272889-createcust1

Notice I now use Mapper.Map instead of CreateMap, the templated parameters remain the same, the first one my input object, second one my output object but here i pass a object as a parameter so that Automapper can read from this object and write it to my desired object and give me back..

You would want to see that Model inserted don’t you.. Let’s generate another view called Show Customer which will fetch this view out, Only this time we will use Automapper to convert our Data Model LINQ Customer class to our View Model Customer class and pass that along in the View so that our view can display that. Here is a method that does that. Sure enough I have my Data Model as input, View Model as output and a LINQ queried data object of Customer passed in to Mapper.Map method. It gives me my View Model as expected. I now use this View Model and pass it down to my strongly typed View.

 

37272899-showcust1
37272900-shwcust

OK so we have seen what Nuget can do, and how via automapper we can convert the objects quickly into one another without writing any stupid redundant code, without even specifiying the individual member mapping in any of these model, pretty slick I think.

WAIT It is not over yet…

If you are anything like me you would wonder Automapper sure converts the data under the hood nicely but what are the cost that comes with it? Well for this kind of a mapping, not much.. Have a look a this.

Specially the last image.

37272760-timetest1
 37272761-timetest2
37272763-timetest3
37272765-timetest4

I am comparing the binding of 1, 10, 100, 1000, 10000, 100000 set of sample models via manual mapping and Automapper, manual mapping ofcoruse is faster but Automapper is not left that far behind. But again this is my i7 with 8 Gb RAM server and you will hardly parse 100000 model on a single shot. Also It’s the application expected use which decide how crucial these timings are and whether it is wise to choose / add conversion layer between your data model and view model. That being said, do not forget this is a smile mapping and not fancy. In part 2 we will have complex model binding and again see if Automapper keeps up the performance like this..

Hope you will stay around…

Later..

Times Square on a New Years eve, an amazing experience.

It is always a great experience to do something that has been sitting on your wish list for a while. Being at Times Square, NY was a amazing, tiresome, unique experience, which CAN’T happen anywhere in the whole wide world. I love New York City an…


It is always a great experience to do something that has been sitting on your wish list for a while.

Being at Times Square, NY was a amazing, tiresome, unique experience, which CAN’T happen anywhere in the whole wide world. I love New York City and Times Square. For me It was awesome to be around 1 million people celebrating the new year. Practically the biggest celebration of the new year in the World!!

Yes It was crowded, It was even almost impossible to move your feet without falling or to dance on a song without crushing someones legs but It was all happening, We were a group of 6-8 guys who were waiting on the 46th Street from 2pm for the midnight (Yes we went there around 2 pm and still could only reach around 46th – 47th street), the countdown and the ball drop (42nd street).

Frankly speaking, there is nothing special about the ball drop that 1 million people gather to see. It is just the way it has been, and It’s just the way I think It will be. Its city’s own style of celebrating the new year. The lights of New York City and the music there was the only thing that can make you survive the crowd, the time.

You get the see the live performances by some good singers, for 2012 celebration we had Drake, Justin Bieber, Pitbull and some really cool Spanish singer, I don’t know the name. I don’t care to mention Lady Gaga, as she was boring as hell. If you ever wanna go there, have some quick bites for you handy like chocolates, chips etc. You don’t want to take sodas and drinks with you, unless you wish to rush out to find a place to pee, We had one of that kind in our group, he ended up seeing ball drop from the place where you can not actually see the ball, 😛

The best part for me was that I was with some of my very close friends and we were having a great night, when hungry the pizza guys showed up distributing pizza (NOT FREE…). We caught some flying caps and hit some beach balls around. The crowd near us was friendly and pretty so there was a little to complain except crying feet. The weather was surprisingly awesome which I thought was a great plus point. There is not much you can say, its the energy you feel when you were around the crowd, the music and the event.

And the most awaited midnight came, the countdown was amazing, the shout outs were loud, people were happy, the ball finally dropped, the songs were great, It was as if the most lively city was never alive before. Lovers kissing, Friends hugging, millions of colorful paper flakes fell from the sky.. It was awesome. For me, my girlfriend wasn’t around for the midnight kiss :( but I survived that somehow.. 

Times Square at New Years eve was one of the thing that I wanted to do while I am on the east coast and It finally happened and I loved it.

If you ever want to go, make sure, you are okay with HUGE crowd, have patience, and can stand for more than 6 hours. If so, you should definitely go with your best buds, girlfriend/boyfriend or your spouse if you are still young for this :P.

If you think you would enjoy that, You will have a really tiresome experience but It will all be worth in the end.

Here are some pics if you want to see them..Of course these are just iphone pics in the crowd, If you would like to see really good images search google images for “new years eve 2012 time square”

 

Optimized-36693424-photo_2
Optimized-36693425-photo_3
Optimized-36693454-photo_1

 

 

 

 

 

 

 

 

Optimized-36693427-photo_4

 

 

 

 

 

 

 

 

Later….