This introduction is here in case you'd like to treat this as a programming excercise yourself. If doing so, feel free to implement it in any language you'd like.
Many structures (HttpClient, Delegating handlers in MVC/WebAPI) in C# use a handler pattern. Which often have helper methods make it easy to use them but actually building one from scratch is not often done.
And those ones I've seen are always concrete types as input/outputs. So I'd like to make a generic one, so that those helper mehods do not need to be rewritten for each concrete input/output type that we need.
- Requirement 1: Have a method that when passed
IEnumerable<GenericPipelineHandler<TIn, TOut>>
will result in an object that has a method similar toTOut Process(TIn input);
, and if that method is called each handler is called in order. Each handler should be able to work with and modify both the input object and the output object before it is passed inward or outward respectively.- It can be up to you on if it needs further parameters, below
HttpClientFactory
has a different object for theinnerHandler
than the rest of the handlers, so perhaps that is a good design or even required? - You can also define the interface of
GenericPipelineHandler<TIn, TOut>
- It can be up to you on if it needs further parameters, below
- Requirement 2: We do not want to use a loop to access each Handler, instead it should act like DelegatingHandlers in ASP.NET MVC or like Interceptors in OkHttp
We can maybe use HttpClientFactory and related objects as a basis of our interface? Or perhaps OkHttp interceptors?
The actual solution I came up with is below, for the exercise I'd suggest you attempting it first, then you can look at my solution and compare/contrast. I definitely do not know if I'm fully happy with my implementation, I'd love to hear other people's opinions/ideas.