Our office has decided this year to take part in the local Christmas Cheer Registry. Families sign up, requesting a food and goods hamper, and then people or organizations attempt to fill all the needs of the community. It really is a great cause.
Merry ASP.NET MVC to You
The MVC framework that is now available for ASP.NET is quite powerful. Last night I was able to create a web-based application for our company where we can input the items, allow our staff to “claim” the items (indicating that they will donate them) and keep track of when the donations have been brought in.
While it’s not by any means fancy, I was able to implement all features in about an hour. After sending out the email to invite others to participate, we nearly filled the list (and it looks like we’re going to adopt another family!).
LINQ to Christmas Chicken
What would otherwise be a chore to develop was made quite simple with LINQ functionality. This is an extremely simple data model, with only two tables. I used an mdf in the App_Data directory rather than a full-blown SQL Server Deployment.
LINQ to SQL starts things off for us nice and easy to give us a simple data access layer. Because the dbml tools end up generating partial classes, I was able to easily extend the model to add properties to the Donations data.
For the purposes of this site, a hamper item is considered ‘claimed’ when a name is assigned to the item. While there is no Boolean value saved in the database, the model (and subsequently the auto-generated Views) is easily adapted to provide this information at the class level. Now, I have an easy way to test for claimed or unclaimed hamper items without having to check for nulls.
MVC – Making it Viable for Christmas
…but also known as Model-View-Controller. It’s one of the oldest recognized patterns but it is also an entirely different direction from the Page-Controller experience we’ve had since the inception of ASP.NET.
Without taking into account the recent addition of Areas in MVC, you can think of the organization in this way: there are three folders, one each for your Models, your Controllers and your Views. The Models are the objects you are working with. The Views are what the user looks at. The Controllers respond to user input, build up the models (or load them) and send them back with the appropriate View.
There will obviously be larger projects in your future, but for ours I knew we’d hit a limit of 8-10 views and therefore just elected to use the default Home controller. I stripped out account management and opted to use the domain name of the user to simplify even further. There is no about page and very few management pages.
Creating the views was almost as simple as creating the LINQ to SQL diagram. Visual Studio 2010 has integrated some editor tooling that picks up on code cues and prompts you for the magic.
To access the Add View wizard you simply right-click in the code editor for your controller. The view name is pulled from the controller method where you clicked – in my case, AddDonation was the name of my method – but you can also customize as you feel is appropriate. For my view I have also asked the wizard to use a template and create a draft of my data class Donation. The template is driven from my View content selection “Create”, which stubs out a form for new records using the fields from my model.
Christmas Cheer in an Hour
There you have it. A total of 1 controller, two classes in my model (one of them the DBML, so it was created with the designer), and a combined 10 views – all of which were largely auto-generated – and we’re well on our way to helping out a family for Christmas. In fact, as I finish this note there are only two items left on the list and almost half of the items have already been brought in.
Though it may be early, this geek wishes you the Merriest of Christmases this year.