Click here to Skip to main content
Click here to Skip to main content

EventAggregator Service in PRISM

, 15 Mar 2012
Rate this:
Please Sign up or sign in to vote.
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 specially 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.

License

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
Software Developer (Senior) KAZ Software Limited.
Bangladesh Bangladesh
I am a true visionary as well as have always been passionate about learning. I always love to work with thought-provoking personnel to develop amazing, exciting and innovative ideas as part of an aggressive team that challenges my ingenious ideas, creativeness and extensive experience.
 
My Blog: http://rashimuddin.wordpress.com/
 
My Email: rashimiiuc at yahoo dot com
Follow on   Twitter   LinkedIn

Comments and Discussions

 
GeneralMy vote of 2 PinmemberClifford Nelson20-Mar-12 13:51 
GeneralRe: My vote of 2 PinmemberMd. Rashim uddin21-Mar-12 8:39 
GeneralRe: My vote of 2 PinmemberClifford Nelson21-Mar-12 8:56 
GeneralMy vote of 2 PinmemberDean Oliver15-Mar-12 9:43 
GeneralRe: My vote of 2 PinmemberMd. Rashim uddin15-Mar-12 9:55 
QuestionWhy new up the EventAggregator? PinmemberKarl Shifflett15-Mar-12 6:26 
AnswerRe: Why new up the EventAggregator? PinmemberMd. Rashim uddin15-Mar-12 7:04 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 15 Mar 2012
Article Copyright 2012 by Md. Rashim uddin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid