CDPMessageBus - STARIONGROUP/COMET-SDK-Community-Edition GitHub Wiki

Introduction

The CDP Message Bus is an implementation of the Event Aggregator pattern. The CDP4-COMET-SDK is used to develop GUI applications that should be loosely coupled. Multiple event sources and sinks need to be able to send and receive events, without being aware of eachother.

An Event Aggregator can also generalize the event, converting events that are specific to a source object into a more generic event. That way the observers of the aggregators don't need to register for as many individual event types. This simplifies the registration process for observers, at the cost of being notified of events that may not have any material effect on the observer.

Singleton

The CDP Message Bus is implemented as a singleton. It is availble through the static getter Current.

// the ClearSubscriptions method clears all the subscriptions that listeners may have
CDPMessageBus.Current.ClearSubscriptions();

Generic Send and Listen

The CDP Message Bus can be used to send any object via the bus to any listener. The following methods are used for this purpose:

  1. public IObservable<T> Listen<T>(object target = null, string contract = null)
  2. public void SendMessage<T>(T message, object target = null, string contract = null)

With the Listen<T> message, a listener can subscribe to any messages of type T. With the SendMessage<T> a message of type T can be sent, that any listener listening on that type T can respond to. The Listen<T> method returns an IObservable<T> that can be subscribed to.

SessionEvent

The generic SendMessage method is used to send SessionEvent that are used to notify listeners when a Session is opened or closed using the Dal

// subscribe to SessionEvent messages
CDPMessageBus.Current.Listen<SessionEvent>().Subscribe(x =>
    {
        Console.Write("received");
    });

// create a session object
var uri = new Uri("https://cdp4services-public.cdp4.org");
var credentials = new Credentials("some-user-name", "some-password", uri);
var dal = new CdpServicesDal();
var session = new Session(dal, credentials);

// create and send a Session open message
var sessionEvent = new SessionEvent(session, SessionStatus.Open);
CDPMessageBus.Current.SendMessage(sessionEvent);

SendObjectChangeEvent

The SendObjectChangeEvent is a method specialized in sending POCO update messages via the Message Bus to any listener. The Assembler makes use of this method to notify listeners that a Thing has been added, updated or deleted. This mechanims provides the means to implement GUI's and business logic that respond to changes on Thing objects and the Cache.

public void SendObjectChangeEvent(Thing thing, EventKind eventKind)

The thing and eventkind are the payload of the ObjectChangedEvent that is being sent when this method is used.

var thing = new ElementDefinition();
CDPMessageBus.Current.SendObjectChangeEvent(thing, EventKind.Added);

The above code snipped sends an ObjectChangedEvent that has as payload an ElementDefinition object and an eventkind that states that it has been added. It also sends this message for any super class of the Thing payload. This makes it possible to also listen for generalized messages. If a listener registers to listen for all Thing message updates, any kind of thing that is updated will be received by this listener.