Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use MEF to manage interdependent modules?

I found this question difficult to express (particularly in title form), so please bear with me.

I have an application that I am continually modifying to do different things. It seems like MEF might be a good way to manage the different pieces of functionality. Broadly speaking, there are three sections of the application that form a pipeline of sorts:

  1. Acquisition
  2. Transformation
  3. Expression

In it's simplest form, I can express each of these stages as an interface (IAcquisition etc). The problems start when I want to use acquisition components that provides richer data than standard. I want to design modules that use this richer data, but I can't rely on it being there.

I could, of course, add all of the data to the interface specification. I could deal with poorer data sources by throwing an exception or returning a null value. This seems a long way from ideal.

I'd prefer to do the MEF binding in three stages, such that modules are offered to the user only if they are compatible with those selected previously.

So my question: Can I specify metadata which restricts the set of available imports?

An example:

Acquision1 offers BasicData only

Acquision2 offers BasicData and AdvancedData

Transformation1 requires BasicData

Transformation2 requires BasicData and AdvancedData

Acquisition module is selected first.

If Acquisition1 is selected, don't offer Transformation 2, otherwise offer both.

Is this possible? If so, how?

like image 780
Tom Wright Avatar asked Sep 13 '12 16:09

Tom Wright


1 Answers

Your question suggests a structure like this:

public class BasicData
{
    public string Basic { get; set; } // example data
}

public class AdvancedData : BasicData
{
    public string Advanced { get; set; } // example data
}

Now you have your acquisition, transformation and expression components. You want to be able to deal with different kinds of data, so they're generic:

public interface IAcquisition<out TDataKind>
{
    TDataKind Acquire();
}

public interface ITransformation<TDataKind>
{
    TDataKind Transform(TDataKind data);
}

public interface IExpression<in TDataKind>
{
    void Express(TDataKind data);
}

And now you want to build a pipeline out of them that looks like this:

IExpression.Express(ITransformation.Transform(IAcquisition.Acquire));

So let's start building a pipeline builder:

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.Linq;
using System.Linq.Expressions;

// namespace ...

public static class PipelineBuidler
{
    private static readonly string AcquisitionIdentity =
        AttributedModelServices.GetTypeIdentity(typeof(IAcquisition<>));
    private static readonly string TransformationIdentity =
        AttributedModelServices.GetTypeIdentity(typeof(ITransformation<>));
    private static readonly string ExpressionIdentity =
        AttributedModelServices.GetTypeIdentity(typeof(IExpression<>));

    public static Action BuildPipeline(ComposablePartCatalog catalog,
        Func<IEnumerable<string>, int> acquisitionSelector,
        Func<IEnumerable<string>, int> transformationSelector,
        Func<IEnumerable<string>, int> expressionSelector)
    {
        var container = new CompositionContainer(catalog);

The class holds MEF type identities for your three contract interfaces. We'll need those later to identify the correct exports. Our BuildPipeline method returns an Action. That is going to be the pipeline, so we can just do pipeline(). It takes a ComposablePartCatalog and three Funcs (to select an export). That way, we can keep all the dirty work inside this class. Then we start by creating a CompositionContainer.

Now we have to build ImportDefinitions, first for the acquisition component:

        var aImportDef = new ImportDefinition(def => (def.ContractName == AcquisitionIdentity), null, ImportCardinality.ZeroOrMore, true, false);

This ImportDefinition simply filters out all exports of the IAcquisition<> interface. Now we can give it to the container:

        var aExports = container.GetExports(aImportDef).ToArray();

aExports now holds all IAcquisition<> exports in the catalog. So let's run the selector on this:

        var selectedAExport = aExports[acquisitionSelector(aExports.Select(export => export.Metadata["Name"] as string))];

And there we have our acquisition component:

        var acquisition = selectedAExport.Value;
        var acquisitionDataKind = (Type)selectedAExport.Metadata["DataKind"];

Now we're going to do the same for the transformation and the expression components, but with one slight difference: The ImportDefinition is going to ensure that each component can handle the output of the previous component.

        var tImportDef = new ImportDefinition(def => (def.ContractName == TransformationIdentity) && ((Type)def.Metadata["DataKind"]).IsAssignableFrom(acquisitionDataKind),
            null, ImportCardinality.ZeroOrMore, true, false);
        var tExports = container.GetExports(tImportDef).ToArray();
        var selectedTExport = tExports[transformationSelector(tExports.Select(export => export.Metadata["Name"] as string))];

        var transformation = selectedTExport.Value;
        var transformationDataKind = (Type)selectedTExport.Metadata["DataKind"];

        var eImportDef = new ImportDefinition(def => (def.ContractName == ExpressionIdentity) && ((Type)def.Metadata["DataKind"]).IsAssignableFrom(transformationDataKind),
            null, ImportCardinality.ZeroOrMore, true, false);
        var eExports = container.GetExports(eImportDef).ToArray();
        var selectedEExport = eExports[expressionSelector(eExports.Select(export => export.Metadata["Name"] as string))];

        var expression = selectedEExport.Value;
        var expressionDataKind = (Type)selectedEExport.Metadata["DataKind"];

And now we can wire it all up in an expression tree:

        var acquired = Expression.Call(Expression.Constant(acquisition), typeof(IAcquisition<>).MakeGenericType(acquisitionDataKind).GetMethod("Acquire"));
        var transformed = Expression.Call(Expression.Constant(transformation), typeof(ITransformation<>).MakeGenericType(transformationDataKind).GetMethod("Transform"), acquired);
        var expressed = Expression.Call(Expression.Constant(expression), typeof(IExpression<>).MakeGenericType(expressionDataKind).GetMethod("Express"), transformed);
        return Expression.Lambda<Action>(expressed).Compile();
    }
}

