In the version 3.0 of the Eloquera Database we have introduced a new functionality that we call dynamic objects. From the point of view of the documentation, it is a minute change – just a new class called Dynamic in the Eloquera.Client namespace. But it adds new magic to the developer’s treasury of tricks.
Those familiar with .NET 4.0 new features, may recall there are 2 entities in C# 4.0, bearing the name ‘dynamic’. The first one is a new keyword dynamic that makes the compiler to skip any checks for methods during the compile-time. In all other aspects, dynamic is synonymous with the object (or Object in non-C#-world) variable type.
The second one is the DynamicObject class that lets developers have their properties and methods be created in the run time. In conjunction with the dynamic keyword, DynamicObject’s descendants can present a truly dynamic way of development (and also a few hours of headache to a poor guy trying to read someone’s deliriously dynamic code where any method and property can be called).
The Dynamic class from the Eloquera DB client library allows creating dynamic objects (as containers) in both .NET 3.5 and .NET 4.0. In the .NET 4.0 Dynamic behaves like a true DynamicObject – in no small measure due to its origin from the latter.
The simplest way to use Dynamic variables in .NET 3.5 is to use them as the key/value storage objects:
Too easy, right?
Right, you could do it with a usual class, like this:
The special thing about this Dynamic class is that you can add any properties of the basic types like int, bool, string, DateTime, Guid, and arrays of such types. And if another instance of your data needs more (or less) properties to describe the entity, you don’t need to extent your class or leave unnecessary properties/fields empty:
By the way, in .NET 4.0 you can use all power of DynamicObject with Eloquera’s Dynamic:
The effect will be the same as in the example above.
Let’s add another guy to the database:
Righto, we can add all this data. How would we get them back?
Good old SQL is still here:
That should return all writers (but not poets) from our database. An easy way to check it is to print on the screen:
Seeing is believing – the result is printed as below:
LINQ is young but equally mighty:
Those who prefer lapidary lambdas in LINQ can adhere to their style, too:
Unfortunately, dynamic objects of .NET 4.0 cannot be used in expression trees, and therefore LINQ queries cannot enjoy the same compact style as with the assignments. The following code doesn’t even compile in the Visual Studio 2010:
Currently, dynamic objects can be used in queries over enumerations, but not on Queryable objects.
Copyright © 2008-2012 Eloquera Corp. All rights reserved.