Friday, February 19, 2010

Dynamic fun with C#, XML and the ElasticObject

amazedsaint’s .net journal - Introducing ElasticObject for .NET 4.0 - An easier way to work with XML in CSharp, A Smarter Cousin of ExpandoObject.

“This is an intro post on the ‘ElasticObject’ concept I’ve implemented some time back – 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.

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 ‘!’ XML Conversion Operator

This is what you need to do to generate an XElement representation from the above ‘store’ object. The XML Conversion operator ‘!’ can be used for this. Consider this code,

image

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

image

9 - 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 :).

image …”

I’ve not spent much (cough… any…) time looking at the ExpandoObject (no matter that I love the name ;) but after seeing this article it looks like I’m going to have to spend some quality time with it and variants like the ElasticObject.

I’ve heard how the Dynamic keyword is going to make our XML life’s better, but didn’t understand how much until I saw this… I mean, wow, that’s cool.

1 comment:

Colin said...

I find myself craving the dynamic type alot since i heard of c#4. No it wont dirty things up or destroy type saftey if you use it correctly.

I'm waiting for .net to go into production license though. feels like im wasting my time experiementing before it is "go live" ;)