MEF – Conventional catalog for registering non-attributed parts

October 18, 2010

tags: ,
no comments

As part of the task where I had to upgrade an existing WPF project to PRISM 4.0 and work with MEF, one of the requirements was to support registering existing non-attributed parts.

Consider the following example –

Code Snippet
  1. public interface IFoo { }
  3. public class Foo : IFoo { }
  5. static void Main(string[] args)
  6. {
  7.     //This is the desired thing to do
  8.     myCatalog.Register<IFoo, Foo>();
  9. }

As you can see, type Foo has no export definitions set on it.
One approach would be to go over all the registrations and add attributes where needed, or create wrappers for legacy components, but the goal in our case was to support this kind of registration.

After digging around, I found two ways that you can go about it –

  1. Implement a conventional catalog using the ReflectionModelServices which is the MEF’s API for building reflection related parts information.
    1. Example – CodePaste.NET – A conventional catalog for MEF (Prototype)
  2. Use the MEF’s AttributedModelServices with a TypeDelegator to expose the actual type as an attributed type even though it doesn’t have attributes defined directly on it.
    This is quite a sophisticated solution and I decided to stick with that one.
    1. Example – Poco, Mef, and custom type systems. Are you ready to take the red pill

You can look at the links to see the actual code of each solution. Plus, you can integrate it with the type catalog which supports type registration in runtime which I described in my previous post.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>