MEF & Silverlight 4 Beta - Part 3, Catalogs

Sign in to queue

The Discussion

  • User profile image
    samcov

    Mike, this one was the eye opener for me.  Not so much about MEF, but on a whole raft of technologies coming out of Microsoft.

     

    MEF, Unity, and a few other things are ALL solutions built around this simple concept... type catalogs!

     

    While they all sit on this base, they set out to achieve different things, and solve problems in their particular domain.

     

    At first my biggest confusion was which product to use, since MEF and Unity solved an overlapping set of problems, but I didn't see the commonality, which is they use type catalogs to provide a mapping of types to be delivered anywhere in your application.  Of course that's a simplified description of what they do, but at least I've resolved why they appear the same(at least in my warped mind).

     

    Thanks!

  • User profile image
    Soviut

    Excellent video.  The flow is nice and brisk, all the examples are set up well and I never found myself getting bored.  Kudos for the additional visual materials like the notepad titling.  It does a far better job of keeping the viewer's attention than staring at the desktop the whole time.

  • User profile image
    Aaron1

    Really great job, Mike, thanks!  I really enjoy how you dig into the framework so the average developer is educated and not blinded by the abstraction.  I really enjoy watching all of your presentations and look forward to seeing more!

  • User profile image
    Thundar Viking

    The code in this video is mostly working but the last part I can't get to work. There are some changes in the MEF libraries, so for example - PartInitializer is renamed to CompositionInitializer.

     

    Here is the code I use, I could not comment out CompositionInitializer.SatisfyImports(this).

     

    public partial class MainPage : UserControl
        {
            [Import]
            public IWordProcessor WordProcessor { get; set; }

            
            public MainPage()
            {
                InitializeComponent();
                InitializeContainer();
                CompositionInitializer.SatisfyImports(this);
            }

            private void InitializeContainer()
            {
                //TypeCatalog catalog = new TypeCatalog(
                //    typeof(WordProcessor), typeof(TextEngine));
                //AssemblyCatalog catalog1 = new AssemblyCatalog(typeof(TextEngine).Assembly);
                //AssemblyCatalog catalog2 = new AssemblyCatalog(typeof(WordProcessor).Assembly);
                //AggregateCatalog catalog = new AggregateCatalog(catalog1, catalog2);

                AggregateCatalog catalog = new AggregateCatalog();
                foreach (var assemblyPart in Deployment.Current.Parts)
                {
                    StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri(assemblyPart.Source, UriKind.Relative));
                    Assembly assembly = assemblyPart.Load(streamInfo.Stream);
                    catalog.Catalogs.Add(new AssemblyCatalog(assembly));
                }
                CompositionContainer container = new CompositionContainer(catalog);
                CompositionHost.Initialize(container);
            }

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                int x = 2;
                WordProcessor.ProcessUsingHighres();
            }
        }

     

     

    //Is it possible to comment out the call to SatisfyImports?

  • User profile image
    mtaulty

    Hi,

     

    Sorry for replying so late to your question, I'd missed it as a comment.

     

    In your code here, you're doing an import of a IWordProcessor onto your WordProcessor member variable. You're also (in the same class) initializing the composition container with all the Assemblies that have shipped with your Silverlight XAP.

     

    By the way - you should *not* have to write that code because it is the default behaviour of MEF in Silverlight anyway so you should be able to get rid of all the code in the function InitializeContainer().

     

    However, if you want MEF to satisfy the import that you have on your MainPage then you do need to call CompositionInitializer.SatisfyImports() on that instance so you can't comment that code out.

     

    Hope that helps.

     

    Mike.

Add Your 2 Cents