Gentle Introduction to MEF–Part One

At the Tampa C# Meetup on August 3rd, I presented this Gentle Introduction to MEF using the same project modified over three steps. This is part one, where I show the application without MEF.


The target program will have the following functionality:

  • Transform text data using a selectable transformation method
  • Generate source data (in case the user is too lazy to type something in)
  • Clear the source and destination text blocks
  • Text generation and transformation must be pluggable strategies for future expansion

Text Generation Interface

This is the interface for my text generators. They exposed property, Name, will return the name of the implementation model. The exposed method, Generate, will return the generate text.

Text Generation Implementation

The text generation method for part one will insert a block of text that was originally generated from the Lorem Ipsum website when a button is clicked. Here is the code.

Transformation Interface

The transformation interface is another simple one. The exposed property returns the name of the implementation. The exposed method takes the source text and returns the transformed text.

Transformation Implementations

I have implemented two different transformers with this sample. The first does a lower case transformation, and second an upper case transformation. Here is the code.

In both cases, these are simple pass through methods to String.ToLower() or String.ToUpper().

Transformation Engine

The glue that ties all this grand functionality together is a composition class creatively titled TransformationEnginer. Here is that code.

This class holds the reference to the single IGenerator we are using, and the collection of ITransformers.

Wiring it All Up

We now have all our functionality, so lets wire it up, but first, lets see what the user interface for so complex a project will be.

Good. Now you know what it will look like, so lets do the set up. Here is the code for the single field we need and the constructor for MainWindow class. I have added line number so I can refer to specific lines below.

private readonly TranformationEngine _tranformationEngine;

public MainWindow()
  _tranformationEngine = new TranformationEngine
      Generator = new LoremIpsumGenerator(),
      Transformers = new ITransformer[] {new LowerCaseTransformer(), new UpperCaseTransformer()}

  cbTransformationOptions.ItemsSource = _tranformationEngine.Transformers;
  cbTransformationOptions.DisplayMemberPath = "Name";
  cbTransformationOptions.SelectedIndex = 0;

Line 4 creates our TransformationEngine, and initializes with the LoremIpsumGenerator, the LowerCaseTransformer, and the UpperCaseTransformer.

Line 11 sets the combo box ItemsSource property to the list of transformers.

Line 12 tells the combo box to use the Name property on the items referred to in the ItemsSource collection.

Line 13 tells the combo box to select the first item in the list.

I have also wired up some events in the XAML to handle the button clicks and we will cover that code in a moment, but at this point, our user interface will display our transformers, allow us to select one.

Handling Events

private void btnGenerate_Click(object sender, RoutedEventArgs e)
  txtSourceData.Text = _tranformationEngine.Generator.Generate();

private void btnClear_Click(object sender, RoutedEventArgs e)
  txtSourceData.Text = txtDestinationData.Text = string.Empty;

private void btnTransform_Click(object sender, RoutedEventArgs e)
  var transformer = cbTransformationOptions.SelectedItem as ITransformer;
  if (transformer != null)
    txtDestinationData.Text = transformer.Transform(txtSourceData.Text);

The three methods above handle the events and control the transformation.

The btnGenerate_Click method calls the Generate method on the generator exposed by the transformation engine, and stores the result in the text property of the source data control.

The btnClear_Click method sets both the source and destination data text properties to string.Empty in order to clear the text blocks.

The btnTransform_Click method finds the selected transformer from the combo box (remember how we set the transformers collections to the ItemsSource property in the constructor?). Then it calls the Transform method, passing in the source data text property, and setting the destination data text property to the return value.


This program is a very contrived example of how many programs implement simple decoupling of functionality, and the selection of an execution strategy (see Strategy Pattern). So where is the MEF? Well that is coming in Part Two. First I wanted to show how the program without all the MEF goodness to come. Stay tuned for the next two parts. In Part Two I will change this program to use MEF to do the wiring up we did in the constructor. In Part Three we will break this program up across multiple assemblies to show how we can assemble the the programs functionality at run time.


comments powered by Disqus