Skip to content

Commit

Permalink
Scaling back generic constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
jbogard committed Feb 7, 2023
1 parent caff80e commit e611b1b
Show file tree
Hide file tree
Showing 14 changed files with 27 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace MediatR.Examples;

public class ConstrainedRequestPostProcessor<TRequest, TResponse>
: IRequestPostProcessor<TRequest, TResponse>
where TRequest : Ping, IRequest<TResponse>
where TRequest : Ping
{
private readonly TextWriter _writer;

Expand Down
10 changes: 5 additions & 5 deletions samples/MediatR.Examples/ExceptionHandler/LogExceptionAction.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using MediatR.Pipeline;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace MediatR.Examples.ExceptionHandler;

public class LogExceptionAction : RequestExceptionAction<Ping>
public class LogExceptionAction : IRequestExceptionAction<Ping>
{
private readonly TextWriter _writer;

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

protected override void Execute(Ping request, Exception exception)
{
_writer.WriteLineAsync($"--- Exception: '{exception.GetType().FullName}'");
}
public Task Execute(Ping request, Exception exception, CancellationToken cancellationToken)
=> _writer.WriteLineAsync($"--- Exception: '{exception.GetType().FullName}'");
}
1 change: 0 additions & 1 deletion samples/MediatR.Examples/GenericPipelineBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace MediatR.Examples;

public class GenericPipelineBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
private readonly TextWriter _writer;

Expand Down
1 change: 0 additions & 1 deletion samples/MediatR.Examples/GenericRequestPostProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
namespace MediatR.Examples;

public class GenericRequestPostProcessor<TRequest, TResponse> : IRequestPostProcessor<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
private readonly TextWriter _writer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
namespace MediatR.Examples;

public class GenericStreamPipelineBehavior<TRequest, TResponse> : IStreamPipelineBehavior<TRequest, TResponse>
where TRequest : IStreamRequest<TResponse>
{
private readonly TextWriter _writer;

Expand Down
2 changes: 1 addition & 1 deletion src/MediatR/IPipelineBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace MediatR;
/// </summary>
/// <typeparam name="TRequest">Request type</typeparam>
/// <typeparam name="TResponse">Response type</typeparam>
public interface IPipelineBehavior<in TRequest, TResponse> where TRequest : IBaseRequest
public interface IPipelineBehavior<in TRequest, TResponse> where TRequest : notnull
{
/// <summary>
/// Pipeline handler. Perform any additional behavior and await the <paramref name="next"/> delegate as necessary
Expand Down
3 changes: 1 addition & 2 deletions src/MediatR/IStreamPipelineBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ namespace MediatR;
/// </summary>
/// <typeparam name="TRequest">Request type</typeparam>
/// <typeparam name="TResponse">Response type</typeparam>
public interface IStreamPipelineBehavior<in TRequest, TResponse>
where TRequest : IStreamRequest<TResponse>
public interface IStreamPipelineBehavior<in TRequest, TResponse> where TRequest : notnull
{
/// <summary>
/// Stream Pipeline handler. Perform any additional behavior and iterate the <paramref name="next"/> delegate as necessary
Expand Down
16 changes: 9 additions & 7 deletions src/MediatR/Pipeline/IRequestExceptionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace MediatR.Pipeline;
/// <typeparam name="TResponse">Response type</typeparam>
/// <typeparam name="TException">Exception type</typeparam>
public interface IRequestExceptionHandler<in TRequest, TResponse, in TException>
where TRequest : IRequest<TResponse>
where TRequest : notnull
where TException : Exception
{
/// <summary>
Expand All @@ -31,7 +31,7 @@ public interface IRequestExceptionHandler<in TRequest, TResponse, in TException>
/// <typeparam name="TRequest">Request type</typeparam>
/// <typeparam name="TResponse">Response type</typeparam>
public interface IRequestExceptionHandler<in TRequest, TResponse> : IRequestExceptionHandler<TRequest, TResponse, Exception>
where TRequest : IRequest<TResponse>
where TRequest : notnull
{
}

Expand All @@ -41,10 +41,12 @@ public interface IRequestExceptionHandler<in TRequest, TResponse> : IRequestExce
/// <typeparam name="TRequest">Request type</typeparam>
/// <typeparam name="TResponse">Response type</typeparam>
public abstract class AsyncRequestExceptionHandler<TRequest, TResponse> : IRequestExceptionHandler<TRequest, TResponse>
where TRequest : IRequest<TResponse>
where TRequest : notnull
{
async Task IRequestExceptionHandler<TRequest, TResponse, Exception>.Handle(TRequest request, Exception exception, RequestExceptionHandlerState<TResponse> state, CancellationToken cancellationToken)
=> await Handle(request, exception, state, cancellationToken).ConfigureAwait(false);
async Task IRequestExceptionHandler<TRequest, TResponse, Exception>.Handle(TRequest request, Exception exception, RequestExceptionHandlerState<TResponse> state, CancellationToken cancellationToken)
{
await Handle(request, exception, state, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Override in a derived class for the handler logic
Expand All @@ -63,7 +65,7 @@ async Task IRequestExceptionHandler<TRequest, TResponse, Exception>.Handle(TRequ
/// <typeparam name="TResponse">Response type</typeparam>
/// <typeparam name="TException">Exception type</typeparam>
public abstract class RequestExceptionHandler<TRequest, TResponse, TException> : IRequestExceptionHandler<TRequest, TResponse, TException>
where TRequest : IRequest<TResponse>
where TRequest : notnull
where TException : Exception
{
Task IRequestExceptionHandler<TRequest, TResponse, TException>.Handle(TRequest request, TException exception, RequestExceptionHandlerState<TResponse> state, CancellationToken cancellationToken)
Expand All @@ -87,7 +89,7 @@ Task IRequestExceptionHandler<TRequest, TResponse, TException>.Handle(TRequest r
/// <typeparam name="TRequest">Request type</typeparam>
/// <typeparam name="TResponse">Response type</typeparam>
public abstract class RequestExceptionHandler<TRequest, TResponse> : IRequestExceptionHandler<TRequest, TResponse>
where TRequest : IRequest<TResponse>
where TRequest : notnull
{
Task IRequestExceptionHandler<TRequest, TResponse, Exception>.Handle(TRequest request, Exception exception, RequestExceptionHandlerState<TResponse> state, CancellationToken cancellationToken)
{
Expand Down
2 changes: 1 addition & 1 deletion src/MediatR/Pipeline/IRequestPostProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace MediatR.Pipeline;
/// </summary>
/// <typeparam name="TRequest">Request type</typeparam>
/// <typeparam name="TResponse">Response type</typeparam>
public interface IRequestPostProcessor<in TRequest, in TResponse> where TRequest : IRequest<TResponse>
public interface IRequestPostProcessor<in TRequest, in TResponse> where TRequest : notnull
{
/// <summary>
/// Process method executes after the Handle method on your handler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ namespace MediatR.Pipeline;
using System.Threading.Tasks;

/// <summary>
/// Behavior for executing all <see cref="IRequestExceptionAction{TRequest,TException}"/>
/// or <see cref="RequestExceptionAction{TRequest,TResponse}"/> instances
/// Behavior for executing all <see cref="IRequestExceptionAction{TRequest,TException}"/> instances
/// after an exception is thrown by the following pipeline steps
/// </summary>
/// <typeparam name="TRequest">Request type</typeparam>
/// <typeparam name="TResponse">Response type</typeparam>
public class RequestExceptionActionProcessorBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
where TRequest : notnull
{
private readonly IServiceProvider _serviceProvider;

Expand Down
2 changes: 1 addition & 1 deletion src/MediatR/Pipeline/RequestExceptionProcessorBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace MediatR.Pipeline;
/// <typeparam name="TRequest">Request type</typeparam>
/// <typeparam name="TResponse">Response type</typeparam>
public class RequestExceptionProcessorBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
where TRequest : notnull
{
private readonly IServiceProvider _serviceProvider;

Expand Down
2 changes: 1 addition & 1 deletion src/MediatR/Pipeline/RequestPostProcessorBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace MediatR.Pipeline;
/// <typeparam name="TRequest">Request type</typeparam>
/// <typeparam name="TResponse">Response type</typeparam>
public class RequestPostProcessorBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
where TRequest : notnull
{
private readonly IEnumerable<IRequestPostProcessor<TRequest, TResponse>> _postProcessors;

Expand Down
2 changes: 1 addition & 1 deletion src/MediatR/Pipeline/RequestPreProcessorBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace MediatR.Pipeline;
/// <typeparam name="TRequest"></typeparam>
/// <typeparam name="TResponse"></typeparam>
public class RequestPreProcessorBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
where TRequest : notnull
{
private readonly IEnumerable<IRequestPreProcessor<TRequest>> _preProcessors;

Expand Down
9 changes: 4 additions & 5 deletions test/MediatR.Tests/PipelineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public async Task<Pong> Handle(Ping request, RequestHandlerDelegate<Pong> next,
}
}

public class InnerBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
public class InnerBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : notnull
{
private readonly Logger _output;

Expand All @@ -118,8 +118,7 @@ public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TRe
}
}

public class OuterBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
public class OuterBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : notnull
{
private readonly Logger _output;

Expand All @@ -139,7 +138,7 @@ public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TRe
}

public class ConstrainedBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : Ping, IRequest<TResponse>
where TRequest : Ping
where TResponse : Pong
{
private readonly Logger _output;
Expand Down

0 comments on commit e611b1b

Please sign in to comment.