Skip to main content

Introducing ElasticObject implemented using .NET 4.0 dynamic features - An easier way to work with XML in CSharp, A Smarter Cousin of ExpandoObject.

This is an intro post on the ‘ElasticObject’ class. First of all, let us see what exactly the ElasticObject implementation is capable off. After that we might touch the implementation details. To start with, here are few scenarios you can use ElasticObject

  • An easier, fluid way to work with data formats – like XML and JSON. Presently, we’ve some support for XML.
  • Cleaner code though it is duck typed
  • A hierarchical way to maintain loosely typed data.

Note: A Quick Clarification after reading few tweets - ElasticObject is not a class in .NET BCL, it is something that I’ve implemented, and you can get the code by clicking the above link :)

1 – You can create multi level Dynamic objects automatically, like this

ElasticObject supports creating hierarchical dynamic data structures. For example, consider this code.


2 – Then, you can simply convert that to XML using the ‘>’ Conversion Operator

In ElasticObject implementation, several operators are having special meaning. For example, the ‘>’ operator can be used to convert an ElasticObject instance to another data format. Now, this is what you need to do to generate an XElement representation from the above ‘store’ object.


And this is what you’ll see if you check the generated XML.


3 – You can convert back and forth between XML and ElasticObject

In the above example, we’ve seen how to convert from a ‘dynamic’ ElasticObject to XML. Infact, you can convert XML directly to a ‘dynamic’ ElasticObject, using the XElement.ToElastic() extension method in DynamicExtensions class. See this unit test for getting a better idea.


4 – You can add multiple elements with same name, as child elements.

Assume you want to add multiple products. Consider the following code, especially see how we are adding multiple individual products to the Products collection.


And if you have a look at the XML representation, you’ll see


Also, notice that you can assign some text content to a node using the <<= operator.

5 – You can use ‘<<’ or ElementAdder operator to add ‘Named’ elements

An ElasticObject has child elements and attributes. You can use the Left shift operator to add child elements to an object.  The ‘<<’ or Element Addition operation will return the newly added ElasticObject. And you can use the Indexer to get a list of child elements with that name – see how we are calling myobj[“Item”]. To get a list of all child elements, you can pass a null as the index parameter. eg: var allChildren=myobj[null];


6 – You can use ‘<’ or AttributeAdder operator to add ‘Named’ attributes

You can use the ‘<’ operator to add attributes to an ElasticObject. The ‘<’ or Attribute addition operation will return the newly added attribute ElasticObject.

Also, the <<= operator can be used to assign some value directly to an attribute variable.


7 - And Finally, A Quick Twitter Timeline Viewer

Let us create a quick Twitter console app, that loads my time line (oh, by the way, follow me @amazedsaint in twitter for sure) and prints it. Here we go.


What we do here is pretty simple. Grab the XML, convert that to an ElasticObject, and get the status elements under root to print out the user’s screen name and text.

If you are wondering what is that ‘tilde’ character (~) before screen_name and text – screen_name and text are elements in xml, and ‘~’ will fetch you the content of those elements. You don’t need that if screen_name and text were attributes rather than elements. Have a look at the screen_name and text elements in Twitter xml feed.

8 – Don’t forget to see the Unit Tests

Finally, Have a look at DynamicExtensionsTest – you’ll find couple of interesting Unit Tests there to help you understand the concepts further. The entire source code is attached that covers the above scenarios (that contains all the given examples), see the code download link above. In this post, I’m just discussing what ElasticObject can do, and I’m not really going to the actual implementation of ElasticObject. That is for another blog post.

Note: Already posted one, see A 10 Minute Twitter Search App in ASP.NET MVC and ElasticObject Dynamic View Models

9 – Violating C# Norms

