Click here to Skip to main content
15,175,835 members
Articles / All Topics
Technical Blog
Posted 15 Mar 2012

Tagged as



EventAggregator Service in PRISM

Rate me:
Please Sign up or sign in to vote.
4.47/5 (7 votes)
15 Mar 2012CPOL1 min read
Using the EventAggregator service in PRISM.

Some days ago, when I was working with PRISM, I needed to send some notifications from one view to another based on some business logic. This is something like, if a view receives a notification from other resources, the view requires changing its appearance. To handle this type of situation, we usually remove the view, update its properties, and add the view in the particular region again. Another way we do it is by using the .NET Framework Events mechanism but this is not loosely coupled.

In PRISM, there is a nice way to do that especially to communicate with different Views/Modules (User controls, Entities, external project resources, and so forth) by using the EventAggregator service.

The EventAggregator service is primarily a container for events that allow decoupling of originators and receivers so that they can develop autonomously.

I share with you here some of my code in which I have used the EventAggregator service.

This is the entity class:

public class Company
    public Company()
    { }
    public int CompanyID { get; set; }
    public string CompanyName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string Postcode { get; set; }
    public Country Country{ get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime? DateModified { get; set; }

The event that I have used has been given below. The publisher and subscriber will use this event to communicate with themselves:

public class CompanyAddedEvent:CompositePresentationEvent<Company>

The MyEventAggregator class is basically for the EventAggregator instance:

public class MyEventAggregator
    private static EventAggregator myEventAggregator = null;
    private static readonly object syncLock = new object();
    private MyEventAggregator() { }
    public static EventAggregator GetEventAggregator()
        lock (syncLock)
            if (myEventAggregator == null)
                myEventAggregator = new EventAggregator();
            return myEventAggregator;

Event publishing:

MyEventAggregator.GetEventAggregator().GetEvent<CompanyAddedEvent>().Publish(your company Object);

Event subscription:

MyEventAggregator.GetEventAggregator().GetEvent<CompanyAddedEvent>().Subscribe((company) =>
////DO Whatever you want
}, false);

One thing I need to add here is, the publisher and subscriber do not have a direct reference to each other. That is, multiple publishers can raise the same event and multiple subscribers can listen to the same event.

This way, the event may subscribe by different views/modules and, once the event is published, the subscriber will get the notification and can start the task accordingly. This is very easy stuff and by using this facility, you could do very nice things. However, if you want to know the details, please have a look at this article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Md. Rashim uddin
Chief Technology Officer RightKnack Limited
Bangladesh Bangladesh
A big fan of getting down the latest cutting-edge technologies to the ground to innovate exceptionally amazing ideas.

My Blog:

My Email: rashimiiuc at yahoo dot com

Comments and Discussions

GeneralMy vote of 2 Pin
Clifford Nelson20-Mar-12 14:51
MemberClifford Nelson20-Mar-12 14:51 
GeneralRe: My vote of 2 Pin
Md. Rashim uddin21-Mar-12 9:39
MemberMd. Rashim uddin21-Mar-12 9:39 
Thanks for your reply. But this is a small technical blog and hence i didn't put the total source code. If you need any assistance please let me know. I will try my level best to help you.
GeneralRe: My vote of 2 Pin
Clifford Nelson21-Mar-12 9:56
MemberClifford Nelson21-Mar-12 9:56 
GeneralMy vote of 2 Pin
Dean Oliver15-Mar-12 10:43
MemberDean Oliver15-Mar-12 10:43 
GeneralRe: My vote of 2 Pin
Md. Rashim uddin15-Mar-12 10:55
MemberMd. Rashim uddin15-Mar-12 10:55 
QuestionWhy new up the EventAggregator? Pin
User 27100915-Mar-12 7:26
MemberUser 27100915-Mar-12 7:26 
AnswerRe: Why new up the EventAggregator? Pin
Md. Rashim uddin15-Mar-12 8:04
MemberMd. Rashim uddin15-Mar-12 8:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.