Skip to content

NatanaelLemos/N2tl.EventBroker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

N2tl.EventBroker

Implementation of the Observer pattern in the form of an EventBroker.

You can find this project published at https://www.nuget.org/packages/N2tl.EventBroker/

Usage

To inject it to your dependency container, follow:

With IServiceCollection

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddEventBroker();
        }

Without IServiceCollection

   IEventBroker eventBroker = EventBrokerBuilder.Build();

Optionally, the EventBroker can be configured with interrupters.

Once interrupters are added to the pipeline, events will only go through if the condition in any interrupter returns true.

ObserverBuilder with event specific interrupters.

        public void ConfigureServices(IServiceCollection services)
        {
            Func<MyEventType, Task<bool>> interrupter = t => Task.FromResult(true);

            services.AddEventBroker(opt => opt.AddEventInterrupter(interrupter));
        }

        or

        Func<MyEventType, Task<bool>> interrupter = t => Task.FromResult(true);
        EventBrokerBuilder.Build(opt => opt.AddEventInterrupter(interrupter));

ObserverBuilder with general interrupters.

        public void ConfigureServices(IServiceCollection services)
        {
            Func<object, Task<bool>> interrupter = t => Task.FromResult(true);

            services.AddEventBroker(opt => opt.AddGeneralInterrupter(interrupter));
        }

        or

        Func<object, Task<bool>> interrupter = t => Task.FromResult(true);
        EventBrokerBuilder.Build(opt => opt.AddGeneralInterrupter(interrupter));


After injecting the observer, you'll have access to an instance of IEventBroker from which you can subscribe and notify events.

private readonly IEventBroker _eventBroker;

public MyClass(IEventBroker eventBroker)
{
    _eventBroker = eventBroker;
    _eventBroker.Subscribe<MyEventType>(MyEventTypeHandler);
}

private Task MyEventTypeHandler(MyEventType arg)
{
    // Do whatever.
}

private async Task AnotherMethod()
{
    await _eventBroker.Notify(new MyEventType());
}

About

CQRS implementation in the form of an EventBroker.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published