Click here to Skip to main content
14,304,866 members

Composite Applications with Prism in WPF - Part 3

Rate this:
5.00 (13 votes)
Please Sign up or sign in to vote.
5.00 (13 votes)
5 Dec 2015CPOL
In this article we will learn about EventAggregator and IActiveAware interface and its uses in WPF application using Prism. This is continuation of second part of article series having total three parts.

Introduction

In this article we will learn about EventAggregator and IActiveAware interface and its uses in WPF application using Prism. This is continuation of second part of article series having total three parts.

In first part we have created Shell and defined the regions to load modules within regions of Shell. In second part we have created common infrastructure and five modules. So far we are able to load modules on application start as well as on demand and able to save request to database (xml file). As we have discussed in last section of second part, there is an issue with "All Request" module. In this part we will fix that issue and fulfill one more requirement.

Note: If you have not gone through first part and second part, I recommend you to first visit those because it will give you better context and help you to understand this article better.

Outlines

Part 1

  • Overview of Final Demo
  • Architecture of Final Demo
  • What is Prism
  • What is Shell
  • Creating Shell and Defining Regions

Part 2

  • Creating Common Infrastructure
  • Creating Modules
    1. Navigation Module
    2. Employee Module
    3. Software Module
    4. Hardware Module
    5. Request Module
  • Loading Modules
  • Issues in Demo Application

Part 3

  • What is Event Aggregator
  • Use of Event Aggregator
  • What is IActiveAware Interface
  • Use of IActiveAware

Recap of the Issue

We left last demo application with an issue in "All Request" module discussed in last section of second part. Once "All Request" module is loaded for the first time, combobox is showing correct request Ids associated with entered Employee Id. But next time when Employee Id get change, combobox is not showing correct request Ids which are associated with latest Employee Id.

As per our requirement whenever Employee Id gets change, in "All Request" module combobox need to show all request Ids associated with recently entered Employee Id. To accomplish that we need to notify "All Request" module. This issue can be fixed with the help of EventAggregator.

What is EventAggregator

EventAggregator provides a global service to publish or subscribe events across application. EventAggregator is a class which implements IEventAggregator interface. It comes under "Microsoft.Practices.Prism.Events" namespace in Microsoft.Practices.Prism.dll. EventAggregator works as a mediator between publisher and subscriber objects for events. Whosever object wants to publish any event, need to communicate to EventAggregator by specifying parameter of event if any. Whosever object subscribes that event is notified by EventAggregator with published parameter (payload of event).

Following screenshot shows general pictorial description of EventAggregator with multiple publishers and subscribers.

general pictorial description of EventAggregator

In Demo application, EmployeeViewModel is playing role of publisher and RequestViewModel is playing role of subscriber. EventAggregator works as a mediator between EmployeeViewModel and RequestViewModel module. Following screenshot shows implementation of EventAggregator in Demo application:

general pictorial description of EventAggregator
 

How to Use EventAggregator

To use EventAggregator, there are some standard steps need to be followed. We will follow those steps in this section to use EventAggregator in order to fix the issue.

  1. First we need create a class inherited from "CompositePresentationEvent<TPayload>" that serve as event which we want to notify to different part of application. Already, in first part of the article series, we have added a class called EmployeeUpdatedEvent under Eventes folder in ITHelpDesk.Common.Infrastructure project. Typically such events are declared in a common project (here we have ITHelpDesk.Common.Infrastructure) because those events are supposed to be used in multiple projects in the application.

    When we use EventAggregator with Prism based application then we get generic class called "CompositePresentationEvent". Since it is generic class so we need to provide a specific type. This type of message will be passed to the subscribers. So we have created EmployeeUpdatedEvent class and inherited by CompositePresentationEvent class. With EmployeeUpdatedEvent publisher (EmpoyeeViewModel) want to publish latest Employee Id which is an integer. So the parent class of EmployeeUpdatedEvent is CompositePresentationEvent which takes message (Payload of event) type as int.
    public class EmployeeUpdatedEvent : CompositePresentationEvent<int> { }
    
    EmpoyeeViewModel need to notify RequestViewModel whenever Employee Id gets change. For that EmpoyeeViewModel will publish an event called EmployeeUpdatedEvent with latest Employee Id.
     
  2. Publisher and Subscriber object must be using global EventAggregator provided by DI (Dependency Injection) Container. So here we will use EventAggregator provided by Unity Container and it is initialized in UnityBootstrapper class. This is out of the box and we need to look into the code of ConfigureContainer method of UnityBootstrapper class. Below is the code taken from open source Prism Library Code from Codeplex.

    Configure Container Method

    So creation of global EventAggregator is already done as our ITHelpDeskBootstrapper class inherits from UnityBootstrapper.
     
  3. EmpoyeeViewModel will act as a publisher so we need to inject EventAggregator object. Now in constructor of EmpoyeeViewModel we are injecting IEventAggregator and initializing a private field eventAggregator in constructor itself as shown below:
    public EmployeeViewModel(IEmployeeService employeeService, IEventAggregator  eventAggregator)
    {
          this.eventAggregator = eventAggregator;
          this.employeeService = employeeService;
    }
    
  4. Employee Id needs to be published with updated value. So in the set block on "EmployeeId" property, we have added one line of code for publishing EmployeeUpdatedEvent after "if" block.
    public int EmployeeId
    {
            get { return employeeId; }
            set
               {
                if (value != 0)
                  {
                      employeeId = value;
                      fetchEmployeeDetail(employeeId);
                      RaisePropertyChanged("EmployeeId");
                  }
                eventAggregator.GetEvent<EmployeeUpdatedEvent>().Publish(value);
               }
    }
    
    Note: It is bit tricky to decide the right place for publishing and subscribing the event. To realize it, replace eventAggregator.GetEvent<EmployeeUpdatedEvent>().Publish(value); code at first line (just above if (value != 0) line) inside set block. Then we can see EventAggregator will not be helping us to fix the issue which we were facing.
     
  5. Any number of objects can subscribe published event (EmployeeUpdatedEvent). Let us see how to subscribe "EmployeeUpdatedEvent" event in RequestViewModel. In constructor of RequestViewModel we are subscribing for EmployeeUpdatedEvent. While subscribing we are passing a method (employeeUpdateHandler) which will be called when event will occur. In employeeUpdateHandler method we are calling fetchAllRequest method. As we can see from fetchAllRequest method, GetAllRequest method of EmployeeService is being called. Here we are getting updated empId in employeeUpdateHandler but we are not using it as we know that in EmployeeService, updated Employee Id will be available already. Below is the related code:
    public RequestViewModel(IEmployeeService employeeService,IEventAggregator eventAggregator)
    {
       this.employeeService = employeeService;
       this.eventAggregator = eventAggregator;
       eventAggregator.GetEvent<EmployeeUpdatedEvent>().Subscribe(employeeUpdateHandler);
       fetchAllRequest();
    }
    
    private void employeeUpdateHandler(int empId)
    {
       fetchAllRequest();
    }
    
    private void fetchAllRequest()
    {
        List<Request> listOfRequest = employeeService.GetAllRequest();
        AllRequests = new ObservableCollection<Request>(listOfRequest);
    }
    
    GetAllRequest method of Employee service is calling GetAllRequest method of repository by passing CurrentEmployeeId.
    public List<Request> GetAllRequest()
        {
            return dataSource.GetAllRequest(CurrentEmployeeId);
        }
    

Now run the application and combobox will always show request Ids associated with Employee Id.
 

What is IActiveAware

IActiveAware is an interface which is used if we need to perform some action on Activation or Deactivation of a View. If we are following MVVM, we should implement this interface in corresponding ViewModel class.

This interface has two members: a property called as IsActive and an event IsActiveChanged:

// Summary:
//     Interface that defines if the object instance is active and notifies when
//     the activity changes.
public interface IActiveAware
{
    // Summary:
    //     Gets or sets a value indicating whether the object is active.
    bool IsActive { get; set; }

    // Summary:
    //     Notifies that the value for Microsoft.Practices.Prism.IActiveAware.IsActive
    //     property has changed.
    event EventHandler IsActiveChanged;
}

When the View or corresponding ViewModel, gets active or deactivate, RegionActiveAwareBehavior class sets the new value of IsActive. We can use IsActiveChanged event to notify interested parties about Active status change.

New Requirement: We will understand IActiveAware by implementing a new scenario. Let's say we get a requirement that when user revisits any module, its controls must be refreshed. Those controls should not be holding old values. Those must be empty while revisiting again.

As of now if we make some selection in Software Module, and then Navigate to other module and revisit Software Module again we will see the old selected values in controls. Below screenshot is having steps to highlight this behavior in the demo we have developed so far:

