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. 


    



Sunday, September 27, 2009

Concept Camp 2009 Review



Next year we are getting tee-shirts made before hand that say "I Survived Concept Camp 2010" because the odds were against us this year. My friend Erika and I got in around 10pm Friday night. Took us about 4 hours to get down to Hagerstown.We set up our tent in the dark (two of them) and blew up our air mattress in the dark (two of them). We hung out for a bit, exchanged some greetings, then we went to bed. 

 

I was woken in the morning by screaming children at 6:30am. Charming. Our morning keynote was unable to make it so I emerged from my tent rather leisurely around 9am. We started breakfast, eggs, bacon, sausage, turkey sausage. It was hard to wrangle but we did it. After breakfast it started raining a bit. No better campers than engineers I say. The guys put up a great fort to protect us from the rain. It did, and it was the perfect place to start our talks. First we heard from Chris Ballance about Design Patterns for Scalability, then from Steve Andrews about Giving Technical Presentations. The rain got worse as the afternoon went on however we were still in good spirits. 

 

After getting some snacks, and picking up our steaks (thank you to our sponsor,Infragistics) we made our way over to a large open air gazebo where Matt Podwysocki taught us all the power behind F# and Functional Programming. 

 

The consensus at this point was that we should cook our beautiful steaks. This was no easy task as the rain was coming down pretty hard at this point. However, geek ingenuity prevailed again as we stacked some "fire rings" on top of each other and cooked our steaks, corn, and potatoes. We found a covered picnic area to eat under and we had a great dinner. 

 

Then I spoke about working on personal projects, we took a break to start playing some drinking games and rejoined back up at the gazebo for an unforgettable keynote by K Scott Allen that really spoke to me. He spoke about when to declare "Victory" in software. He posted it here, I think it's excellent reading it really gave me insight into all of our mindsets when it comes to success. It has made me revisit things I consider successes/failures. 

 

After his talk the night developed into a real party of games, fun, and real community. I left feeling like I bonded with current friends, and made some awesome new ones. The event was better than I could have imagined even with all the rain I wouldn't have changed a thing. I will be posting pictures tomorrow after I get them all together. 

 

Thank you to everyone who made this an amazing and unforgettable event. 

 

 

Pictures are up!!! See them here. 



Sunday, September 20, 2009

The XX Files

Hello all, hope your machines are treating you well. Just wanted to blag about some news as it relates to Sara.


Tickets to Concept Camp will be sold up until Weds 9/23. Get them while you still can. We recently got sponsors for Concept Camp as well as some great new speakers. News will be going up on the Concept Camp site. You can also follow ConceptCamp2009 on Twitter.


On October 3rd Microsoft in NY is doing a MVC Firestarter and I've been given the great opportunity to present. This is my largest event to date and I'm really looking forward to sharing some of the MVC goodness. It's at the Grand Hyatt New York and you can register to attend here, also it will be broadcast on the web for you at home. You can learn more about Firestarters in Steve Bohlen's great post here.


I'm Beta testing my latest personal web project. It's a URL Aggregator built in ASP.NET MVC. If you would like an invitation shoot me a line, if you are waiting on one you will see it in your inbox shortly.


This weekend I had the awesome opportunity to go to the xkcd book signing party here in NY. I got to meet Randall Munroe and he signed my netbook. It was nerdtastic and I giggled like a 5th grader the entire time. The book is great, you can buy your copy here.




Sunday, September 13, 2009

ASP.NET MVC Translated for the Web Forms Programmer (4 in a series)


 


Ok, so, do you remember learning about HTTP Get and HTTP Post in school? Ok, do you remember when you started building Web Forms and you just chalked that up to another thing that you learned in school that's not applicable in the real world? Well, guess what. MVC is 100% get and post. Never fear, it's an easy concept. Once you get it you will realize it's truly awesome to be working with the web at its root. 


 


Now, for those of you who's memory is a bit fuzzy http stands for Hyper Text Transfer Protocol. It's the way things are sent from your computer to the server and then back. It's an integral part of what we do, and I appreciate that MVC brings us closer to the action.


tree


Today we shall create one page that does both things, a Get AND a Post. We're going to create a new project for a restaurant named TGIW (this place seriously loves Wednesdays, weird). We've already learned about Views so I went ahead and built that myself. Today we're going to learn about two big things: Rest and populating our views with data.


 


REST stands for Representational State Transfer and it was actually created by one of the inventors of HTTP. In REST calls individual requests are made using URIs like hypertext links. You can make GET or POST requests by using URLs. This is how MVC performs its actions.


 


Now, what we want to do is have our daily specials loaded as the form comes up, and then send back our order. Our application is using LINQ to SQL and we have two tables, “Specials” that contain our daily specials, and “Orders.” which is where our orders go.


 


What we're going to do first is create a ViewModel. MVC is very good at simple CRUD behavior with data objects, however, when our application requires data that is more complex the best way to pass this data into our View is through a ViewModel. Since our view needs to be able to manipulate multiple classes we're going to aggregate them in a ViewModel.


view model


Our ViewModel is called OrdersViewModel, and it includes a constructor that gets our special of the day via by creating a special and passing the day of the week.


 


Now we come to the “Get,” we're going to build an action that returns an ActionResult that populates our page. Inside our action we're calling a method called that accepts a string “day” and returns our special. When we return our View, we're going to pass in that special. We reference the special on our page.


orders.aspx


In our view we're going to make sure that the type passed into “System.Web.Mvc.ViewPage” is our “OrderOnlineViewModel” class, because that's what we're binding our page to. This allows us to reference any of the values in that class. We will display today's special, I'm going to throw it in a div. We use Model.PropertyRefrenced as the syntax.


 


So, next we're going to do our Post. We have our text boxes, where we can input how many meals we want to order (this place has a very limited selection). We're going to create our Post action the same way we did our Get, except this time we will be passing IN a our Order to be saved. Within our post we put all the data we grab all the data collected in our form. There are a few ways to do this. The first step, however, is to make sure all our html controls are named in a way that we can reference in our controller.


 


This brings us to an excellent point that we need to go over, the concept of “Convention Over Configuration.” I don't know if you've noticed but this has been floating around a lot recently. It basically means that it's better to agree on a way that things should be set up and make that the way you do things, than it is to manually configure each time. This concept is exhibited in ASP.NET MVC in the way that controllers have to be named after the folder where the views are contained, and how the values on the view are referenced why whatever the controls are named.


text boxes


For this project we're going to use simple text boxes right now, we'll move onto more involved controls. All we need to do is create a textbox to enter our data, then on our controller we're going to name our action the same thing as our “get,” this time however we're going to specify that it is a post. We did this in our last edition, and now we're doing it the same way. Don't forget to specify that your form has a default action of “Post” and you'll be set, your most method will automatically submit after your click event.


post


Next up: the HTML Helper, and ways you can use it.


 


I apologize for the code images, will embedd code in the future. For now you can download the source here.