Skip to content

Repository files navigation

HODI

Logo of HODI

Higher Order Dependency Injection

HODI is a set of functions designed to be simple and consistent with a minimal implementation.

NuGet Downloads Coverage Status

Why HODI?

  • Testable by default: pass dependencies as arguments, not service locators
  • Type-safe: compiler catches missing deps before runtime
  • Works with Giraffe/web handlers

Install

PM> Install-Package HODI

or...

dotnet add package HODI

Defining Handlers

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 {
            // ...
        }

Usage

The functions can be used when defining routes.

let app =
    choose [
        route "/a" >=> exampleHandler |> inject
        route "/b" >=> handlerWith2Dependencies |> inject2
        routef "/c/%s" >=> handlerWithArgumentAndDependency |> injectPlus
    ]

Unit Testing

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.

About

Higher Order Dependency Injection

Topics

Resources

Security policy

Stars

15 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages