Created
April 15, 2015 21:17
-
-
Save yreynhout/a7ed8a6ba41a6abf11f2 to your computer and use it in GitHub Desktop.
Projac Generic ConnectedProjection
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
/// <summary> | |
/// Represent a connected projection. | |
/// </summary> | |
public abstract class ConnectedProjection<TConnection> | |
{ | |
private readonly List<ConnectedProjectionHandler<TConnection>> _handlers; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="ConnectedProjection{TConnection}" /> class. | |
/// </summary> | |
protected ConnectedProjection() | |
{ | |
_handlers = new List<ConnectedProjectionHandler<TConnection>>(); | |
} | |
/// <summary> | |
/// Specifies the message handler to be invoked when a particular message occurs. | |
/// </summary> | |
/// <typeparam name="TMessage">The type of the message.</typeparam> | |
/// <param name="handler">The message handler.</param> | |
/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="handler" /> is <c>null</c>.</exception> | |
protected void When<TMessage>(Func<TConnection, TMessage, Task> handler) | |
{ | |
if (handler == null) throw new ArgumentNullException("handler"); | |
_handlers.Add( | |
new ConnectedProjectionHandler<TConnection>( | |
typeof (TMessage), | |
(connection, message, token) => handler(connection, (TMessage) message))); | |
} | |
/// <summary> | |
/// Specifies the message handler to be invoked when a particular message occurs. | |
/// </summary> | |
/// <typeparam name="TMessage">The type of the message.</typeparam> | |
/// <param name="handler">The message handler.</param> | |
/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="handler" /> is <c>null</c>.</exception> | |
protected void When<TMessage>(Func<TConnection, TMessage, CancellationToken, Task> handler) | |
{ | |
if (handler == null) throw new ArgumentNullException("handler"); | |
_handlers.Add( | |
new ConnectedProjectionHandler<TConnection>( | |
typeof (TMessage), | |
(connection, message, token) => handler(connection, (TMessage) message, token))); | |
} | |
/// <summary> | |
/// Gets a read only collection of projection handlers. | |
/// </summary> | |
/// <value> | |
/// The projection handlers associated with this specification. | |
/// </value> | |
public ConnectedProjectionHandler<TConnection>[] Handlers | |
{ | |
get { return _handlers.ToArray(); } | |
} | |
} |
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
/// <summary> | |
/// Represents a handler of a particular type of message. | |
/// </summary> | |
public class ConnectedProjectionHandler<TConnection> | |
{ | |
private readonly Type _message; | |
private readonly Func<TConnection, object, CancellationToken, Task> _handler; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="ConnectedProjectionHandler{TConnection}" /> class. | |
/// </summary> | |
/// <param name="message">The message.</param> | |
/// <param name="handler">The handler.</param> | |
/// <exception cref="System.ArgumentNullException"> | |
/// Throw when <paramref name="message" /> or <paramref name="handler" /> is | |
/// <c>null</c>. | |
/// </exception> | |
public ConnectedProjectionHandler(Type message, Func<TConnection, object, CancellationToken, Task> handler) | |
{ | |
if (message == null) throw new ArgumentNullException("message"); | |
if (handler == null) throw new ArgumentNullException("handler"); | |
_message = message; | |
_handler = handler; | |
} | |
/// <summary> | |
/// The type of message to handle. | |
/// </summary> | |
public Type Message | |
{ | |
get { return _message; } | |
} | |
/// <summary> | |
/// The function that handles the message. | |
/// </summary> | |
public Func<TConnection, object, CancellationToken, Task> Handler | |
{ | |
get { return _handler; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment