C# View Model Messenger Service

C# View Model Messenger Service

When building a WPF application using the Inversion of Control (IOC) Principle with dependency injection we often need to inject view models into view models so we can share the data, events or property’s. This tightly couples the view models together which violates the IOC principle. To get around this we want to use the Messenger Service pattern to communicate between the view models and share data.

Messenger Service

Why Use a Custom Messaging System?
Off-the-shelf messaging solutions may not always align perfectly with the requirements of your web application. By developing a custom messaging system, you gain full control over how messages are sent, received, and processed. This enables you to tailor the messaging system to the specific needs of your application, resulting in better performance and scalability.

Key Components of the Messaging System

  • Messenger Class: The heart of the messaging system, the Messenger class provides methods for sending and subscribing to messages of various types.
  • Subscription Record: A simple record type that encapsulates a subscriber object and the action to be performed when a message is received.

Functionality Overview

  1. Sending Messages: The Send method allows messages of any type to be sent to subscribers. It updates the current state of the message and notifies all subscribers associated with the message type.
  2. Subscribing to Messages: The Subscribe method enables objects to subscribe to messages of a specific type. Subscribers provide an action that is executed when a message of the subscribed type is received.
  3. Unsubscribing from Messages: Subscribers can unsubscribe from receiving messages of a particular type using the Unsubscribe method.

Integration with WPF Applications

  • Real-time Updates: Integrate the messaging system to provide real-time updates to users, such as live chat features, notifications, or dynamic content updates.
  • Event Triggering: Use the messaging system to trigger events based on user actions or system events, enhancing the interactivity of your web application.
  • Customized Interactions: Tailor the messaging system to meet the specific requirements of your web application, ensuring seamless integration and optimal performance.

Messenger Service Code

C#

Integrating into a WPF Application

See my post on dependency injection if you need some help.

App.Xaml.cs

In the services section of the App.XAML.cs file we need to AddSingleton for the Messenger service as shown below.

C#

Message Records

I added a folder for messages and created a new class called MessageRecords. The class it’s self gets deleted so we can create this standalone record object. The OnlineStatusChangedMessage record represents a message used within a messaging system to convey changes in online status. Specifically, it encapsulates a boolean value indicating whether a user has transitioned to an online state or not.

Message Record

Subscribing View Model

Within a View Model like the Main View Model we add a reference in the constructor to the Messenger interface.

C#
C#
Updating the Property

We can then add a subscription and a method to handle the message.

C#

Sending the Message

From another view model we can now use the Message Record to send a message to the Main View Model that the Online Status has changed.

C#

Conclusion

Implementing a custom messaging system in C# empowers developers to create highly interactive and responsive WPF applications. By leveraging the flexibility and control offered by a custom solution, developers can design messaging systems that seamlessly integrate with their applications, delivering enhanced user experiences and improved functionality.

With the insights provided in this article, you’re equipped to explore the possibilities of integrating a custom messaging system into your WPF applications, unlocking new avenues for real-time communication and interaction.

Example

App.xaml

XAML

App.xaml.cs

C#

Commands Base

C#

View Model Base

C#

Message Records

C#

Navigation Store

C#

Left View Model

C#

Right View Model

C#

Main View Model

C#

Left View

XAML

Right View

XAML

appsettings.json (Content, Copy if Newer)

YAML

MainWindow.xaml

XAML

MainWindow.xaml.cs

C#