Skip to content

Commit

Permalink
Merge pull request #253 from jbogard/consolidate-handlers
Browse files Browse the repository at this point in the history
Consolidating some handlers; renamed some things for posterity
  • Loading branch information
jbogard authored Apr 9, 2018
2 parents 2ec52c1 + d858b7d commit a833811
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 47 deletions.
2 changes: 1 addition & 1 deletion samples/MediatR.Examples/JingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public JingHandler(TextWriter writer)
_writer = writer;
}

protected override Task Handle(Jing request)
protected override Task Handle(Jing request, CancellationToken cancellationToken)
{
return _writer.WriteLineAsync($"--- Handled Jing: {request.Message}, no Jong");
}
Expand Down
20 changes: 1 addition & 19 deletions src/MediatR/INotificationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface INotificationHandler<in TNotification>
public abstract class NotificationHandler<TNotification> : INotificationHandler<TNotification>
where TNotification : INotification
{
public Task Handle(TNotification notification, CancellationToken cancellationToken)
Task INotificationHandler<TNotification>.Handle(TNotification notification, CancellationToken cancellationToken)
{
Handle(notification);
return Unit.Task;
Expand All @@ -37,22 +37,4 @@ public Task Handle(TNotification notification, CancellationToken cancellationTok
/// <param name="notification">Notification</param>
protected abstract void Handle(TNotification notification);
}

/// <summary>
/// Wrapper class for an async notification handler, ignoring the cancellation token
/// </summary>
/// <typeparam name="TNotification">The notification type</typeparam>
public abstract class AsyncNotificationHandler<TNotification> : INotificationHandler<TNotification>
where TNotification : INotification
{
public Task Handle(TNotification notification, CancellationToken cancellationToken)
=> Handle(notification);

/// <summary>
/// Override in a derived class for the handler logic
/// </summary>
/// <param name="notification">Notification</param>
/// <returns>A task</returns>
protected abstract Task Handle(TNotification notification);
}
}
33 changes: 7 additions & 26 deletions src/MediatR/IRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,45 +30,26 @@ public interface IRequestHandler<in TRequest> : IRequestHandler<TRequest, Unit>
{
}


/// <summary>
/// Wrapper class for a handler that asynchronously handles a request and returns a response, ignoring the cancellation token
/// </summary>
/// <typeparam name="TRequest">The type of request being handled</typeparam>
/// <typeparam name="TResponse">The type of response from the handler</typeparam>
public abstract class AsyncRequestHandler<TRequest, TResponse> : IRequestHandler<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
public Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken)
=> Handle(request);

/// <summary>
/// Override in a derived class for the handler logic
/// </summary>
/// <param name="request">Request</param>
/// <returns>Response</returns>
protected abstract Task<TResponse> Handle(TRequest request);
}

/// <summary>
/// Wrapper class for a handler that asynchronously handles a request and does not return a response, ignoring the cancellation token
/// Wrapper class for a handler that asynchronously handles a request and does not return a response
/// </summary>
/// <typeparam name="TRequest">The type of request being handled</typeparam>
public abstract class AsyncRequestHandler<TRequest> : IRequestHandler<TRequest>
where TRequest : IRequest
{
public async Task<Unit> Handle(TRequest request, CancellationToken cancellationToken)
async Task<Unit> IRequestHandler<TRequest, Unit>.Handle(TRequest request, CancellationToken cancellationToken)
{
await Handle(request).ConfigureAwait(false);
await Handle(request, cancellationToken).ConfigureAwait(false);
return Unit.Value;
}

/// <summary>
/// Override in a derived class for the handler logic
/// </summary>
/// <param name="request">Request</param>
/// <param name="cancellationToken"></param>
/// <returns>Response</returns>
protected abstract Task Handle(TRequest request);
protected abstract Task Handle(TRequest request, CancellationToken cancellationToken);
}

/// <summary>
Expand All @@ -79,7 +60,7 @@ public async Task<Unit> Handle(TRequest request, CancellationToken cancellationT
public abstract class RequestHandler<TRequest, TResponse> : IRequestHandler<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
public Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken)
Task<TResponse> IRequestHandler<TRequest, TResponse>.Handle(TRequest request, CancellationToken cancellationToken)
=> Task.FromResult(Handle(request));

/// <summary>
Expand All @@ -97,7 +78,7 @@ public Task<TResponse> Handle(TRequest request, CancellationToken cancellationTo
public abstract class RequestHandler<TRequest> : IRequestHandler<TRequest>
where TRequest : IRequest
{
public Task<Unit> Handle(TRequest request, CancellationToken cancellationToken)
Task<Unit> IRequestHandler<TRequest, Unit>.Handle(TRequest request, CancellationToken cancellationToken)
{
Handle(request);
return Unit.Task;
Expand Down
4 changes: 3 additions & 1 deletion test/MediatR.Tests/SendVoidInterfaceTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Threading;

namespace MediatR.Tests
{
using System.IO;
Expand All @@ -20,7 +22,7 @@ public class PingHandler : AsyncRequestHandler<Ping>

public PingHandler(TextWriter writer) => _writer = writer;

protected override Task Handle(Ping request)
protected override Task Handle(Ping request, CancellationToken cancellationToken)
=> _writer.WriteAsync(request.Message + " Pong");
}

Expand Down

0 comments on commit a833811

Please sign in to comment.