views:

305

answers:

3

Brief Background:

My team has decided to use Microsoft's Managed Extensibility Framework (MEF) in order to provide an extensible model for adding new "providers" into our system.

This allows for us to plug in new 3rd party providers with relative ease.

Note: I was impressed by how simple MEF was to use and get up and running with.

My Question:

Since these providers commonly have different properties associated with them, when loading these providers into the system at run-time we need to access the providers data streams and properties.

What approach should be taken in order to work with said provider plug-ins due to the differing properties? Noting they all do a similar job.

My Solution:

Create an interface which the providers must conform to, resulting in a "wrapper" being created around each of the 3rd party providers resulting in a consistent interface / programming model for working with each provider.

Plug-in = 3rd party data source (provider) + Common interface implementation.

+ve: No need for a more complex reflection based dynamic "plug" for said plug-ins.

-ve: Have to write a wrapper for each provider. (We need to add the MEF Export tags regardless)

Further Note:

To me the interface / wrapper approach would be the simplest but I have been told to investigate a reflection based approach which may utilize reflection in order to discover the properties at run-time which can be exposed to the system.

I am not in favor of any one solution over another, but I would be interested in hearing the thoughts of the community (most of which are more experienced than I).

Thanks.

+1  A: 

It's not very clear what "properties" and "data streams" are you talking about, but still.

Yes, common interface is always a good thing. And since you have all these "properties" and such, I suggest the following:

interface IProperty
{
    string Name { get; }
    object Value { get; }
}

interface IDataStreamProvider
{
    Stream OpenStream();
}

interface IPlugin
{
    ReadOnlyCollection<IProperty> Properties { get; }

    ReadOnlyCollection<IDataStreamProvider> DataStreams { get; }
}

Speaking of "wrappers": I do not understand the intent of those. All third-party plugins must implement IPlugin interface and must be decorated with either ExportAttribute or PluginAttribute as in this:

class PluginAttribute : ExportAttribute
{
    public PluginAttribute() :
        base(typeof(IPlugin))
    {
    }
}

Reflection should be avoided as much as possible because of maintainability concerns.

Anton Gogolev
+1  A: 

What I have done for adding info like this is to make some custom attributes for the plugins and then read those in with MEF when the plugins get loaded. You can add anything into the attribute class, like names, enums, ints, other strings, and it is pretty easy to use. But be careful, the new preview6 did change a few things in how those are handled.

[MetadataAttribute]
public class MyMetadataAttribute : Attribute
{
    public MyType MyUsage { get; set; }
}

public interface IMyMetadataView
{
    MyType MyUsage { get; }
}

public enum MyType
{
    Undefined,
    TypeOne,
    TypeTwo
}

And then in the plugin you can define it like this...

[Export(typeof(IMyInterface))]
[MyMetadataAttribute(MyUsage = MyType.TypeOne)]
public class PluginClass: IMyInterface
{
}

You need to add things to the import then also

[ImportMany(AllowRecomposition = true)]
public IEnumerable<Lazy<IMyInterface, IMyMetadataView>> plugins { get; set; }

You can then use the data directly for each plugin

var typeOnePlugin = plugins.FirstOrDefault(p => p.Metadata.MyUsage == MyType.TypeOne);

Again this is the way using preview 6 that came out in July.

SteveM
+2  A: 

Actually in Preview 6 we have unsealed export and allow you to create custom export attribute which include metadata thus removing the need for part authors to add a separate export. All of our import attributes are also unsealed.

[MetadataAttribute]
[AttributeUsage(AllowMultiple=false)] 
public class RuleAttribute : ExportAttribute {
  public RuleAttribute(string name, string description) {
    Name=name;
    Description=description;

  } : base(typeof(IRule))

  public string Name {get;private set;}
  public string Description {get; private set;}
}

RuleAttribute above exports IRule, and also allows providing Name metadata.

The usage would then be the following:

[Rule("AddOneRule", "Adds one to the value")]
public class AddOneRule {
}

HTH Glenn

Glenn Block
Minor addition, the RuleAttribute class should also be marked with:[AttributeUsage(AllowMultiple=false)]
Nicholas Blumhardt
Updated, thanks Nick! Gotta love that feature for StackOverflow that lets you edit ;-)
Glenn Block