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

By Anoop Madhusudanan

Vote on HN

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?

© 2012. All Rights Reserved.