Higher Order Dependency Injection
HODI is a set of functions designed to be simple and consistent with a minimal implementation.
The purpose of this library is to make unit testing of functions used as HTTP handlers in F# web applications more straightforward.
PM> Install-Package HODI
or...
dotnet add package HODI
Instead of...
let oldWayHandler : HttpHandler =
fun (next: HttpFunc) (ctx: HttpContext) ->
task {
let dep = ctx.GetService<Dependency>()
// ...
}
this can be done...
let anotherWayHandler : HttpHandler =
fun (dep: Dependency) (next: HttpFunc) (ctx: HttpContext) ->
task {
// ...
}
The functions can be used when defining routes.
let app =
choose [
route "/a" >=> exampleHandler |> inject
route "/b" >=> handlerWith2Dependencies |> inject2
routef "/c/%s" >=> handlerWithArgumentAndDependency |> injectPlus
]
When unit testing the HTTPHandler functions, you could simply pass the dependencies without having to set up a test server. In other words, the tests can focus on the handlers behavior instead of caring about dependency injection.