.NET, New releases, Sitecore

Will dynamics in .NET 4 change the way we do Sitecore?

I am spending my time these days walking the halls of Berlin Messe at the Microsoft TechEd 2009. One of the interesting upcoming features of .NET 4 is the introduction of dynamic language support in the .NET Language Runtime. This does not only mean that you can write your .NET code in dynamic languages such as Python (IronPython) and Ruby (IronRuby), but also that you can use some of the dynamic features from C#. With the introduction of the dynamic type keyword, method calls and member access can be evaluated at runtime instead of at compile time (previously known as late-binding). This does not mean that C# is no longer a type safe language, it merely means that the language now provides a more “syntax correct” way of accessing those dynamic types of data, we previously used to access via strings, invokation, reflection etc. Examples of dynamic data models includes COM, the HTML DOM and XML.

Another one of the dynamic types of data we all works with each day, is Sitecore. The structure, i.e. templates and fields, we are coding up against changes constantly, and Sitecore provides a string based way to access specific items and fields and values in fields. Some of these dynamic types are exposed via classes in the Sitecore API, e.g. the fields type classes in the Sitecore.Data.Fields namespace. In Pentia we have a concept we call Sitecore wrapper classes, i.e. classes which we custom write to provide a proper interface to items of a given template. Writing these classes are tedious and potentially error-prone, which is why we are working on code generator for generating these classes (similar to e.g. LINQ to SQL). But with dynamics in .NET 4, we have a way of eliminating these classes completely, while still maintaining a proper syntax – and hopefully Sitecore will implement this to our common advantage. Here is an example of how code from the current version could look in a future version of Sitecore:

//BEFORE: .NET 3.5 Framework, Sitecore 6
public string GetContextItemDocumentImage() {
  Item item = Sitecore.Context.Item;
  ImageField field = ((ImageField)item.Fields["DocumentImage"]);
  return field.Src;

//AFTER .NET 4 Framework, Sitecore 7?
public string GetContextItemDocumentImage()
  dynamic item = Sitecore.Context.Item;
  return item.DocumentImage.Src;

Note that although the syntax differs, the actual functionality executed in both example would be the same. So, what does it require from Sitecore to do this? The secret lies in the System.Dynamics. IDynamicMetaObjectProvider interface or more easily, the DynamicObject class. This interface and class allows the derived class to provide its own resolving to e.g. members access and method calls via a number of virtual methods. The methods are Try based, which means that you should return false if the functionality is not supported, or e.g. a method with an invalid method is called. Some of the most important virtual methods which can be implemented via DynamicObject are:


A cast is attempted, trying to convert the dynamic type to a static.


An indexer is called, trying to get a value with a given id or index.


There is an attempt to get the value of a named member (e.g. a field or property)


A method with a given name and parameters is invoked.


There is an attempt to set the value in an indexer with the given id or index.


There is an attempt to get the value of a named member (e.g. a field or property)


An example of the above Sitecore 7 (:-)) implementation could therefore be:

namespace Sitecore.Data.Items
  public class Item : DynamicObject //...and other interfaces
    public override bool TryGetMember(GetMemberBinder binder, out object result)
      dynamic field = Fields[binder.Name];
      if (field != null) {
        result = field;
        return true;
     	return base.TryGetMember(binder, out result);

Let this be an inspiration to Sitecore for the next version – I for one truly hope we see something like this in the product.


6 thoughts on “Will dynamics in .NET 4 change the way we do Sitecore?

  1. Hello Thomas,

    Thank you for your thought. It’s a very interesting concept indeed. I’m not sure if late-binding and dynamic is capable in all situations Items are used and above all… if it performs well, but it is an interesting thought.

    One thing I’m extremely wondering about is if the dynamic keyword gets broad adoption. So far I’ve to say that I depend a lot on Resharper to get C# 3 and sometimes even C# 2 functionalies working(or even worst: refactor it towards). I’m wondering if the regular developer is able to catch up and understand these capabilities. Late-binding and the whole keyword ‘dynamic’ isn’t an easy topic(even though it looks like that in your post).

    Thanks once more,

    Alex de Groot

  2. Eldblom says:

    Anders: thanks, ill try to keep up the good work;-)
    Lars & Alex: good to hear that we can expect something like this from Sitecore. The point i’m trying to make here is that we are all doing “late-binding” today, specifying fields in strings. The dynamics in .net 4, as described in the example, will not give change the Sitecore code actually running – therefore there will be no performance penalty aside from the one given from using the dynamic keyword. This penalty will most likely be very very minimal, since its mostly about a few extra method calls.
    The cool feature – if implementing IDynamicMetaObjectProvider in the Sitecore Item class – is that the class can be used both as a static class (as today, preserving backwards compatibility) AND as a dynamic type. In my view, it really solves the adoption problem, as it does not force the developer.

  3. I share the worries with Alex that the average developer will not be able to keep up with the new features of .NET. I myself is still new to LINQ, and link is old news now.

    But that souldn’t keep Sitecore from adopting the dynamic language support, as long as you keep the old interfaces as well.
    I am looking forward to seeing a DynamicObject implementation in the next release of Sitecore 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s