views:

116

answers:

4

In my plugin architecture I am currently passing a plugin name (string), method name (string) and parameters (object array) to my plugin service to execute the specified method and return the result (of type T).

The plugin service's execute method can be seen below:

public TResult Execute<TResult>(string pluginName, string operation, params object[] input) {
    MethodInfo method = null;
    TResult result = default(TResult);

    var plugin = _plugins.Enabled().FirstOrDefault(x => x.GetType().Name.Equals(pluginName,  StringComparison.InvariantCultureIgnoreCase));

    if (plugin != null) {
        method = plugin.GetType().GetMethods().FirstOrDefault(x => x.Name == operation);
        if (method != null) {
            result = (TResult)method.Invoke(plugin, input);
        }
    }
    return result;
  }

An example usage:

var url = AppHelper.PluginService.Execute<string>(
    "ImagePlugin",
    "GetImageUrl",
    new object[] { image, size });

What I would rather do is pass in an anonymous type instead (as I think this is more readable) i.e.

var url = AppHelper.PluginService.Execute<string>(
    "ImagePlugin",
    "GetImageUrl",
    new { image = image, targetSize = size });

How would I change my Execute method to map the anonymous type properties to my plugin method parameters?

I had considered using the new dynamic type in .net 4.0 but I prefer to define my parameters on the plugin method rather than accepting one dynamic object.

Thanks Ben

[Update]

After looking through the ASP.NET MVC source code it seems simple enough to pull the anonymous type into an object dictionary e.g. RouteValueDictionary. With the help of reflection a linq expression is created dynamically. Although its a good implementation, I didn't really want all this extra complexity.

As per the comment below, I can achieve readability just by specifying my parameters inline (no need for the object array declaration):

var url = AppHelper.PluginService.Execute<string>("ImagePlugin", "GetImageUrl", image, size);
A: 

I did this once. What you can do is get the parameters expected from the function through reflection. Then, you can build up your array of parameters by matching the names in the array of parameters with the keys of the anonymous object.

Hope that helps :-).

Alxandr
A: 

First of all, check System.Addin namespace, you might get some help there.

Second, you can create an interface of your own with specific method name and parameters, and let the plugin implement the interface. You can define plugin interface in a different project that can be referenced in both application as well as plugin project.

Akash Kava
We are already using interfaces along with StructureMap to call specific plugin types. This is more for plugins that the user may create on-the-fly and want to use within the UI (for example, they may want to swap out the ImagePlugin above for one that pulls images from Amazon S3)
Ben
+3  A: 

There are some ways to make this possible although I wouldn't advice any of them.

First, you can use reflection which means you have to write a lot of additional (error-prone) code in your PluginService.Execute method to get the values you want.

Second, if you know the parameters of the anonymous type you are passing to your method you can use the technique described here. You can cast to another anonymous type inside your method that has the same properties. Here is another description of the same technique from Jon Skeet.

Third, you can use classes from the System.ComponentModel. For example, ASP.NET MVC uses this. It uses reflection under the hood. However, in ASP.NET MVC either the property names are well-known (controller and action for example) or their names don't matter because they are passed as-is to a controller method (id for example).

Ronald Wildenberg
Ronald, thanks for the links. Starting to think that I should stick to my current implementation. However, isn't this something that is used extensively in ASP.NET MVC, for example in HTML helpers and in routing. I'm sure there must be a "good" way of doing this otherwise Microsoft wouldn't have done it :)
Ben
Good point. I updated my answer with another way of doing what you want.
Ronald Wildenberg
@Ronald, I've marked your response as the answer as it was the most complete. However, after looking through the MVC source code to see how they are achieving this, I am going to stick with my implementation but use Necros suggestion to improve readability.
Ben
+1  A: 

I did eventually come across this post that demonstrates using anonymous types as dictionaries. Using this method you could pass the anonymous type as a method parameter (object) and access it's properties.

However, I would also add that after looking into the new dynamic features in .net 4.0 such as the ExpandoObject, it feels much cleaner to pass a dynamic object as a parameter:

        dynamic myobj = new ExpandoObject();
        myobj.FirstName = "John";
        myobj.LastName = "Smith";

        SayHello(myobj);
        ...........

        public static void SayHello(dynamic properties)
        {
           Console.WriteLine(properties.FirstName + " " + properties.LastName);
        }
Ben