O'Reilly logo

Testing with F# by Mikael Lundin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Dependency injection

What we're used to from object-oriented systems is that dependencies are injected into the classes where they're used. There are a couple of different kinds of injections, namely:

  • Constructor injection
  • Property injection
  • Method injection

Constructor injection is by far the most common injection type in object-oriented programming. This is, of course, in order to make the necessary dependencies mandatory and not have to check if they're set or not.

Here is what constructor injection would look like in F#:

type CustomerRepository (csvReader : ICsvReader) =
    member this.Load filePath = 
        csvReader.Load filePath 
            |> Seq.map schema
            |> Seq.toList

The dependency, which here is the ICsvReader, is received in the constructor of the CustomerRepository ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required