Skip to content

Instantly share code, notes, and snippets.

@yreynhout
Created April 15, 2015 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yreynhout/a7ed8a6ba41a6abf11f2 to your computer and use it in GitHub Desktop.
Save yreynhout/a7ed8a6ba41a6abf11f2 to your computer and use it in GitHub Desktop.
Projac Generic ConnectedProjection
/// <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(); }
}
}
/// <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