Monthly Archive for July, 2009

Microsoft and the Case of the Missing MVC

As you may know I am an ASP.NET developer, a junior one no doubt, but I’m learning.

The .NET framework is on version 3.5, and there was at least 1 version of ASP before the .NET framework ‘revolutionized’ the way we make websites on/with MS products. But the MVC (model – view – controller) plug-in/framework/whatever is only on version 1. Why is this? And more importantly, why is it not more widely advertised?

I was having some serious issues trying to enforce company ‘best practices’ in my web application. (We are a group of computer programmers, and in theory we should be able to code for the internet using the same methodology). Basically best practices means you need the user-interface (UI) layer, the database (db), the data model, and the business object (BO).

Well as it turns out, MS kinda builds some of that in to ASP.NET Web Forms (the not MVC part is Web Forms) but if you should dare deviate from the MS way at all, nothing works right, from persistence, to sorting, to authorization. And finding information that is timely and useful is very difficult. (The problems I do solve, I try to post here in hopes that the next person who needs that question answered finds it here.) What the MVC framework does is enforce those best practices, by design. All I had to do was add my database and *poof* my 4 layered approach to my project literally falls into place.

Basically the data model is the M, the UI is V, and the BO is.. well the C gets the BO, and tells the V what M to display. In reality the BO is one a layer between the MVC and the db, and the C just returns things like urls, and does some validation. Really the C is the bouncer.

I will go into more detail later on, I just wanted to do my part to get the word out about ASP.NET MVC.

Realizations

I have been my job for about a month now, and I have some observations I would like to share.

Developing using MS-specific languages (and to a lesser extant, tools) sucks. But not for the reasons you think. (Disclaimer: I am a OS X man, and have been for 3 or 4 years now. Prior to that I was a Windows Xp/Linux man.)

It should be noted that, for the duration of the post, at minimum, I will be speaking .NET 3.5/ASP.NET/C#, and I use Visual Studio Pro 2008, and Expressions Studio 2.

Microsoft Visual Studio make developing you application easy. To easy. I can do all kinds of wonderful things so simply that when I run into something that doesn’t work like I expect, or understand it to, it is a bit surprising. (Sorting with GridView for example, is wonderful, if your application read directly from a database, then spits it out on your view. But try sorting the cached data that you really should be viewing, and the nifty automagic sorting goes away.) So off to Google I go. What do I find? Useless forum posts that say something like

All you need to do is..

YourObject.ThisMethod().ThatMethod();

Well that nice, but what the hell are those methods, or (most likely) I am here because those methods don’t work.

I got into the writing the methods to sort the GridView and since I was in a IEnumerable collection that was [Serialized()], I decided I was going to use LinQ to do all the heavy lifting, but I was unsure of the exact syntax I was gong to need to use. So off to Google I go. I discover, through many sources that I can sort the List in place like so..

List YourList = new List();
YourList.OrderBy(YourListItems => YourListItems.Property)//YourListItems is a temporary variable that gets destroyed once the method is done running, it is storage for the individual ListItems as they are being sorted. Property is one of the instance variables in the class itself, that you are using to sort, a la length in string

Like I said supposedly that sorts your list, buy the property you select, in place. It doesn’t. And since there is no documentation along the lines of the Java SE Docs website for C#, finding out why that wasn’t working was fairly harrowing.

Turns out that all you need to do is…

List YourList = new List();
List YourSortedList = null; //Just to make sure the compiler doesn't try to help
YourSortedList = YourList.OrderBy(YourListItems => YourListItems.Property).ToList();

Basically the .OrderBy() method doesn’t return anything. You MUST invoke the .ToList() method, and put the resulting list somewhere if you want use the results.

I am putting this out there for 2 reasons. First Googling for help was not successful, which leads to; Second, now the answer is on the internet for all to find as needed.