jeudi 24 septembre 2015

Creating dynamic proxy class from "object representation"

Is it possible to create an object model representation of an actual class that can be turned into a real class again which fits the signature of the origin for using it as some sort of dynamic proxy?

Currently, I've got a plugin API / interfaces inside my application which looks like this (simplified dummy):

public interface IPlugin {
    void Initialize();
    void SendData(DataObject data);
}

A DataObject could look like this:

public class DataObject {
    public string Value1 { get; set; }
    public string Value2 { get; set; }
}

With this interface, I can send whatever kind of data I like to a plugin, however it has a few drawbacks:

  • It's not really extensible
  • The plugin needs to manually parse/interpret the data
  • I can send complete gibberish to the plugin
  • I need an additional specification to specifiy how the data has to look like.

What I'd like to have is a plugin interface that could be extended like this:

public interface IPlugin :
    ICanReceive<ArbitaryClass1>,
    ICanReceive<ArbitaryClass2>
{
    // implements void Receive(ArbitaryClass1 value);
    // implements void Receive(ArbtiaryClass2 value);
}

The plugin would provide the classes itself without having the main application to know about these types. In order to make this work, I thought about the following:

  • Reflect the generic parameters of the interfaces the plugin implements
  • Map the types into a "object model", which could look like this:

Example

interface IType {
    + TypeKind (e.g. primitive, structured, reference, collection)
}

interface IStructuredType : IType {
    + collection of <IPropertyType>
    + StructuredTypeKind (e.g. complex, entity)
}

  • Persist the object model in a database for value editing
  • Upon sending the data to the plugin, create proxy classes out of the object model with the same signature that was originally reflected from the types, thus matching the methods of ICanReceive<'T>

My question is: How could such a thing work (especially the dynamic proxy stuff)? How could this be done? Reflection.Emit? CodeDOM? There are already certain standards to represent an object model, such as EDM (used in EntityFramework, OData) and AFAIK, Entity Framework already returns dynamic proxy objects for stuff like lazy loading when accessing entities from a DbSet<'T>, so there must be a way to do this. :)





Aucun commentaire:

Enregistrer un commentaire