Click here to Skip to main content
15,867,686 members
Articles / All Topics

EventAggregator Service in PRISM

Rate me:
Please Sign up or sign in to vote.
4.47/5 (7 votes)
15 Mar 2012CPOL1 min read 11.3K   7
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:

C#
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:

C#
public class CompanyAddedEvent:CompositePresentationEvent<Company>
{
}

The MyEventAggregator class is basically for the EventAggregator instance:

C#
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:

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

Event subscription:

C#
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.

License

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


Written By
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: http://rashimuddin.wordpress.com/

My Email: rashimiiuc at yahoo dot com

Comments and Discussions

 
GeneralMy vote of 2 Pin
Clifford Nelson20-Mar-12 13:51
Clifford Nelson20-Mar-12 13:51 
GeneralRe: My vote of 2 Pin
Md. Rashim Uddin21-Mar-12 8:39
Md. Rashim Uddin21-Mar-12 8:39 
GeneralRe: My vote of 2 Pin
Clifford Nelson21-Mar-12 8:56
Clifford Nelson21-Mar-12 8:56 
GeneralMy vote of 2 Pin
Dean Oliver15-Mar-12 9:43
Dean Oliver15-Mar-12 9:43 
GeneralRe: My vote of 2 Pin
Md. Rashim Uddin15-Mar-12 9:55
Md. Rashim Uddin15-Mar-12 9:55 
QuestionWhy new up the EventAggregator? Pin
User 27100915-Mar-12 6:26
User 27100915-Mar-12 6:26 
Why are you newing up the EventAggregator? This is created for you by the Prism bootstrapping process.
Cheers, Karl

My Blog | Mole's Home Page |
XAML Power Toys Home Page

Just a grain of sand on the worlds beaches.



modified 27-Feb-21 21:01pm.

AnswerRe: Why new up the EventAggregator? Pin
Md. Rashim Uddin15-Mar-12 7:04
Md. Rashim Uddin15-Mar-12 7: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.