You are here

Dynamic is here

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:

    Dynamic willSh = new Dynamic();
    // This guy is still more popular than The Beatles.
    willSh["FirstName"] = "William";
    willSh["LastName"] = "Shakespeare";
    willSh["YearBorn"] = 1564;
    willSh["YearDied"] = 1616;
    willSh["Occupation"] = "Poet";
    db.Store(willSh);

Too easy, right?

Right, you could do it with a usual class, like this:

    public class Author
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int YearBorn { get; set; }
        public int YearDied { get; set; }
        public string Occupation { get; set; }
    }

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:

    Dynamic georgeSand = new Dynamic();
    georgeSand["FirstName"] = "George";
    georgeSand["LastName"] = "Sand";
    georgeSand["YearBorn"] = 1804;
    georgeSand["YearDied"] = 1876;
    georgeSand["Occupation"] = "Writer";
    georgeSand["RealFirstName"] = "Amantine";
    georgeSand["RealLastName"] = "Dudevant";
    db.Store(georgeSand);

By the way, in .NET 4.0 you can use all power of DynamicObject with Eloquera’s Dynamic:

    Dynamic georgeSand = new Dynamic();
    georgeSand.FirstName = "George";
    georgeSand.LastName = "Sand";
    georgeSand.YearBorn = 1804;
    georgeSand.YearDied = 1876;
    georgeSand.Occupation = "Writer";
    georgeSand.RealFirstName = "Amantine";
    georgeSand.RealLastName = "Dudevant";

The effect will be the same as in the example above.

Let’s add another guy to the database:

    Dynamic heywoodJohn = new Dynamic();
    heywoodJohn["FirstName"] = "John";
    heywoodJohn["LastName"] = "Heywood";
    heywoodJohn["YearBorn"] = 1497;
    heywoodJohn["YearDied"] = 1580;
    heywoodJohn["Occupation"] = "Writer";
    db.Store(heywoodJohn);

Righto, we can add all this data. How would we get them back?

Good old SQL is still here:

db.ExecuteQuery("SELECT Dynamic WHERE Occupation = 'Writer'");

That should return all writers (but not poets) from our database. An easy way to check it is to print on the screen:

    var writers = db.ExecuteQuery("SELECT Dynamic WHERE Occupation = 'Writer'") .Cast<Dynamic>().ToList();
    writers.ForEach(x => Console.WriteLine("{0} {1}", x["FirstName"], x["LastName"]));

Seeing is believing – the result is printed as below:

John Heywood
George Sand

LINQ is young but equally mighty:

from Dynamic d in db where d["Occupation"] == "Writer" select d;

Those who prefer lapidary lambdas in LINQ can adhere to their style, too:

db.Query<Dynamic>().Where(d => d["Occupation"] == "Writer");

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:

from Dynamic d in db where d.Occupation == "Writer" select d; // does not compile!

Currently, dynamic objects can be used in queries over enumerations, but not on Queryable objects.

Copyright © 2008-2012 Eloquera Corp. All rights reserved.