Are MEF's ComposableParts contracts instance-based?

Posted by Dave on Stack Overflow See other posts from Stack Overflow or by Dave
Published on 2010-06-07T16:36:28Z Indexed on 2010/06/07 16:52 UTC
Read the original article Hit count: 309

Filed under:
|
|

I didn't really know how to phrase the title of my questions, so my apologies in advance. I read through parts of the MEF documentation to try to find the answer to my question, but couldn't find it.

I'm using ImportMany to allow MEF to create multiple instances of a specific plugin. That plugin Imports several parts, and within calls to a specific instance, it wants these Imports to be singletons. However, what I don't want is for all instances of this plugin to use the same singleton.

For example, let's say my application ImportManys Blender appliances. Every time I ask for one, I want a different Blender. However, each Blender Imports a ControlPanel. I want each Blender to have its own ControlPanel. To make things a little more interesting, each Blender can load BlendPrograms, which are also contained within their own assemblies, and MEF takes care of this loading. A BlendProgram might need to access the ControlPanel to get the speed, but I want to ensure that it is accessing the correct ControlPanel (i.e. the one that is associated with the Blender that is associated with the program!)

This diagram might clear things up a little bit:

alt text

As the note shows, I believe that the confusion could come from an inherently-poor design. The BlendProgram shouldn't touch the ControlPanel directly, and instead perhaps the BlendProgram should get the speed via the Blender, which will then delegate the request to its ControlPanel.

If this is the case, then I assume the BlendProgram needs to have a reference to a specific Blender. In order to do this, is the right way to leverage MEF and use an ImportingConstructor for BlendProgram, i.e.

[ImportingConstructor] public class BlendProgram : IBlendProgram { public BlendProgram( Blender blender) {} }

And if this is the case, how do I know that MEF will use the intended Blender plugin?

© Stack Overflow or respective owner

Related posts about plugins

Related posts about inversion-of-control