And that's it! A simple example application would look like this:

[Export(typeof(IAcquisition<>))]
[ExportMetadata("DataKind", typeof(BasicData))]
[ExportMetadata("Name", "Basic acquisition")]
public class Acquisition1 : IAcquisition<BasicData>
{
    public BasicData Acquire()
    {
        return new BasicData { Basic = "Acquisition1" };
    }
}

[Export(typeof(IAcquisition<>))]
[ExportMetadata("DataKind", typeof(AdvancedData))]
[ExportMetadata("Name", "Advanced acquisition")]
public class Acquisition2 : IAcquisition<AdvancedData>
{
    public AdvancedData Acquire()
    {
        return new AdvancedData { Advanced = "Acquisition2A", Basic = "Acquisition2B" };
    }
}

[Export(typeof(ITransformation<>))]
[ExportMetadata("DataKind", typeof(BasicData))]
[ExportMetadata("Name", "Basic transformation")]
public class Transformation1 : ITransformation<BasicData>
{
    public BasicData Transform(BasicData data)
    {
        data.Basic += " - Transformed1";
        return data;
    }
}

[Export(typeof(ITransformation<>))]
[ExportMetadata("DataKind", typeof(AdvancedData))]
[ExportMetadata("Name", "Advanced transformation")]
public class Transformation2 : ITransformation<AdvancedData>
{
    public AdvancedData Transform(AdvancedData data)
    {
        data.Basic += " - Transformed2";
        data.Advanced += " - Transformed2";
        return data;
    }
}

[Export(typeof(IExpression<>))]
[ExportMetadata("DataKind", typeof(BasicData))]
[ExportMetadata("Name", "Basic expression")]
public class Expression1 : IExpression<BasicData>
{
    public void Express(BasicData data)
    {
        Console.WriteLine("Expression1: {0}", data.Basic);
    }
}

[Export(typeof(IExpression<>))]
[ExportMetadata("DataKind", typeof(AdvancedData))]
[ExportMetadata("Name", "Advanced expression")]
public class Expression2 : IExpression<AdvancedData>
{
    public void Express(AdvancedData data)
    {
        Console.WriteLine("Expression2: ({0}) - ({1})", data.Basic, data.Advanced);
    }
}


class Program
{
    static void Main(string[] args)
    {
        var pipeline = PipelineBuidler.BuildPipeline(new AssemblyCatalog(typeof(Program).Assembly), StringSelector, StringSelector, StringSelector);
        pipeline();
    }

    static int StringSelector(IEnumerable<string> strings)
    {
        int i = 0;
        foreach (var item in strings)
            Console.WriteLine("[{0}] {1}", i++, item);
        return int.Parse(Console.ReadLine());
    }
}
like image 148
main-- Avatar answered Nov 05 '22 14:11

main--