Glenn Block

Sponsors

The Lounge

Wicked Cool Jobs

News

  • View Glenn Block's profile on LinkedIn

    Me

Advertisement

MEF has landed in Silverlight 4. We come in the name of extensibility.

UFO over highway B83 by Markusram.

http://www.flickr.com/photos/markusram/3641457838/

At PDC we announced that MEF has is now part of Silverlight 4.

This may sound like alien speak :-), so I’ll break it down for you. It means building maintainable/pluggable RIA apps just got a whole lot easier. 

What is MEF?

The Managed Extensibility Framework (MEF) is a new library in Silverlight 4 for building RIAs that can be incrementally extended. With MEF you extend your apps in a declarative fashion and let MEF do the plumbing rather than writing all the plumbing code yourself.

  • It’s for you and your dev team – You can add and replace functionality to the system without having modify existing code. This means you and your QA team are happy because you don’t have to worry about introducing bugs into the existing code base.
  • It’s for your customers – MEF allows you to design applications and libraries that can be extended by third-parties after they have been deployed.
  • Always there, always ready – In Silverlight, MEF is ready to go whenever and wherever you need it. You don’t have to be an acrobat, or have a masters degree in complexity :-) You just do it.

Also available in Silverlight 3

MEF ships as part of Silverlight 4, but we’ve also made a Silverlight compatible version of MEF available on codeplex at MEF.Codeplex.com.

The Basics

THREE concepts, that’s all you need to know to get going.

image

  • Export to tell MEF you are providing a service or capability.
  • Import to tell MEF you need something.
  • Compose It – To tell MEF to go do it!

Below you can see the basics in action for creating a Hello MEF pluggable dashboard. First we create a pluggable widget which in this case is a simple user control.

Export it

To tell MEF it’s available, we simply throw an Export attribute on it and specify the contract (in this case we use a type) you want to export it as. We could choose any type that the Widget implements, but to keep it simple, lets just use UserControl. As you can see, we don’t have to derive from any special base types, or use any interfaces, you use whatever class you would like.

image

Import It  - Import single

Notice our Widget  has a Message property. Let’s let MEF provide that rather than hardcoding it as I want it to be customizable. To tell MEF we need it, we’ll just slap on an [Import] attribute. If we don’t specify a contract for the import, it will just derive the contract as being a string. However, being that we don’t want just ANY string, we want the Message, we can specify a specific contract such as “HelloMEF.Message”.

image

Next let’s create our message which our widget(s) will import. To do this we’ll create an export, but it will use a feature of MEF called a Field Export. This is because we don’t own System.String and can’t derive from it, instead we want to somehow create an instance of a string and export it as our message. With a Field Export we can do just that as can be seen below. If our needs are beyond simply setting a value which we can do in a field, MEF also supports property exports.

image

Import It – Import many

Lastly we need our host app to discover the extensions. To do that we’ll do two things. First, we will create a public collection of type UserControl. Next we’ll add an ImportMany attribute, telling MEF that our MainPage needs all exports of UserControl to be injected into it.

image

Compose It – Satisfy imports

Next we need to somehow tell MEF to go do some work and give it to us. Doing this is very easy, we just drop into the constructor of our MainPage and call the SatisfyImports method. Once we do, our Widgets collection will be loaded up with all available widgets.

image 

Now we can simply loop through that collection and show the widgets, in this case by adding them to an Items control.

image

We’ve just seen the core essentials of MEF in SL4, that’s all it takes to get going! Our dashboard works, but it’s pretty simplistic. This is just the beginning of what is possible with MEF, we’ve literally only scratched the surface, however even with this small set of tools I’ve just given you, you can accomplish a lot! In future posts (on this blog or others :-) ) and in our Silverlight documentation you’ll see more about what’s possible. I covered some of this in my talk including lazy instantiation, export metadata, custom exports, dynamic recomposition, and application partitioning.

PDC talk

At PDC, I had the pleasure of delivering a talk on building extensible RIAs which is now available online here. As a highlight I was joined by Ariel Ben-Horesh of Sela, and Jackson Harper of the Moonlight team at Novell

Check out my PDC talk: Building Extensible Rich Internet Applications with the Managed Extensibility Framework. In addition to seeing MEF in action, you’ll see some work around integrating MEF with Prism and even using MEF on Moonlight, the open source Silverlight implementation. More on this in a future post! You’ll also see how to surgically add extensibility to an existing application, and how to dynamically partition applications such that functionality is delivered on demand at runtime!

Can’t wait, want more?

  • Check out the attached demos (Yes code!) from that talk. (Requires Silverlight 4)
  • Head over to MEF.Codeplex.com and download MEF Preview 8 which contains several Silverlight samples including an extensible DataGrid. These samples work on SL3 or on SL4.
  • Check out Brad Abrams series on MEF and Silverlight
  • Check out Erik Mork’s screencast on MEF and Silverlight.
  • Check out these podcasts on MEF and Silverlight

