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:
- Create IView implementation
- Create IModel implementation
- Create Presenter and pass IModel and IView to its constructor
- 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?
==Update==
I have found a solution and I blogged about it here:
Krzysztof Koźmic’s blog – Creating tree of dependencies with MEF
However, I’d be more than happy if someone came up with a better solution.**
Check my answer here.
http://codebetter.com/blogs/glenn.block/archive/2008/11/12/mvp-with-mef.aspx
EDIT: (Added from the link, to prevent not being flagged as low quality / LOA)
So what’s going on here?
Shell gets injected with Presenter. Presenter gets injected with View and Model. Everything here is singletons, but doesn’t have to be.
The difference between our two examples is that the Presenter is getting injected into the shell rather than the View. If the Presenter is creating the View then you can’t just grab the View first (as he was doing), or the Presenter will not get created. Well you can do it, but you end up hacking it to bits. Cleaner is to just inject the Presenter and have it expose an IView. We did this in Prism and it worked quite well.