Managed Extensibility Framework (MEF) Preview 8

FileDownload[1]

I have been using MEF Preview 6 for Silverlight since the moment the came out, I have been really happy at that release, allows me to load XAP files dynamically in order to speed up the starting time of my Silverlight application as well as decompose my applications blocks to very maintainable pieces that will be downloaded on the clients machine on demand. So the love fest between me and MEF started on Preview 6.

MEF Preview 8 was release and I kept myself away from the download as I was still very happy to use Preview 6, fulfill all my needs. So why did I wanted to upgrade? Well, I had a few dlls that didn’t want to get into the package to get downloaded, so I thought that was time to upgrade to Preview 8 to see if there was any difference.

What’s MEF?

First let’s describe MEF for people that haven’t use this framework yet. The Managed Extensibility Framework will provide developers a way to load application extensions.

The structure of MEF is very simple to find out what extension to package.

FileDownload[1]

Diagram from codeplex  MEF

It’s time to write some code.

In preview 6 the way to get a package of assemblies was to create your Package Catalog

PackageCatalog catalog = new PackageCatalog();

and then added into a container

var container = new CompositionContainer(catalog);
container.ComposeParts(this);

So you can just download the package and attached it to your application:

          Package.DownloadPackageAsync(new Uri(sXap, uriKind), (s, p) =>
            {                
                catalog.AddPackage(p);                
                if (LayoutRoot != null)
                {
                    System.Collections.Generic.List<System.Reflection.Assembly> assemblies = p.Assemblies as System.Collections.Generic.List<System.Reflection.Assembly>;
                   
                    System.Reflection.Assembly assembly = assemblies[0];

Now let’s talk about how Preview 8 changes all this:

So instead of PackageCatalog() you need to create an instance of

AssemblyCatalog catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());

 

Silverlight 4 and MEF

With Silverlight 4  or .NET 4 for that matter, now all this has changed, you won’t need to download a that module to use it on your applications, MEF comes out of the box as well as WCF RIA Services, also known as .NET RIA Services.

Cheers

Al

2 Comments

  • Couple notes worth mentioning.
    1) The PackageCatalog and the AssemblyCatalog changed it too are not equivalent. The AssemblyCatalog will only contain Exports in the main executing assembly where the PackageCatalog will contain all exports found in all assemblies in the downloaded package (including the default application package).
    2) If you are still interested in using the PackageCatalog it now availabe in the Silverlight Toolkit 4 beta at http://silverlight.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=36060.

  • And how does that mean? I do not understand anything.

Comments have been disabled for this content.