tags:

views:

564

answers:

4

==Update==

I have found a solution and I blogged about it here: link

However I'd be more than happy if someone came up with a better one.**

Using MEF I want to do the following.

I have a WPF Shell. To the shell I want to Import from another DLL a UserControl that is also a View of my MVP triad. The way the MVP triad works, is that in presenter I have a constructor that takes both IModel and IView and wires them up. So, in order for this to work, I need MEF to do the following:

  1. Create IView implementation
  2. Create IModel implementation
  3. Create Presenter and pass IModel and IView to its constructor
  4. Import IView implementation into my shell when it gets displayed

Instead what it does, is it only creates the type Exporting IView and passes it to the shell, basically skipping steps 2 and 3. Its pretty logical, when you think about it, but how can I tell MEF to also create the whole triad when I ask for a IView. I don't need to reference Presenter nor model anywhere else in my Shell .dll so puting it as an Import as well is not an option (and it would be quite ugly anyway :).

I'm using the latest drop of MEF (Preview 2 Refresh). Anyone?

A: 

Did you put the [ImportingContructor] on the contructor?

Glenn

Glenn Block
A: 

Yes I did, check the link I provided, there is a simplified version of the code, and my solution. It works but I'm wondering if there's a better way to do this.

Krzysztof Koźmic
+1  A: 

Hi Kryzstof,

The way you outlined in your blog post is the perfectly valid way for utilizing MEF. This is nested composition, and while designing it is always good to keep in mind that Container is the decider, so as a plug-in / extender vendor you will focus on your services that you are "exporting" and as an important, you shouldn't worry about what you need to be serviced, or "importing" (this point has some issues in the last drop, but I hear good enough to be optimistic about it).

So in nested composition, you may need some external services but at the time you can also be providing some. When you do a compose, it will plug everything together.

I have a blog post containing 2 examples illustrating this way of thinking :

http://www.sidarok.com/web/blog/content/2008/09/26/what-is-this-managed-extensibility-framework-thing-all-about.html

Also, to drop the dll and watch the types in it, you can use DirectoryPartCatalog to watch that folder.

You will also need to watch out the scenarios where there are multiple exports for the same contract, and determine the right one from metadata provided.