Event Aggregator VS Dependency Injection

For many Weeks, I have herd peoples saying they use Event Aggregator in prism as main publish/subscribe event mechanism.

When I started using this technology, I asked myself why?

Is Dependency Injection not good enough?

After a little research, I concluded that Dependency Injection could do the same job with less chance of bugs.

For Example, if we have Master Details Application with two modules: Tree Module and Details Module.

Event Aggregation Pattern in Master Details Application

If the Details Module want to know if selected item changed, Details module need to register to particular event but he don’t know if the publisher exists.

On the other hand, the publisher can send event but he do not know if this event have subscribers.

The Publisher:

Private void OnSelectedItemChanged(object selectedObject){



The Subscriber

Private void SubscribeToEvent(){



Private void OnSelectionChanged(object eventArgs)


//Do something


In our loosely coupled spaghetti code the publisher, don’t know if anyone listening and reacting to the fired event.

It make sense that if the publisher fire event he should know if this event have subscribers and here the benefit of “Dependency Injection” it keeps the loosely couple pattern but Verifies react when firing event.

Communication with Dependency Injection

public interface ISelectionObserver
        ViewModelBase SelectedViewModel { get; set; }

        event EventHandler SelectedChanged;

Here the TreeViewModel implement the ISelectionObserver interface that define all the public functions require to send SelectedChanged event

public class TreeViewModel : ViewModelBase, ISelectionObserver

public class TreeViewModel : ViewModelBase, ISelectionObserver
 public event EventHandler SelectedChanged;
        private void OnSelectedItemChanged()
            if (SelectedChanged != null)
                SelectedChanged(this, EventArgs.Empty);


DetailsViewModel do not know who is Implementing ISelectionObserver and he don’t care about it.

 public class DetailsViewModel : ViewModelBase
        #region fields
        ISelectionObserver _selectionObserver;
        public DetailsViewModel(ISelectionObserver selectedobserver)
            _selectionObserver = selectedobserver;
            _selectionObserver.SelectedChanged += (sender, e) =>

        void SelectedChanged()
            this.Data = _selectionObserver.SelectedViewModel;


BootsTrapper is responsible to inject the ISelectionObserver implementation component to DetailsViewModel.

Benefits of using Dependency Injection

  • Keeps loosely coupled pattern.
  • More testability.
  • The implementation is simple, and enable code reuse.
  • Reduces Dependence of technology (Prism).



Conceptually, in real world if something happens then the response should come, as well as in our programming world.

For example, if fire broke out then the publisher must to know that someone listen to the event, what EventAggregator does not provide

In addition, Dependency Injection does.

So if we look from responsibility side,

Dependency Injection gives us better way to communicate between loosely coupled components.


About Idan Reuven

Idan is Microsoft Certified Proffesional Developer (MCPD) Idan Working As Senior Software developer. and specialize in professional application development with WPF, WCF, Entity Framework, XAML, HTML 5, Java Script , jQuery, MVC, MVVM, C#, C++, SQL Server Technologies.
This entry was posted in WPF. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s