-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
122 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace M.EventBroker | ||
{ | ||
/// <summary> | ||
/// Represents an event broker. | ||
/// </summary> | ||
public interface IEventBrokerAsync : IDisposable | ||
{ | ||
/// <summary> | ||
/// Adds subscription for events of type <typeparamref name="TEvent"/>. | ||
/// </summary> | ||
/// <typeparam name="TEvent">The type of the event.</typeparam> | ||
/// <param name="handler">A delegate that will be invoked when event is published.</param> | ||
/// <param name="filter">A delegate used to perform filtering of events before invoking the handler.</param> | ||
/// <param name="onError">Called when an error is caught during execution.</param> | ||
void Subscribe<TEvent>(Func<TEvent, Task> handler, Func<TEvent, Task<bool>> filter = null, Func<Exception, TEvent, Task> onError = null); | ||
|
||
/// <summary> | ||
/// Adds subscription for events of type <typeparamref name="TEvent"/>. | ||
/// </summary> | ||
/// <typeparam name="TEvent">The type of the event.</typeparam> | ||
/// <param name="handler">An instance of IEventHandler<TEvent> which Handle method will be invoked when event is published.</param> | ||
void Subscribe<TEvent>(IEventHandlerAsync<TEvent> handler); | ||
|
||
/// <summary> | ||
/// Removes subscription for events of type <typeparamref name="TEvent"/>. | ||
/// </summary> | ||
/// <typeparam name="TEvent">The type of the event.</typeparam> | ||
/// <param name="handler">A delegate to remove form subscribers.</param> | ||
void Unsubscribe<TEvent>(Func<TEvent, Task> handler); | ||
|
||
/// <summary> | ||
/// Removes subscription for events of type <typeparamref name="TEvent"/>. | ||
/// </summary> | ||
/// <typeparam name="TEvent">The type of the event.</typeparam> | ||
/// <param name="handler">An instance of IEventHandler<TEvent> to remove form subscribers.</param> | ||
void Unsubscribe<TEvent>(IEventHandlerAsync<TEvent> handler); | ||
|
||
/// <summary> | ||
/// Publishes an event of type <typeparamref name="TEvent"/>. | ||
/// </summary> | ||
/// <typeparam name="TEvent">The type of the event.</typeparam> | ||
/// <param name="event">An <typeparamref name="TEvent"/> instance to be passed to all handlers of the event.</param> | ||
/// <returns>The task object representing the asynchronous operation.</returns> | ||
Task Publish<TEvent>(TEvent @event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace M.EventBroker | ||
{ | ||
/// <summary> | ||
/// Represents a logic for handling events. | ||
/// </summary> | ||
/// <typeparam name="TEvent">The type of the event.</typeparam> | ||
public interface IEventHandlerAsync<TEvent> | ||
{ | ||
/// <summary> | ||
/// Handles the event. | ||
/// </summary> | ||
/// <param name="event">An instance of TEvent representing the event.</param> | ||
/// <returns>The task object representing the asynchronous operation.</returns> | ||
Task HandleAsync(TEvent @event); | ||
|
||
/// <summary> | ||
/// Returns a value indicating whether the event handler should be executed. | ||
/// </summary> | ||
/// <param name="event">An instance of TEvent representing the event.</param> | ||
/// <returns>A value indicating whether the event handler should be executed.</returns> | ||
Task<bool> ShouldHandleAsync(TEvent @event); | ||
|
||
/// <summary> | ||
/// Called when an error is caught during execution. | ||
/// </summary> | ||
/// <param name="exception">The exception caught.</param> | ||
/// <param name="event">The event instance which handling caused the exception.</param> | ||
/// <returns>The task object representing the asynchronous operation.</returns> | ||
Task OnErrorAsync(Exception exception, TEvent @event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace M.EventBroker | ||
{ | ||
/// <summary> | ||
/// Represents a provider for event handling instances. | ||
/// </summary> | ||
public interface IEventHandlerAsyncFactory | ||
{ | ||
/// <summary> | ||
/// Returns an event handling instances. | ||
/// </summary> | ||
/// <typeparam name="TEvent">The type of the event.</typeparam> | ||
/// <returns>An IEnumerable of event handlers. Null is valid return value.</returns> | ||
IEnumerable<IEventHandlerAsync<TEvent>> AsyncHandlersFor<TEvent>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace M.EventBroker | ||
{ | ||
/// <summary> | ||
/// Represents runner of event handlers. | ||
/// </summary> | ||
public interface IEventHandlerRunnerAsync : IDisposable | ||
{ | ||
/// <summary> | ||
/// Runs event handlers. | ||
/// </summary> | ||
/// <param name="handlers">The event handlers to run.</param> | ||
/// <returns>The task object representing the asynchronous operation.</returns> | ||
Task RunAsync(params Func<Task>[] handlers); | ||
} | ||
} |