Issue Without IActiveAware

In next section we will implement IActiveAware to meet this new requirement.

How to Use IActiveAware

First we will implement IActiveAware interface in SoftwareViewModel class. In order to reset the controls, we need to set bound properties with default values. Let us inherit SoftwareViewModel with IActiveAware interface and provide the implementation as shown in below code. Here refreshSoftwareModule method is being called from set block of IsActive property. In this method we are setting bound properties with default values.

#region IActiveAware implementation
bool active;
public bool IsActive
{
    get
    {
        return active;
    }
    set
    {
        if (active != value)
        {
            active = value;
            if (active == true)
                refreshSoftwareModule();
        }
    }
}

private void refreshSoftwareModule()
{
    SelectedSoftwareCategory = null;
    SelectedSoftwareName = null;
    Comment = null;
}

public event EventHandler IsActiveChanged;

#endregion

Now run the application and follow the steps from 1 to 7 as per above screenshot. At 7th step we will see controls of Software Module are refreshed as per the given new requirement as shown below:

With IActiveAware

In similar way we can implement IActiveAware interface for Hardware Module also.

Conclusion

In this part of the article series, we learned about how to use EventAggregator and IActiveAware in the Prism application. This is last part of the article series, hope you will find this series helpful. Your comments/suggestions and queries are most welcome. Thanks.

License

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

Share

About the Author

Snesh Prajapati
Software Developer
India India
I am a Software Developer working on Microsoft technologies. My interest is exploring and sharing the awesomeness of emerging technologies.

Comments and Discussions

 
QuestionGreat Article. This is the best article on WPF and Prism. Pin
krishnaaaaaaaa29-Jun-18 10:06
memberkrishnaaaaaaaa29-Jun-18 10:06 
QuestionVery good article madam! Pin
Raghavendran Srinivasan11-Dec-17 15:24
memberRaghavendran Srinivasan11-Dec-17 15:24 
QuestionNIce article Pin
lopezan6-Dec-17 0:36
memberlopezan6-Dec-17 0:36 
QuestionReally a great article series Pin
Member 1314292429-Jun-17 1:02
professionalMember 1314292429-Jun-17 1:02 
QuestionGood Starting Point for Prism 4.0, useful for new versions too... Pin
earthStrapped21-Jun-17 14:47
memberearthStrapped21-Jun-17 14:47 
AnswerRe: Good Starting Point for Prism 4.0, useful for new versions too... Pin
Snesh Prajapati21-Jun-17 16:13
professionalSnesh Prajapati21-Jun-17 16:13 
PraiseReally a helpful article Pin
koolkarthik24-Apr-17 19:22
memberkoolkarthik24-Apr-17 19:22 
AnswerRe: Really a helpful article Pin
Snesh Prajapati25-Apr-17 4:36
professionalSnesh Prajapati25-Apr-17 4:36 
QuestionBest article series on Prism Pin
@nkit Bajpai18-Apr-17 4:42
professional@nkit Bajpai18-Apr-17 4:42 
AnswerRe: Best article series on Prism Pin
Snesh Prajapati18-Apr-17 6:09
professionalSnesh Prajapati18-Apr-17 6:09 
GeneralRe: Best article series on Prism Pin
@nkit Bajpai2-May-17 3:05
professional@nkit Bajpai2-May-17 3:05 
AnswerRe: Best article series on Prism Pin
Snesh Prajapati2-May-17 15:28
professionalSnesh Prajapati2-May-17 15:28 
GeneralRe: Best article series on Prism Pin
@nkit Bajpai3-May-17 3:06
professional@nkit Bajpai3-May-17 3:06 
GeneralMy vote of 5 Pin
Santhakumar M5-Dec-15 7:43
professionalSanthakumar M5-Dec-15 7:43 
AnswerRe: My vote of 5 Pin
Snesh Prajapati5-Dec-15 15:39
professionalSnesh Prajapati5-Dec-15 15:39 
GeneralRe: My vote of 5 Pin
Santhakumar M5-Dec-15 19:01
professionalSanthakumar M5-Dec-15 19:01 
PraiseRe: My vote of 5 Pin
Snesh Prajapati5-Dec-15 20:09
professionalSnesh Prajapati5-Dec-15 20:09 

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.

Article
Posted 5 Dec 2015

Stats

15.7K views
932 downloads
11 bookmarked