Events handling

The event model used in the API is of the type pub/sub and events are only fired in the same application that raised the event.

Event broker service

The event broker is used to handle events. When using multiple applications, web farms or multiple developers with the same database, the event synchronization between all applications are made with a service bus.

The service Litium.Events.EventBroker is responsible to:

  • subscribe event notifications
  • publish events to all subscriptions.

When registered for an event, the event broker will return a handle for the actual listener, the handle will be used to unsubscribe the event.

using Litium.Runtime;

[Autostart]
public class EventBrokerExample : IDisposable
{
    private readonly Litium.Events.ISubscription<Litium.Globalization.Events.CurrencyCreated> _subscription;

    public EventBrokerExample(Litium.Events.EventBroker eventBroker)
    {
        _subscription = eventBroker.Subscribe<Litium.Globalization.Events.CurrencyCreated>(currencyEvent =>
        {
            // event handler for Litium.Globalization.Events.CurrencyCreated event.
        });
    }

    public void Dispose()
    {
        // unregister the event from event broker
        _subscription.Dispose();
    }
}

To publish an event the EventBroker.Publish method is used.

public class EventBrokerNotifyExample
{
    private readonly EventBroker _eventBroker;

    public EventBrokerNotifyExample(EventBroker eventBroker)
    {
        _eventBroker = eventBroker;
    }

    public void Send()
    {
        // send event with the event broker
        _eventBroker.Publish(new MyEvent(new Currency("SEK")));
    }
}
Is this page helpful?
Thank you for your feedback!