Also watch for Jesse Liberty and Tim Heuer’s blogs as you’ll likely see some MEF/Silverlight content there VERY soon. ;-)


Posted Sun, Nov 29 2009 3:32 AM by Glenn Block
Filed under: , ,

[Advertisement]

Comments

progg.ru wrote MEF включен в Silverlight 4. Мы идем во имя расширяемости
on Sun, Nov 29 2009 7:50 AM

Thank you for submitting this cool story - Trackback from progg.ru

PimpThisBlog.com wrote MEF has landed in Silverlight 4. We come in the name of extensibility.
on Sun, Nov 29 2009 8:01 AM

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

Raghuraman wrote re: MEF has landed in Silverlight 4. We come in the name of extensibility.
on Sun, Nov 29 2009 8:32 PM

Hiyaa Glenn,

Thanks to you, David Poll and Brad A I am now using MEF already in Silverlight 3.0

This is another great writeup.

Thanks

Raghuraman

Microsoft Weblogs wrote Building extensible Silverlight applications with MEF
on Mon, Nov 30 2009 12:41 PM

At PDC09, Glenn Block delivered a great presentation on building extensible RIAs using the Managed Extensibility

RIA Services wrote Building extensible Silverlight applications with MEF
on Mon, Nov 30 2009 12:46 PM

At PDC09, Glenn Block delivered a great presentation on building extensible RIAs using the Managed Extensibility

Justin wrote re: MEF has landed in Silverlight 4. We come in the name of extensibility.
on Mon, Nov 30 2009 5:36 PM

Glen, I can't find the code, where is it?

Blog de Oskar Alvarez wrote Enlaces Interesantes WPF/Silverlight - 01/12/2009
on Tue, Dec 1 2009 2:54 AM

Aquí vienen los enlaces correspondientes a hoy WCF RIA Services and a guide to use DTO/”Presentation

on Tue, Dec 1 2009 1:25 PM

Windows AppFabric/WF 4.0 Getting Started with Windows Server AppFabric - The AppFabric team in a post to the CSD blog The .NET Endpoint, takes you through the steps of installing AppFabic (including prerequisites) and getting started with tutorials. If

[Export] Bnaya Eshet wrote MEF for Beginner (concept) - part 1
on Thu, Dec 3 2009 12:50 PM

MEF for Beginner - part 1 this post is the first in a series of posts of How To start using the new MEF

David Roh wrote re: MEF has landed in Silverlight 4. We come in the name of extensibility.
on Sat, Dec 5 2009 8:56 AM

Hi Glenn,

Thanks for all the great work on MEF!

I also can't find the code for your PDC talk.

Glenn Block wrote re: MEF has landed in Silverlight 4. We come in the name of extensibility.
on Sun, Dec 6 2009 2:11 AM

Hi Guys

I had forgotten to put the attachment. It's there now.

Thanks

Glenn

Darren Fieldhouse wrote re: MEF has landed in Silverlight 4. We come in the name of extensibility.
on Sun, Dec 6 2009 3:59 AM

I assume when you call SatisfyImports MEF uses reflection to discover all the objects in loaded assemblies, and then checks them for the appropriate attributes.

If you have serveral large assemblies loaded could this become an expensive process? How scaleable is this technology?

Thanks.

Glenn Block wrote re: MEF has landed in Silverlight 4. We come in the name of extensibility.
on Sun, Dec 6 2009 4:37 AM

Darren

Very scalable. We're using it in Visual Studio which has many, many thousands of components and assemblies.

In Silverlight we look at all assemblies in the XAP. But MEF also allows you parition into multiple XAPs so that they do not all load at once.

Furthermore, our catalog mechanism is designed in such a way that you can build a cache, thus avoiding the reflection cost until you actually need an instance.

Today that requires you to create a custom catalog using our ReflectionModelServices API to create / deserialize your cache. VS for example uses such a cache. If you check this CodePaste http://codepaste.net/yadusn, you'll see a sample of how you could write such a thing using that API. In this case I am using it for passing non-attributed parts to MEF, but the API was primarily designed for caching scenarios. It is what VS uses.

The important thing it is possible today, and in the future we  will likely provide one out of the box.

HTH

Glenn

Alex Zambelli's Silverlight Media Blog wrote Now available: Expression Encoder 4, IIS Media Services 4.0 Beta, SSME 1.0, SMF 2.0 RC1, Olympics case studies
on Fri, Jun 25 2010 6:17 AM

While most of the world is on a collective vacation watching the World Cup, media teams at Microsoft

Tiago Barbosa wrote Saiu a nova release do Microsoft Silverlight Media Framework 2.0
on Thu, Jul 8 2010 4:39 AM

O que é a Microsoft Silverlight Media Framework? A Silverlight Media Framework é uma framework

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Devlicio.us