Mike Taulty's Blog
Bits and Bytes from Microsoft UK
MEF, Silverlight and the DeploymentCatalog

Blogs

Mike Taulty's Blog

Elsewhere

I posted about the DeploymentCatalog in the new MEF preview the other day and I got a question or two along the lines of “show me an actual example rather than snippets” and so I thought I’d walk through a simple example here.

Let’s say that I want to build a Silverlight application that has a need for a photograph to be provided by a user and I don’t really care how this photograph is captured. It might be captured from the webcam, it might be captured from the file system or drag-drop or anything. I don’t mind, I just want to host a plugin that is happy to gather a photo from the user and then hand it to the rest of my code.

I started to write this up as a blog post but I realised it would involve so many steps that it’d take a long time so I quickly recorded it as a screen capture. Note that this is one of the roughest screen captures I’ve ever made as I largely just pressed the “record” button and then made stuff up on the fly but I hope it still captures the essence of things.

And here’s the code that I just zipped up and copied so it’s exactly as it was in the video – i.e. equally rough and ready.

Update…

In response to this post, a letter flooded in from my reader :-) Turns out he’s also called Mike ( hey Mike! ) and, honest, it’s not me writing to myself.

Mike’s point was around the idea of instancing. That is – I showed a little example of a Silverlight application dynamically adding a plug-in at runtime having downloaded it from the web but what if there was some dependency on another service that needed to be single-instanced?

Will that defeat the power of MEF?

In short, “no” :-) and the answer is buried in the Channel 9 MEF videos but given that I’d got the source-code kicking around I thought I’d just carry on the video…


Posted Wed, Feb 24 2010 1:10 AM by mtaulty
Filed under: ,

Comments

Anonymous wrote re: MEF, Silverlight and the DeploymentCatalog
on Wed, Feb 24 2010 2:51 AM

That's excellent.  It was so much clearer seeing you actually developing on the fly, than it would have been just reading a blog post.  Thanks for sharing it.

Tony wrote re: MEF, Silverlight and the DeploymentCatalog
on Wed, Feb 24 2010 6:18 AM

It was very helpful to watch you think through the process. I would like to see more tutorials of this style.

Cheers,

-Tony

ASeventhSign wrote Linked - List
on Wed, Feb 24 2010 10:43 AM

Linked - List

Amr Elsehemy wrote re: MEF, Silverlight and the DeploymentCatalog
on Thu, Feb 25 2010 4:15 AM

Mike,

Thanks for the videos they are great, can I find them as a download link or I have to stream them online?

Steve Strong wrote re: MEF, Silverlight and the DeploymentCatalog
on Thu, Feb 25 2010 5:46 AM

Mike,

Gutzy video, coding on the fly like that. but VERY VERY useful.   I would like to see more like it,

Sam Minnella wrote re: MEF, Silverlight and the DeploymentCatalog
on Fri, Feb 26 2010 9:19 AM

I really enjoyed coding on the fly!

Dragan wrote re: MEF, Silverlight and the DeploymentCatalog
on Wed, Mar 3 2010 5:34 AM

Mike,

That was one of the best tutorials I have ever seen.

uberVU - social comments wrote Social comments and analytics for this post
on Fri, Mar 5 2010 3:42 PM

This post was mentioned on Twitter by jrguay: MEF, Silverlight and the DeploymentCatalog: I posted about the DeploymentCatalog in the new MEF previ... http://bit.ly/aI8x5F (by @mtaulty)

Bill Seddon wrote re: MEF, Silverlight and the DeploymentCatalog
on Tue, Mar 9 2010 11:39 AM

I agree, a good video which may have been rough and ready to you but I couldn't see many problems and none that detracted from the message.

I'm not a Mike but I've a follow-on question to the follow-on question.  I appreciated your review of the utility of the DeploymentCatalog because we've implemented this kind of functionality for a service and it would be great to be able to lean on a general purpose capability like this.

In response to Mike's question you created the second video.  However in it, the main application was responsible for initializing the singleton service but shouldn't the photograph UI be responsible for creating and initializing the catalog for the singleton?

I appreciate your example was a proof of concept and was not an exhaustive tutorial on the DeploymentCatalog.  Can you comment on whether the photograph UI would be able to set up the singleton service or is it expected that a main application will need to know something about the components from which it is constructed?

In this example, the main app may have also consumed the singleton service to display the number of photos in which case the main app would have to know about the singleton anyway.  But in the case where the main app never needs to know about some internal detail can the catalog dependency be removed?

Long question for what will probably be a yes/no answer.

mtaulty wrote re: MEF, Silverlight and the DeploymentCatalog
on Tue, Mar 9 2010 4:00 PM

Bill,

You made me go and re-watch my own video :-)

In the video we have a main application.The main application imports plugins and the plugins depend on a singleton service.

All the main application knows is that it imports plugins.

All the plugins know is that they import a singleton service.

The main application loads XAPs and adds those XAPs to a catalog. This could easily be factored out into an XML configuration to take this "knowledge" away from the main application. The application does not know which components live in which XAPs or even what components are actually in the XAP so it's really just "dumb loading" a set of XAPs and we could easily factor that out into some other component that was declaratively driven.

Hope that makes sense,

Mike

Mike Taulty's Blog wrote MEF and the DeploymentCatalog
on Tue, Mar 9 2010 11:31 PM

The Silverlight 4 beta came with a version of MEF that had 3 catalogs – namely AssemblyCatalog, TypeCatalog

Anonymous wrote re: MEF, Silverlight and the DeploymentCatalog
on Fri, Apr 23 2010 5:13 PM

Hey Mike -

Just downloaded the sample source and compiled / ran using VS2010 RTM and it doesn't seem to be working. OnImportsSatisfied is only called once (with the default of PluginInstance being null).  Any ideas as to what's going on?