Prism Module Initialization With MEF

Topics: Prism v4 - Silverlight 4
May 25, 2011 at 7:42 AM
Edited May 25, 2011 at 1:25 PM


I'm using Prism 4 and MEF in my Silverlight application.

I'm declaring my Modules with the Export attribute, and declare my imports as public properties (to allow recomposition).

The problem is with the Initialize() method of the IModule interface - could it be that the Initialize() method will be called before all imports have been satisfied?

If so, how can this be solved?

May 26, 2011 at 2:27 PM


The scenario you're mentioning is a valid possibility. To mitigate this situation, you could put the imports in the module's constructor (decorating it with the ImportingConstructor attribute). That way, you would guarantee that, at the time the Initialize method is called, the imports are fulfilled.

I hope you find this helpful.

Guido Leandro Maliandi

May 26, 2011 at 2:36 PM

Thanks for you reply!

Using ImportingConstructor is a good workaround, but it prevents the module (and hence the entire xap?) from supporting recomposition.

My solution was to implement a MefModule class that simply synchronized the Initialize method of IModule with the OnImportsSatisfied method of IPartImportsSatisfiedNotification.

Maybe such a class could be part of the Prism.MefExtensions project?

May 26, 2011 at 2:39 PM


That seems to be a good idea to support that scenario. If you'd like that to be included on a future version, you could create a work item in the issue tracker requesting the feature.

Additionally, it would be helpful if you could provide us with the implementation you've made of this, so that users facing similar scenarios can benefit from your idea.

Guido Leandro Maliandi