Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Last active Sep 22, 2021
Embed
What would you like to do?
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