Quantcast
Channel: Developer Express Global
Viewing all articles
Browse latest Browse all 3420

Blog Post: DevExpress MVVM Framework. Interaction of ViewModels. Messenger.

$
0
0

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:publicenum MessageType { Added, Deleted, Changed }
   2:publicclass Message {
   3:public MessageType MessageType { get; private set; }
   4:publicobject 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:publicclass Module1 {
   2:public Module1() {
   3:         Messenger.Default.Register<Message>(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:thrownew NotImplementedException();
  18:         }
  19:     }
  20: }

Sending a message is even easier:

   1:publicclass 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.

4.Blog.Messaging.001

4.Blog.Messaging.002

OTHER RELATED ARTICLES:

  1. Getting Started with DevExpress MVVM Framework. Commands and View Models.
  2. DevExpress MVVM Framework. Introduction to Services, DXMessageBoxService and DialogService.
  3. DevExpress MVVM Framework. Interaction of ViewModels. IDocumentManagerService.
  4. DevExpress MVVM Framework. Introduction to POCO ViewModels.
  5. THIS POST: DevExpress MVVM Framework. Interaction of ViewModels. Messenger.

Viewing all articles
Browse latest Browse all 3420

Latest Images

Trending Articles



Latest Images