Implementing Event Aggregator

May 5, 2011 at 3:40 PM

I have been following the HelloScreens and HelloEventAggregator samples as my first CM implementation.  I have essentially added another level to the HelloScreens and built a few base classes.  One of the things I realized after a bit is that I need the EventAggregator all over the place and I've found I'm passing it into the constructors all over just to get it into my base classes.

Is that overkill?  Should/Could I just not have a static implementation on my shell?  I know the HelloSample uses the ImportingConstructor attribute but I've found that isn't working so well with base classes (some abstract some not) and having other constructor params that I am passing in my own params.  That may also be partially my ignorance with MEF.



Mar 27, 2012 at 4:17 PM


I find myself in much the same situation; I've got a lot of composed views with a few inherited viewmodels and I can see that I'm going to end passing IEventAggregator around all over. I assume it's either because I've missed something or have similarly misunderstood the MEF usage in the HelloEventAggregator example.

Any advice appreciated,


Mar 27, 2012 at 6:38 PM

Given that the EventAggregator is typically used as a singleton instance throughout our applications, we used a static factory to provide access to it:

    public static class EventAggregatorFactory
        // Singleton instance of the EventAggregator service.
        private static IEventAggregator _eventAggregator = new EventAggregator();

        public static IEventAggregator EventAggregator
                return _eventAggregator;

Having said that, our architecture also contains an Infrastructure project, which is referenced by all of the solution's other projects.  This Infrastructure project contains this factory, as well as any other static classes, so that they're accessible by the entire solution.  We've actually been using the Prism EventAggregator, however, the factory implementation should be the same.

Mar 28, 2012 at 11:25 AM

That seems to make sense in my situation. I think after a lot of digging and reading I understand why I would want to do it in the way demonstrated in the HelloEventAggregator sample, but for what I'm doing at the moment, the singleton implementation above will fit nicely.

As an aside, I absolutely love IHandle, I find it provides a lot of clarity/readability even when it's just my own code.