Saturday, April 04, 2009

The Redmond Developer & Kathleen Dollard get MEF’ed with VB

Redmond Developer - Ask Kathleen - Working with MEF

“Learn how to free your application from dependencies and interchange implementations using Managed Extensibility Framework.

Q: I've been hearing the term MEF lately and I know it means Managed Extensibility Framework, but I don't understand what it does. Would it be a good fit for allowing customers to add their own forms to our Windows Presentation Foundation (WPF) app? We need those new forms to appear in our menus and we don't want to give customers our Visual Basic source code to recompile.

A: This sounds like a good application for MEF. …

MEF is an extensibility model that allows components to interact using a simple model. Components are called parts in MEF (see Figure 1). MEF is designed to be extensible to different types of models, but ships with an attributed model. This lets you define the interaction between your parts via normal .NET attributes. If a part needs something, it uses an Import attribute and if a part supplies something it uses an Export attribute. Any part can be a host or client, provider or consumer. MEF bases these interactions on contracts and offers a flexible discovery model. Your application makes a request and leaves it to MEF to provide the implementation. This frees your application from dependencies and lets you interchange implementations

Your application will be the MEF host while your customers will write MEF clients or extensions. Extensions are simpler to write because the host must also manage the CompositionContainer. I'll step through the process of creating an interface, building extensions and building a WPF host.

The compose method does the actual preparation:

Private Function Compose() As Boolean
Dim cat As New AggregateCatalog

The aggregate catalog allows you to manage several catalogs together. If a set of catalogs appear together in an aggregate catalog, all matching items within this set of catalogs are discovered. Several different types of catalogs are available, including DirectoryCatalog, which loads all assemblies in a specified directory. In your case, I'd suggest including the location of extensions as part of the application settings to allow later configuration. You'll also add the current assembly:

Dim extLocation = My.Settings.ExtensionLocation
cat.Catalogs.Add(New DirectoryCatalog(extLocation))
cat.Catalogs.Add(New AssemblyCatalog( _
Me.GetType.Assembly))
mContainer = New CompositionContainer(cat)

At this point the container is ready to work, but no work has been requested. A composition batch allows you to specify…

…”

MEF is very likely going to be one of those technologies we’re really going to want to know better when VS2010/.Net4 ships.

(via Brad Adams - Redmond Developer: Working with MEF in VB and WPF)

Related Past Post XRef:
Managed Extensibility Framework (MEF) CTP2 Released – Now with the full source
The Managed Extensibility Framework (MEF) CTP Released (Not to be confused with the Managed Addin Framework [MAF] which became System.Addin)

No comments: