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.

image 

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.

image

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

image

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.

image

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.

image

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

image 

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];

image

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.

image

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.

image

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

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…

Hack Raspberry Pi – How To Build Apps In C#, WinForms and ASP.NET Using Mono In Pi

Recently I was doing a bit of R&D related to finding a viable, low cost platform for client nodes. Obviously, I came across Raspberry Pi, and found the same extremely interesting. Now, the missing piece of the puzzle was how to get going using C# and .NET in the Pi. C# is a great language, and there are a lot of C# developers out there in the wild who are interested in the Pi.In this article, I’ll just document my findings so far, and will explain how develop using C# leveraging Mono in a Raspberry Pi. Also, we’ll see how to write few minimal Windows Forms & ASP.NET applications in the Pie as well.Step 1: What is Raspberry Pi?Raspberry Pi is an ARM/Linux box for just ~ $30. It was introduced with a vision to teach basic computer science in schools. How ever, it got a lot of attention from hackers all around the world, as it is an awesome low cost platform to hack and experiment cool ideas as Pi is almost a full fledged computer.  More About R-Pi From Wikipedia.The Raspberry Pi

5 Awesome Learning Resources For Programmers (To help you and your kids to grow the geek neurons)

Happy New Year, this is my first post in 2012. I’ll be sharing few awesome learning resources I’ve bookmarked, and will be pointing out some specific computer/programming related courses I've found interesting from these resources.Also, thought about saving this blog post for my kids as well - instead of investing in these Child education schemes (though they are too small as of today, 2 years and 60 days respectively ). Anyway, personally my new year resolution is to see as much videos from this course collections (assuming I can find some free time in between my regular job && changing my babies diapers).1 – Khan AcademyAs I mentioned some time back, you and your kids are missing some thing huge if you havn’t heard about Khan Academy.  It is an awesome learning resource, especially if you want to re-visit your basics in Math, Science etc.With a library of over 2,600 videos covering everything from arithmetic to physics, finance, and history and 268 practice exercises, th…