Tuesday, October 27, 2009

ASP.NET MVC Translated for the Web Forms Programmer (5 in a series) - What the Frig is a View Model?


I wanted to expound on something very important that I kind of glazed over in the last post, View Models. View Models are something that is very pivotal to MVC and were one of the hardest things for me to understand. I feel like it's important to go over them in depth so you can understand them as well. 

 

I actually don't love the concept of a View Model, it's a little muddy for me. I understand that it is important because it allows the View to do what it was made for: rendering and displaying elements. However, like any other programmer I like things clean and easily defineable. I like concrete answers, and non-redundant interfaces. A View Model is none of these things. It is an arbitrary collection of data gathered for the express purpose of conveiniently grouping your views. 

 

Before I completely talk you out of them I must clarify that I love using them in my MVC applications because it alows me to seperate my OO archtecture from my Views. You know, seperation of concerns and all that. 

 

So how do I get started?  A View Model is simply a class that you build to aggregate the data you want to display in your view. 

 

Let's say I have a class Car and a class Train. I want a view to display all the ways I can get from New Jersey to New York. I have a train schedule and a list of rental cars. The classes Train and Car are not associated to each other in my data model. So, what to do? That is where our View Model comes in. They are pretty easy to create, my Car/Train view Model is called TransportationViewModel and it looks like this:

 


To populate my View in my Controller I create an action and when I return my View I pass in an instance of my TransportationViewModel. Like this:

 


As you can see this is just a simple class that contains both a list of Cars and a Train. My view will be strongly typed and inherit from this View Model. This is how it will look:

 


As you can see, my View Model allows me to pass my Data to my view and I don't have to muss with my objects or my model. 

 

 When it comes to data manipulation you want to keep that in the Model, not do it in your View Model. Unless what you are messing with is specifically for that particular view. 

 

Now, the question of the year is: "Do I need a new View Model for each View?" The answer is: it's up to you. If you only need one class in your particular view than you don't need one. However, I find that to be very rare. 


Thursday, October 22, 2009

Bundl.it release and Philly.NET CC

So, Philly CC went great. I started to make a video about it here: however, You Tube ate my video three times.  You can


watch part of it here: http://www.youtube.com/watch?v=2Ej8L1RxDJU


That was a greeeeat minute. 



I will get back to the video later this week and make it for reals now? I just wanted to touch base and say two things. Firstly, http://bundl.it is going great. We now


have a blog at http://bundlitblog.wordpress.com/2009/10/03/well-hello-there/ run by my trusty sidekick Lloyd. 



I also was awarded an MVP award by Microsoft. I am greatful for the honor and can't wait for the summit. 



Everything else will have to wait until later in the week when I have time to redo this video. Tomorrow we will be celebrating the release of Bundl.it at the New Work


City anniversary party. Should be a blast. 



Wednesday, October 14, 2009

Unbridled Endorsement of the Best Code Camp Ever


So, Philly .NET Code Camp is where I made my speaking debut (amid much stuttering and  shaking). I may be biased when I say this is the best .NET Code Camp in the US. They have it twice a year at the Devry Campus in Fort Washington PA. It goes from 8:30 am until 5:00 pm with Breakfast, Lunch and Prizes. It's all completely free and the presenters are a Who's Who in .NET. I'm really excited to see a lot of people speak, and to meet some people that I haven't before. If you live in the area it would be awesome to see you, please come say hi. 



In other news I am working on a website snapshot generator for the thumbnails on http://bundl.it. I'm really unhappy with the service I am using and its reliability. I am also learning some Silverlight stuff thanks to the help of Chris Bennage of Devlicio.us fame. This is a lot of fun as I never expected to learn something so closely linked in design work. The most I've done on a related level is make a picture fade in and out in flash. It is a slow learning process. 


Thanks to all that came out for the NY Nerd Dinner at Stout tonight. We were a mellow group, but I really enjoyed our conversation and meeting some of you for the first time. 



Sunday, October 4, 2009

Nerd Gear - What to wear?


  So, I do a lot of nerd events as you probably notice. This weekend was the MVC Firestarter done by Microsoft. Stephen Bohlen did an awesome write up here), and I did something a little different than I normally do: I dressed like a girl. Usually I like to up my nerd credibility by wearing a tee shirt and sneakers. However, if you catch me on an average day you'll see me in a stylish (well, at least what I consider stylish) out fit and pumps. Not at all casual. 


  I usually figure that if I wear something more low key it will make me more approachable, however what I really think is that if I dress down it will make the gender gap not so gaping. I also think it makes me a little more approachable and likely to make friends at these things. 


  What changed this weekend?  I don't know. The night before I was thinking of what to wear. I have this default pair of cargo khakis that are great for nerd events. I have a small handful of nerdy tees, and I was thinking about which one to put on and lamenting about the fact that I didn't have anything new and I feel like I have outworn a lot of tees. At that moment I decided that was the end of it, I was going to dress normal for once. I didn't wear anything crazy, just khaki boot cut fitted pants, a red and white striped sweater, and white flats. 


  So, was I treated differently? Well, to be honest I feel like less people came to talk to me. However, that wasn't exactly a scientific study, and I think I will do this a few more times before coming to a conclusion. 



   Ladies, do you find yourself doing the same thing? Gents, how do you feel about my attitude towards this? Any stock in being afraid to be different. Please feel free to be candid. 




Non-sequitors: I'm speaking at the awesome Philly .NET code camp in two weeks, I got a ASP.NET MVP award this week, and my new site http://bundl.it launched!!! What a crazy week, will do a write up about its creation soon.