ASP.Net MVC Framework leads you to extension method heaven


The first pre-release of new ASP.Net MVC (ahem, Ruby-on-Rails for .Net) framework has just been made public.

I find it really exciting that Scott Guthrie and his team are listening to what the people want. Webforms is really quite heavy, especially in comparison to Ruby on Rails, so by offering new frameworks Microsoft will gain new developers. And new developers equals more servers, so it’s a good business model, methinks.

A major plus for me is seeing how the Framework shows off the new C# 3.0 features. I love Python and Ruby, but with no support yet for Visual Studio, it’s not easy to use them. Until now, the expressiveness of Python and Ruby have been missing from C# – it grew up from Java after all – but C# 3.0 is now moving towards that expressiveness too. With XAML and C#, things have really changed for developers over the past 5 or so years.

For example, check out this post from Rob Conery. He demonstrates an extension method which allows you to create a list of attributes as a string. You just call “ToAttributeList()” on any object, and you get a string back like “field1=”0″ field2=”1″”.

I can already think of loads of places where I would like to use that. There’s already a “ToDictionary()” method in the .Net 3.5 Framework, which can be used to create a dictionary from a list of objects, using a field as the key, but I can create a dictionary directly using the property names as keys, with the objects as values.

public static Dictionary<string, object> ToPropertyHash(this object item)
{
  var props = from property in item.GetType().GetProperties()
              select new {Name=property.Name, Value=property.GetValue(item, null)};

var dict = new Dictionary<string, object>();
foreach(var prop in props)
{
dict.Add(prop.Name, prop.Value);
}

return dict;
}

Here’s testing code which demonstrates what you get.

var myObject = new { Name = “Frank”, Age = 5 };
var dict = myObject.ToPropertyHash();
CollectionAssert.Contains(dict.Keys, “Name”);
CollectionAssert.Contains(dict.Keys, “Age”);
Assert.AreEqual(dict["Name"], “Frank”);
Assert.AreEqual(dict["Age"], 5);

Maybe that’s not so interesting for you, but I am developing Windows Workflow Foundation apps, and a workflow instance always requires you to build a new dictionary with the names of the properties as keys. This could be very, very helpful to cut down on code for that.

Instead of …

Dictionary<string, object> args = new Dictionary<string, object>();
args.Add(“Name”, “Richard Bushnell”);
args.Add(“Age”, 32);
args.Add(“NoOfKids”, 5);

… I use:

var args = new { Name = “Richard Bushnell”, Age = 32, NoOfKids = 5 }.ToPropertyHash();

What I cannot understand yet, is why Microsoft has to copy Ruby On Rails? Don’t get me wrong, I love Rails. But in some places it just doesn’t suit ASP.net development. ASP.net developers already have their ways of working, and have bought controls, etc. Why force them to not use them anymore, if they just want to tidy up their code with a new MVC model? Wouldn’t it be best for everyone if they developed another kind of framework? The focus could be on testing, as with MVC, but more directed towards developers who already use WinForms and WebForms.

Microsoft can develop independently. Take LINQ, for example. The .Net team could have simply provided list comprehensions as implemented in Python, but instead, they came up with a totally new model which no other development framework has ever had. And while XAML is similar to the old Delphi DFM files, it’s still taken some good leaps forward. I don’t see that yet with the ASP.Net MVC Framework. They’re still playing catch-up with Rails.

In general though, I’m really excited to watch it developing.

  • >>Why force them to not use them anymore, if they just want to tidy up their code with a new MVC model<<

    I don’t think MS is forcing anyone to do anything to be honest – in fact Scott struck that tone on his first post about it where he states pretty clearly that MVC is an alternative only.

    The neat thing is that there are now two ways to work up an app on the MS platform, which is pretty groovy :) .

    And it’s only a CTP right now – hopefully in a few months both MVC and SubSonic will have some very groovy features…

  • Hi Rob,

    >>I don’t think MS is forcing anyone to do anything to be honest – in fact Scott struck that tone on his first post about it where he states pretty clearly that MVC is an alternative only.<<

    I looked up Scott’s post. It also says:

    “You can use .ASPX, .ASCX and .MASTER files and ASP.NET AJAX to create your ASP.NET MVC Views. Non-UI features in ASP.NET today like Forms Authentication, Windows Authentication, Membership, Roles, Url Authorization, Caching, Session State, Profiles, Health Monitoring, Configuration, Compilation, Localization, and HttpModules/HttpHandlers all fully support the MVC model.”

    So what are we going to lose by going with the new model, and what is it going to take to teach our developers to use it?

    If implementing MVC really means just separating code which would previously have gone in a Code-Behind file, using it will be a no-brainer. Lose nothing, but gain easier testing.

  • And I liked, will be looking at your site.

  • Hm that sounds good but I would like to know more details.

  • Your news is a cool stuff man, keep it going.

  • That’s good man, keep it going.

  • Good story for me but please more details.

  • Good information to me.

  • Stunning blog and good article. High 5 for u man !

  • That is really very good article.Thanks for sharing this information.

You can follow any responses to this entry through the RSS 2.0 feed.

Trackbacks / Pingbacks