Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Last active September 22, 2021 21:07
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 dcomartin/8e618e2241b7d9a357167de202bc200a to your computer and use it in GitHub Desktop.
Save dcomartin/8e618e2241b7d9a357167de202bc200a to your computer and use it in GitHub Desktop.
using System;
using Paramore.Brighter;
namespace PipesAndFilters
{
public class PlaceOrderCommand : IRequest
{
public PlaceOrderCommand(Guid id)
{
Id = id;
}
public Guid Id { get; set; }
}
public class PlaceOrderHandler : RequestHandler<PlaceOrderCommand>
{
[Logging(1)]
[Filter(2, typeof(RetryHandler<>))]
[Filter(3, typeof(ValidationHandler<>))]
public override PlaceOrderCommand Handle(PlaceOrderCommand command)
{
Console.WriteLine($"Placing Order: {command.Id}");
return base.Handle(command);
}
}
public class LoggingAttribute : RequestHandlerAttribute
{
public LoggingAttribute(int step) : base(step) { }
public override object[] InitializerParams()
{
return new object[] { Timing };
}
public override Type GetHandlerType()
{
return typeof(LoggingHandler<>);
}
}
public class LoggingHandler<TRequest>
: RequestHandler<TRequest> where TRequest : class, IRequest
{
public override TRequest Handle(TRequest command)
{
Console.WriteLine($"Generic Logging {typeof(TRequest)}");
return base.Handle(command);
}
}
public class RetryHandler<TRequest>
: RequestHandler<TRequest> where TRequest : PlaceOrderCommand
{
public override TRequest Handle(TRequest command)
{
Console.WriteLine("Retry Handler Executing");
try
{
return base.Handle(command);
}
catch (InvalidOperationException)
{
return base.Handle(command);
}
}
}
public class ValidationHandler<TRequest>
: RequestHandler<TRequest> where TRequest : PlaceOrderCommand
{
public override TRequest Handle(TRequest command)
{
Console.WriteLine($"Validating {nameof(PlaceOrderCommand)}");
if (command.Id == Guid.Empty)
{
throw new InvalidOperationException("Invalid Order Id.");
}
return base.Handle(command);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment