images Workshop: Getting Started with MEF

Let's look at a very simple scenario in which MEF can be used to “inject” an instance of a class into another class, without the need to explicitly “new up” the class instance. We'll make the class known to MEF by “exporting” it, and “import” it into the view's code-behind class so that it can use it.

  1. Create a new project using the Silverlight Application project template, named Chapter14Sample.
  2. Add a reference to the following MEF assemblies:
    • System.ComponentModel.Composition.dll
    • System.ComponentModel.Composition.Initialization.dll
  3. Add a new class to the project, named PersonPart.cs, containing the following code:
    namespace Chapter14Sample
    {
        public class PersonPart
        {
            public string Name { get; set; }

            public PersonPart()
            {
                Name = "Homer Simpson";
            }
        }
    }

    Essentially, we've just added a property named Name to the class, and assigned it a value in the class's constructor. We'll use that value to demonstrate that an instance of this class has been successfully imported shortly.

  4. We now want to make this class known to MEF. Start by adding the following using statement to the top of the class's file:
    using System.ComponentModel.Composition;
  5. We can now “export” this class to announce to MEF that this is a composable part. Decorate the class with MEF's Export attribute, like so:
    [Export]
    public class PersonPart

    The completed code for the PersonPart class that you should now have is as follows:

    using System.ComponentModel.Composition;

    namespace Chapter14Sample
    {
        [Export]
        public class PersonPart
        {
            public string Name { get; set; }

            public PersonPart()
            {
                Name = "Homer Simpson";
            }
        }
    }
  6. Open the code-behind file for the MainPage.xaml view. Let's “import” the PersonPart class that we just exported into this view, and display the value assigned to its Name property. Start by adding the following using statement to the top of the MainPage.xaml.cs file:
    using System.ComponentModel.Composition;
  7. Add a public property to the MainPage class that MEF will inject a PersonPart object into, like so:
    public PersonPart MyImportedPersonPart { get; set; }
  8. We can now get MEF to assign an instance of the PersonPart class to this property for us by decorating the property with MEF's Import attribute, as follows:
    [Import]
    public PersonPart MyImportedPersonPart { get; set; }
  9. We now need to tell MEF to go ahead and “new up” an instance of the PersonPart class and assign it to this property. You do so using the SatisfyImports method on the CompositionInitializer class. Add the following line of code to the MainPage class's constructor:
    CompositionInitializer.SatisfyImports(this);

    This will result in the parts being “composed” by MEF when the MainPage class is initialized, with an instance of the PersonPart class being assigned to the MyImportedPersonPart property.

  10. Finally, to prove that the PersonPart part has been successfully imported, let's display the value that we assigned to its Name property in its constructor in the view. Add the following code in bold to the MainPage class's constructor:
    public MainPage()
    {
        InitializeComponent();

        CompositionInitializer.SatisfyImports(this);

        var nameTextBlock = new TextBlock();
        nameTextBlock.Text = MyImportedPersonPart.Name;
        LayoutRoot.Children.Add(nameTextBlock);
    }

    This code adds a TextBlock control to the view, and displays the value assigned to the PersonPart part's Name property in the view. The complete code that you should now have for the MainPage class is as follows:

    using System.ComponentModel.Composition;
    using System.Windows.Controls;

    namespace Chapter14Sample
    {
        public partial class MainPage : UserControl
        {
            [Import]
            public PersonPart MyImportedPersonPart { get; set; }

            public MainPage()
            {
                InitializeComponent();

                CompositionInitializer.SatisfyImports(this);

                var nameTextBlock = new TextBlock();
                nameTextBlock.Text = MyImportedPersonPart.Name;
                LayoutRoot.Children.Add(nameTextBlock);
            }
        }
    }
  11. Run the application. The text “Homer Simpson” should appear in the top-left corner of the view.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset