Click here to Skip to main content
12,303,833 members (75,683 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

6.8K views
Posted

EventAggregator Service in PRISM

, 15 Mar 2012 CPOL
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)

Share

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

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 2 Pin
Clifford Nelson20-Mar-12 13:51
memberClifford Nelson20-Mar-12 13:51 
GeneralRe: My vote of 2 Pin
Md. Rashim uddin21-Mar-12 8:39
memberMd. Rashim uddin21-Mar-12 8:39 
GeneralRe: My vote of 2 Pin
Clifford Nelson21-Mar-12 8:56
memberClifford Nelson21-Mar-12 8:56 
GeneralMy vote of 2 Pin
Dean Oliver15-Mar-12 9:43
memberDean Oliver15-Mar-12 9:43 
GeneralRe: My vote of 2 Pin
Md. Rashim uddin15-Mar-12 9:55
memberMd. Rashim uddin15-Mar-12 9:55 
QuestionWhy new up the EventAggregator? Pin
Karl Shifflett15-Mar-12 6:26
memberKarl Shifflett15-Mar-12 6:26 
AnswerRe: Why new up the EventAggregator? Pin
Md. Rashim uddin15-Mar-12 7:04
memberMd. 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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160530.1 | Last Updated 15 Mar 2012
Article Copyright 2012 by Md. Rashim uddin
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid