Skip to content

Instantly share code, notes, and snippets.

@dbuksbaum
Created May 2, 2015 23:05
Show Gist options
  • Save dbuksbaum/b005126663b71a4b77df to your computer and use it in GitHub Desktop.
Save dbuksbaum/b005126663b71a4b77df to your computer and use it in GitHub Desktop.
Gentle Introduction to MEF–Part Three
<Application x:Class="RealMef.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
[InheritedExport(typeof(IGenerator))]
public interface IGenerator
{
string Name { get; }
string Generate();
}
[InheritedExport(typeof(ITransformer))]
public interface ITransformer
{
string Name { get; }
string Transform(string text);
}
<Window x:Class="RealMef.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="531" Width="979">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".40*"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width=".40*"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0" Grid.Column="0" Orientation="Horizontal">
<TextBlock Name="txtGeneratorType" HorizontalAlignment="Left"/>
<Button Name="btnGenerate" Click="btnGenerate_Click" HorizontalAlignment="Right">Generate Source Data</Button>
</StackPanel>
<TextBox Grid.Row="1" Grid.Column="0" Name="txtSourceData" TextWrapping="Wrap"/>
<StackPanel Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" Margin="5,0">
<ComboBox Name="cbTransformationOptions"/>
<Button Name="btnTransform" Click="btnTransform_Click">Transform</Button>
<Button Name="btnClear" Click="btnClear_Click">Clear Boxes</Button>
</StackPanel>
<TextBlock Grid.Row="1" Grid.Column="2" Name="txtDestinationData" TextWrapping="Wrap" Background="LightGray" Foreground="Blue"/>
</Grid>
</Window>
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private readonly TranformationEngine _tranformationEngine;
public MainWindow()
{
// bootstrap MEF
// search the named assembly
//using (var catalog = new AssemblyCatalog(Assembly.LoadFile(System.IO.Path.GetFullPath("TransformationImplementation.dll"))))
// search the directory
using (var catalog = new DirectoryCatalog("."))
{
var container = new CompositionContainer(catalog);
_tranformationEngine = new TranformationEngine();
container.SatisfyImportsOnce(_tranformationEngine);
}
InitializeComponent();
txtGeneratorType.Text = _tranformationEngine.Generator.Name;
cbTransformationOptions.ItemsSource = _tranformationEngine.Transformers;
cbTransformationOptions.DisplayMemberPath = "Name";
cbTransformationOptions.SelectedIndex = 0;
}
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);
}
}
}
public class ReverseTransformer : ITransformer
{
#region Implementation of ITransformer
public string Name
{
get { return "Reverse Transformer"; }
}
public string Transform(string text)
{
return new string(text.Reverse().ToArray());
}
#endregion
}
[Export("StarWars", typeof(IGenerator))]
public class StarWarsTextGenerator : IGenerator
{
#region Implementation of IGenerator
public string Name
{
get { return "Star Wars Text Generator"; }
}
public string Generate()
{
return
"Don't underestimate the Force. I suggest you try it again, Luke. This time, let go your conscious self and act on instinct. Look, I can take you as far as Anchorhead. You can get a transport there to Mos Eisley or wherever you're going. Partially, but it also obeys your commands. No! Alderaan is peaceful. We have no weapons. You can't possibly" +
"You're all clear, kid. Let's blow this thing and go home! I find your lack of faith disturbing. The more you tighten your grip, Tarkin, the more star systems will slip through your fingers. What?! As you wish. I care. So, what do you think of her, Han?" +
"Hey, Luke! May the Force be with you. Obi-Wan is here. The Force is with him. I can't get involved! I've got work to do! It's not that I like the Empire, I hate it, but there's nothing I can do about it right now. It's such a long way from here. I have traced the Rebel spies to her. Now she is my only link to finding their secret base." +
"I'm surprised you had the courage to take the responsibility yourself. The Force is strong with this one. I have you now. But with the blast shield down, I can't even see! How am I supposed to fight? Ye-ha! Still, she's got a lot of spirit. I don't know, what do you think? Dantooine. They're on Dantooine." +
"I don't know what you're talking about. I am a member of the Imperial Senate on a diplomatic mission to Alderaan-- I need your help, Luke. She needs your help. I'm getting too old for this sort of thing. Oh God, my uncle. How am I ever gonna explain this? I call it luck. Remember, a Jedi can feel the Force flowing through him.";
}
#endregion
}
public class TranformationEngine
{
[Import(typeof(IGenerator))]
public IGenerator Generator { get; set; }
[ImportMany(typeof(ITransformer))]
public IEnumerable<ITransformer> Transformers { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment