-
-
Save dcomartin/8e618e2241b7d9a357167de202bc200a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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