Any guidance on having both MEF and Unity available?

Topics: Prism v4 - Silverlight 4
Mar 16, 2011 at 1:43 PM


I have a base application template that I use when creating new apps. Currently this template is based on Prism 2. I am currently in the process of converting it to Prism 4. Now of course there are a few more things to think about given the inclusion of MEF. MEF is the "cool" thing now. So I am trying to use this over Unity. However the more I get into it the more I wonder whether MEF is indeed the right option for me (not talking about anyone else, just me). My biggest problem with MEF is that if you don't have attributes, you don't have MEF. It appears future versions of MEF will support an attribute less model but that's then and this is now. You see more often than not my views have no code behind. The code behind is unaware of the view model. More often than not the wiring of my views and view models happens through configuration. The infrastructure through this configuration will instantiate both the view and view model and then wire them, and it currently uses Unity to instantiate these. With MEF it appears your view MUST explicitly import through attributes its view model and the view model MUST explicitly export itself through attributes. I have no problem with this and certainly there are scenarios where this is useful. I personally however am not a huge fan of having to ALWAYS "clutter" my types with attributes just to get dependencies resolved. Again, I think it's OK BUT having worked with Unity in the past it seems like a step backward not being able to resolve an instance without the need of attributes. But again, I like MEF and under certain circumstances I like its usage but not ALWAYS. I was wondering if there is any way to use both with PRISM? I would like to give my team options in terms of dependency resolution. For those that like MEF and don't mind all the attribute noise, fine, use it. But for those that don't, don't. I guess I could live with using the MefBootstrapper but somehow make available Unity as an option. Ideally however I would like a custom container that will be smart enough to know whether to use MEF or Unity based on the type. If the type has MEF decorations use MEF, otherwise fallback on Unity. I believe this option though is much more involved and right now I lack the time to do any such thing. So again, does anyone have any advice on making Unity available as an option when using the MefBootstrapper? Thank you

Mar 16, 2011 at 6:33 PM


Prism is designed to be container agnostic, but not to work with two Dependency Injection containers at the same time.

However, your requirement should be possible to achieve, as a temporal measure. You should take into account that there are certain classes that use ServiceLocation, so they must use MEF, since there is one Locator Provider, which is configured in the corresponding bootstrapper.

Additionally, you might find the following chapter from the Prism MSDN documentation useful:

Chapter 3: Managing Dependencies Between Components

I hope you find this helpful.

Guido Leandro Maliandi