Click here to Skip to main content
15,868,419 members
Articles / All Topics

A Peek at LinFu.DynamicObject

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
19 Jan 2018LGPL33 min read 5.1K   3
Although it's been quite a while since my last post, LinFu has been coming along quite nicely with some recent additions. In the past month, I've managed to create two new things for LinFu: A Dynamic Typing System for LinFu.DynamicObject, and.

Although it's been quite a while since my last post, LinFu has been coming along quite nicely with some recent additions. In the past month, I've managed to create two new things for LinFu:
 

  • A Dynamic Typing System for LinFu.DynamicObject, and
  • A MyXaml clone that will deserialize the DynamicModel from disk and load it into memory.

The Current State of Affairs

In LinFu's current state, you can only dynamically add properties and methods to DynamicObjects on a per-instance basis. Currently, there's no way to create a prototype for an entire set of DynamicObject instances and have all of those instances share the same conceptual prototype. For example, suppose that I wanted to create a Person type that looks something like this:

public interface IPerson
{
string Name { get; set; }
int Age { get; set; }
}

In order to have a set of dynamic objects emulate a IPerson instance, I would have to manually generate a Name and an Age property on each dynamic object that I needed to use. While such a scenario might be acceptable if I only needed to use a few DynamicObjects, the story starts to become quite different if that small few suddenly becomes a few hundred thousand. Suffice to say, there's quite a cost in having to set up a couple hundred thousand dynamic objects in memory at runtime. There must be a better way to do this, and thus, LinFu.DynamicModel was born.

Build Your Own Model, All at Runtime

LinFu.DynamicModel allows you to create an in-memory type specification (or prototype) of what you want your DynamicObjects to look like. You can add or remove methods and properties to this specification at runtime, all without having to create a single DynamicObject. Any DynamicObject that is attached to that type specification will effectively have all the properties and methods of that spec. The best part about all this is that all changes you make to a spec at runtime will be reflected in every single DynamicObject that uses that type specification.

For example, let's suppose that I wanted to create an implementation of the IPerson interface and have two DynamicObjects share the same conceptual Person type. Here's how it would look like:

DynamicObject firstPerson = new DynamicObject();
DynamicObject secondPerson = new DynamicObject();

// Both of these LooksLike() calls will return false
// since IPerson has yet to be implemented
Console.WriteLine("Object #1: IsPerson? {0}", firstPerson.LooksLike<iperson>());
Console.WriteLine("Object #2: IsPerson? {0}", secondPerson.LooksLike<iperson>());

// Create a person type with only the Name property implemented
TypeSpec personSpec = new TypeSpec();
personSpec.AddProperty("Name", typeof(string));


DynamicType personType = new DynamicType(personSpec);

// Make both DynamicObjects act like the person type
firstPerson += personType;
secondPerson += personType;

// Note: This still will return false since we haven't implemented the Age property
Console.WriteLine("Object #1: IsPerson? {0}", firstPerson.LooksLike<iperson>());
Console.WriteLine("Object #2: IsPerson? {0}", secondPerson.LooksLike<iperson>());

// Finally, implement the age property so that both types look like an IPerson
personSpec.AddProperty("Age", typeof(int));


// Note: Both DynamicObject instance calls to LooksLike() will now return true
// since the type they refer to now implements IPerson
Console.WriteLine("Object #1: IsPerson? {0}", firstPerson.LooksLike<iperson>());
Console.WriteLine("Object #2: IsPerson? {0}", secondPerson.LooksLike<iperson>());

// Use both instances normally as IPerson instances...
IPerson first = firstPerson.CreateDuck<iperson>();
IPerson second = secondPerson.CreateDuck<iperson>();

// ...

As you probably noticed from the example above, the only thing I needed to do was modify the TypeSpec instance and both DynamicObjects automatically changed their behavior to match the given TypeSpec. In theory, this will allow you to effectively generate an entire object model in memory without having to recompile the application. In fact, the only thing you would need at this point is a reliable way to deserialize the object model from disk. At first, using the .NET BCL's serialization mechanisms might be the easiest way to do this, but LinFu.DynamicModel's metamodel heavily relies on interfaces to provide each method and property implementation, and those interfaces cannot be serialized to disk by the classes in the System.Runtime.Serialization namespace.

Legally Infeasible

Now, since MyXaml is very good at instantiating object graphs, and LinFu's DynamicModel is nothing but a metamodel object graph, the next logical step would be to use MyXaml, but there's just one problem--MyXaml is licensed under the GPL, not the LGPL, which means that MyXaml can't be used in commercial applications unless you obtain a commercial license from Marc Clifton, or you publish the source code to your own commercial applications. Since I'm committed to making all of LinFu available under the LGPL, I had no choice but to write my own MyXaml engine from scratch, and hopefully, I can cover that in my next blog post. For now, all I can say about my unnamed MyXaml clone is that it's some of the best code that I've ever written, and this one is definitely worth the wait.

In the meantime, this should whet our appetite for what's to come in LinFu, and the future looks bright indeed.

Stay tuned!

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) Readify
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNeeds formatting Pin
Richard MacCutchan23-Jan-18 21:54
mveRichard MacCutchan23-Jan-18 21:54 
QuestionYou should have asked :) Pin
Marc Clifton20-Jan-18 6:52
mvaMarc Clifton20-Jan-18 6:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.