MEF with Prism/MVVM

Topics: Prism v4 - WPF 4
Jul 26, 2011 at 8:23 AM

HI all,

In my application I want to stick to Prism/MVVM. The application consists of several separated assemblies (modules if you will). Those modules have views, that can appear in various Regions of the Shell. Whenever events happen/trigger in one of the views, other views in other Regions have to react/respond to that event.

Example: when a user chooses one Person from a PersonList in view A, the PersonDetails have to appear in PersonDetailView of view B.

If I understand correctly I need an EventAggregator to achieve this. Furthermore, I need to "inject" that EventAggregator into the different modules, so that they can publish and subscribe on the EventAggregator's CompositePresentationEvent.

My questions:

1. How do I instantiate the EventAggregator in the Container?

2. How do I inject the EventAggregator into the modules so that they can publish and subscribe respectively?

If possible I would appreciate documentation for further study and examples. Please no examples without proper MVVM structure, I've seen more than a few like those. The trick for me is in the MVVM with the dependeny injection!

Thanks in advance!

Jul 26, 2011 at 2:04 PM


In applications developed using Prism, services such as the EventAggregator are exported automatically. For example, you might find the MefEventAggregator class useful to see how this is achieved. The contract for the export involving the EventAggregator is its interface, IEventAggregator. Therefore, in order to inject the EventAggregator into your modules, you need to use the Import attribute, or the ImportingConstructor, or anything similar that achieves similar result, for a property of type IEventAggregator. That will bring the EventAggregator to your module, thus allowing you to publish and subscribe events for your purposes.

I hope you find this helpful.

Guido Leandro Maliandi

May 24, 2012 at 12:43 PM

This reply is all very well and correct, but my problem is that each view model is getting a new and different instance of the object, an object that must only be instantiated once. I am "simply" trying to pass around in this way an event aggregator, and so far, each VM is seeing a different one.

Can anyone advise on how I can ensure that only one event aggregator is created. I am using Prism 4 with MEF and a full MVVM pattern.



May 24, 2012 at 7:54 PM


By default Prism registers a singleton instance of the EventAggregator class in the container through the IEventAggregator interface. This can be seen in the ConfigureContainer method in the UnityBootstrapper implementation provided with Prism when using Unity or in the MefEventAggregator class when using MEF.

Hence if you retrieve this instance using the IEventAggregator interface from the container (note that you must use the interface to retrieve the mapped instance of the EventAggregator class), then you should be able to retrieve the same instance in all the view models without problems.

If this doesn't help it would be helpful if you could provide us with more information about your particular scenario, and the way you obtain the EventAggregator instance in each of your view models.


Agustin Adami