Skip to content
Marek Fišera edited this page Jan 2, 2016 · 7 revisions

Events provides a way to decouple processes in an complex application. Communication between two (or more) parts of the application is abstracted by an event bus and POCO-style class describing parameters of the event.

Event types

The events are aggregated by the type of event parameters.

public class UserRegistered
{
    public Int32Key NewUserKey { get; private set; }

    ...
}

As you can see, events are named is pass tence and are immutable. It's because an event represents action that already occurred and thus should be modified. The event object can be of any type, user or system defined.

The events should be as small as possible. You should avoid events like UserUpdated and instead use UserNameChanged, UserLoginDisabled and etc.

Handlers

Each event has registered handlers, processes that needs to know about particular changes. The cardinality of these handlers of from zero (no one is interested in the particular event) to N (every wants to hear about changes). Such handler must implement IEventHandler with particular type of the event and register itself to be notified.

public class GreetingEmailSender : IEventHandler<UserRegisted>
{
    public Task HandleAsync(UserRegistered payload)
    {
        //TODO: Send email to the newly registered user.
    }
}

Another useful event handler could be "home directory creator". Instead of this all stuff having in the main registration component, using events you can extract it and even conditionally register.

Handler registration

Each handler that should be notified must registered. This registration is very implementation specific. For manual registration there is IEventHandlerCollection prepared, but you can easily implement automatic type discovery and registration.

Dispatching

For simple and generic event publishing the package contains IEventDispatcher which has one method for publishing new events. Based on implementation published events can be persisted for reliable delivery, can be raised asynchronously, can be distributed over the wire to the other servers (or clients) or can be processed any way the business requires it, as all events pass through the event dispatcher.

Extensions

We have implemented Behaviors decoration of handlers for interception and have proposal for distribution of the events over the wire. Also in EventSourcing there is an event sourcing based implementation of the event dispatcher.

Clone this wiki locally