Wednesday, December 30, 2009

.NET 4.0 Dynamic with ASP.NET MVC

.NET 4.0 ships with a lot of cool, new stuff. For C# the major new feature is the support of dynamic programming. The idea with dynamic support in C#, in short, is that you can statically type a variable as dynamic (yeah, this sounds funny, statically typed as dynamic, I know) in which case the compiler will not check any methods or property binding on this object, rather it will defer all of these resolutions to runtime.
It's not just that, but with dynamic support you can also declare your own objects to act as dynamic objects by implementing IDynamicMetaObjectProvider or -- as a shortcut -- by extending DynamicObject. Both types exist in the namespace System.Dynamic.
There's also a very famous type called ExpandoObject.
ExpandoObject is a dynamic object that you can simply create properties on it at runtime.
Now the trick that I want to use, is to declare my views to extend the generic System.Web.Mvc.ViewPage. This will allow me to pass to my view any dynamic object and access its properties through the Model property of the view. 
Here's a code example:
 using System.Dynamic;
    using System.Web.Mvc;

    public class ArticlesController : Controller
        public ViewResult Index()
            dynamic x = new ExpandoObject();
            x.Title = "Programming for Cowards";
            x.Url = "";
            //I even can nest
            x.Author = new ExpandoObject();
            x.Author.Name = "Galilyou";
            return View(x);
In the above code I declare an ExpandoObject and start to set some properties on it like Title, Url, etc.
I do this by simply using the syntax var.Property = value; which will automagically create a property on the dynamically created object which type will be the type of value.
If you look closely you would notice that  the Author property is an ExpandoObject itself. This nesting is allowed to as many levels as you want.
Here's how my view looks like:
< %@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

Note, the view is declared to extend System.Web.Mvc.ViewPage
Now you can use the values of the model like so:
here's how the entire view will look like:

And here's the page after running the application:

        You shouldn't do this in a large scale application. View model objects are still the best tool for the       job when the application grows. However, if you want to quirk something pretty quickly -- a small thing-- you might get away with that.
Hope this helps.


Rahul said...


First of all. Thanks very much for your useful post.

I just came across your blog and wanted to drop you a note telling you how impressed I was with the information you have posted here.

Please let me introduce you some info related to this post and I hope that it is useful for .Net community.

There is a good C# resource site, Have alook

blog2100 said...

U really opened a interesting site.
I am also a dot net programmer.

currently my blog is

follow me in my blog then i will to keep in touch.

Shopping guide said...

It is a nice helpful blog.
mirror from

Night Owl Coders said...

Nice post about the Expando object. There are not too many actual useful blogs out there talking about it.

My latest blog post (and following articles) will touch on taking Dynamics past the cookie cutter functionality. Feel free to drop in and say hi!

Muhammad Azeem said...

This is a nice article..
Its very easy to understand ..
And this article is using to learn something about it..

c#,, php tutorial

Thanks a lot..!