Skip to content

Eventing

Michael Vivet edited this page Jan 30, 2018 · 49 revisions

Table of Contents


Summary

Nano supports different ways of publishing and subscribing to events.

Adding eventing annotations to model implementations, provides a way of synchronizing entities between applications. Additionally, custom event models can be implemented and published, and consumed through subscriptions.

The ILoggingProvider is registered during startup, and the implementing type defines the eventing provider used in the application. Furthmore, the interface IEventing is registered as well, and defines the entry to publishing and subscribing to events.


Configuration

The Eventing section of the configuration defines the behavior of eventing (message queuing) in the application.

The section is serialized into an instance of EventingOptions, and injected as dependency during startup, thus available for injection throughout the application.

See Appendix - App Settings - Eventing for details about the section and the meaning of the variables.

Eventing Section
"Eventing": {
  "Host": null,
  "VHost": null,
  "Port": 5672,
  "AuthenticationCredential": {
    "Username": null,
    "Password": null
  },
  "UseSsl": false,
  "Timeout": 30,
  "Heartbeat": 0
}

Eventing Provider

Nano provides several eventing providers (and more added on request), so usually there is no need to implement a custom data provider for your application.

Eventing providers implements the interface IEventingProvider. It contains a single method Configure(...), that is responsible for handling any configuration and setup required for the eventing provider.

The eventing providers currently supported by Nano, can be referenced in the Appendix - Supported Providers.


Dependency Injection

The eventing provider must be registered as dependencies. Invoke the method .AddEventing<TProvider>();, using the eventing provider implementation as generic type parameters.

Sample Implementation
.ConfigureServices(x =>
{
    x.AddEventing<EasyNetQProvider>();
})
Injected Dependencies
  • Nano.Eventing.EventingOptions
  • Nano.Eventing.Interfaces.IEventing
  • Nano.Eventing.Interfaces.IEventingProvider
  • Nano.Eventing.Interfaces.IEventingHandler<>

For a full list of supported dependencies, see Appendix: Injected Dependencies.


Publishing Event

An event can be published from anywhere within the application.

Nano controllers has the IEventing dependency injected, and publishing from one of it's actions is recommended. Either override an existing inherited action or implement a new one.

Sample Implementation
public Task Publish()
{
    var @event = new MyEvent();
    this.Eventing.PublishAsync(@event);
}

Null Eventing

By default, IEventing resolves to NullEventing, which as the name indicates doesn't publish or subscribe to anything. Registering an eventing provider dependency, will override the NullEventing with the configured eventing provider implementation.


Subscription Handler

Subscribing to an event requires an event handler implementation.

Implementing the interface IEventingHandler<TEvent> ensures that the event handler is registered during startup, and the subscription is configured to listen for incoming events. The method CallbackAsync(...) is invoked for evert event received.

It's recommended to share the event model, as both the publisher and subscriber must understand the object model.

using System;
using Nano.Eventing.Interfaces;
using Nano.Services.Interfaces;

namespace Org
{
    public class MyEventHandler : IEventingHandler<MyEvent>
    {
        protected virtual IService Service { get; }

        public MyEventHandler(IService service)
        {
            this.Service = service;
        }

        public void CallbackAsync(MyEvent @event)
        {
            // Event logic.
        }
    }
}

Clone this wiki locally