Higher Order Dependency Injection
HODI is a set of functions designed to be simple and consistent with a minimal implementation.
- Testable by default: pass dependencies as arguments, not service locators
- Type-safe: compiler catches missing deps before runtime
- Works with Giraffe/web handlers
PM> Install-Package HODIor...
dotnet add package HODIInstead 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.
