DevExpress MVVM Framework. Interaction of ViewModels. Messenger. (original) (raw)
OTHER RELATED ARTICLES:
- Getting Started with DevExpress MVVM Framework. Commands and View Models.
- DevExpress MVVM Framework. Introduction to Services, DXMessageBoxService and DialogService.
- DevExpress MVVM Framework. Interaction of ViewModels. IDocumentManagerService.
- DevExpress MVVM Framework. Introduction to POCO ViewModels.
- THIS POST: DevExpress MVVM Framework. Interaction of ViewModels. Messenger.
- DevExpress MVVM Framework. Using Scaffolding Wizards for building Views.
- DevExpress MVVM Framework. Data validation. Implementing IDataErrorInfo.
- DevExpress MVVM Framework. Using DataAnnotation attributes and DevExpress Fluent API.
- DevExpress MVVM Framework. Behaviors.
- DevExpress MVVM Framework. TaskbarButtonService, ApplicationJumpListService and NotificationService.
- DevExpress MVVM Framework. Asynchronous Commands.
- DevExpress MVVM Framework. Converters.
An application’s architecture depends on the degree of connection between its modules. Loosely-coupled systems are suited for large applications. This usually means many scattered modules, operating without awareness of each other. Ideally, modules are the building blocks of an adaptive design. Loosely-coupled architectures are easy to support and improve because adding or removing functionality simply means registering or unregistering a specific module without concern towards the others.
To facilitate interaction between modules, we implemented aclass to exchange messages regardless of which module is sending or receiving the message. This class is Messenger.
Let’s examine a simple sample to get a clearer understanding.
Imagine we have a database which can be modified from several modules. One module will need to be notified when modifications happen (e.g., adding, removing, and changing a record). We’ll first need to create a message:
1: public enum MessageType { Added, Deleted, Changed }
2: public class Message {
3: public MessageType MessageType { get; private set; }
4: public object RecordID { get; private set; }
5: public Message(object recordID, MessageType messageType) {
6: RecordID = recordID;
7: MessageType = messageType;
8: }
9: }
We can then subscribe to the message from anywhere in the application. For instance:
1: public class Module1 {
2: public Module1() {
3: Messenger.Default.Register(this, OnMessage);
4: }
5: void OnMessage(Message message) {
6: switch(message.MessageType) {
7: case MessageType.Added:
8: //...
9: break;
10: case MessageType.Changed:
11: //...
12: break;
13: case MessageType.Deleted:
14: //...
15: break;
16: default:
17: throw new NotImplementedException();
18: }
19: }
20: }
Sending a message is even easier:
1: public class Module2 {
2: void SendMessage() {
3: Messenger.Default.Send(new Message(0, MessageType.Added));
4: }
5: }
As you can see, this approach implements module interaction without reference to a module’s code. Even if you remove Module1 or Module2 while developing the application, it will not cause errors and the entire system will continue to function.
We also prepared a real example with this architecture. It can be found here. Screenshots of the example are below.
Free DevExpress Products - Get Your Copy Today
The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.