ElasticObject violates few so called ‘standard’ statically typed C# norms. For example, These are few special meaning operators for ElasticObject, to note.

  • ‘>’ or Data Converter Operator – To convert a dynamic ElasticObject instance to another format. Presently XML is supported – See step 2 above
  • ‘<<’ Element Adder Operator – To add an ‘element’ or ‘child’ to an existing instance – See step 5 above
  • ‘<’ Attribute Adder Operator – To add an ‘attribute’ or ‘property’ to an existing instance – See step 6 above
  • ‘~’ Content Getter – To get the internal value of an element – See step 7 above.
  • ‘<<=’ Content Assigner – To set the internal value of an element – See step 4 above

Also, a dynamic instance of ElasticObject is not expected to handle any method calls. Any thing of the format obj.Something() will just add a new child named Something to obj – much like what the ‘<<’ operator does. You may or may not like it, bear with me. A better discussion point might be how these features are implemented :).

10 - Tail End

I hope ElasticObject may evolve as a ‘smarter cousin’ of .NET 4.0 ExpandoObject in the long run :). I blogged about ExpandoObject class few months back - and showed how to implement a minimal ExpandoObject like class. Read that if you are interested.

Also here is a list of few interesting blog posts, to understand the ‘dynamic’ keyword, and ExpandoObject if you want to refresh.

As with most other posts on C# 4.0 dynamic features, this post might break out another argument thread about whether we really need (or don’t need) dynamic features in C#. I think those kinds of arguments are pretty out dated, and dynamic features has its own benefits :).

Stay tuned, more is coming regarding the practical uses of this!! Keep in touch, subscribe to my blog. Btw, if you are a .NET guy, have you read my post on Top 5 Mistakes .NET developers must avoid?

Popular posts from this blog

MVVM - Binding Multiple Radio Buttons To a single Enum Property in WPF

I had a property in my View Model, of an Enum type, and wanted to bind multiple radio buttons to this.

Firstly, I wrote a simple Enum to Bool converter, like this.

public class EnumToBoolConverter : IValueConverter { #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (parameter.Equals(value)) return true; else return false; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return parameter; } #endregion }

And my enumeration is like

public enum CompanyTypes { Type1Comp, Type2Comp, Type3Comp } Now, in my XAML, I provided the enumeration as the ConverterParameter, of the Converter we wrote earlier, like

Creating a quick Todo listing app on Windows using IIS7, Node.js and Mongodb

As I mentioned in my last post, more and more organizations are leaning towards Web Oriented Architecture (WOA) which are highly scalable. If you were exploring cool, scalable options to build highly performing web applications, you know what Node.js is for.After following the recent post from Scott Hanselman, I was up and running quickly with Node.js. In this post, I’ll explain step by step how I’ve setup Node.js and Mongodb to create a simple Todo listing application.Setting up Node.jsThis is what I’ve done.1 – Goto, scroll down and download node.exe for Windows, and place it in your c:\node folder2 – Goto IIS Node project in Git at, download the correct ‘retail’ link of IIS Node zip file (I downloaded the already built retail package, otherwise you can download and build from the source).3 – Extract the zip file some where, and run the install.bat or install_iisexpress.bat depending on your IIS Version. If you don’t have IIS in…

Top 7 Coding Standards & Guideline Documents For C#/.NET Developers

Some time back, I collated a list of 7 Must Read, Free EBooks for .NET Developers, and a lot of people found it useful. So, I thought about putting together a list of Coding Standard guidelines/checklists for .NET /C# developers as well.As you may already know, it is easy to come up with a document - the key is in implementing these standards in your organization, through methods like internal trainings, Peer Reviews, Check in policies, Automated code review tools etc. You can have a look at FxCop and/or StyleCop for automating the review process to some extent, and can customize the rules based on your requirements.Anyway, here is a list of some good Coding Standard Documents. They are useful not just from a review perspective - going through these documents can definitely help you and me to iron out few hidden glitches we might have in the programming portion of our brain. So, here we go, the listing is not in any specific order.1 – IDesign C# Coding StandardsIDesign C# coding stand…