Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ocaml, Functors : dependency injection

In Real World Ocaml Chapter 9 which is about functors :

Dependency injection

Makes the implementations of some components of a system swappable. This is particularly useful when you want to mock up parts of your system for testing and simulation purposes.

But I fail to grasp the idea. I also looked at Wikipedia about DI - but I actually do not catch the relaton with testing and simulation purposes.

like image 487
Pierre G. Avatar asked Mar 14 '23 11:03

Pierre G.


1 Answers

Dependency injection is a software engineering technique, whose purpose is to reduce the interdependencies between two subsystems of a program. A very important detail of this technique, is that it involves not two, but three subsystems:

  • a service,
  • a client using a
  • an injector, whose responsibility is to prepare a service for a client.

The latter subsystem, with its responsibility, an often overlooked but crucial detail: this implies that the client knows as little about the service as its public interface, which means that one can easily use a mocked service to test the client.

Assume that we write an application communicating with a key-value store over the network. The key-value store has the following signature:

module type AbstractKeyValueStoreService =
sig
   exception NetworkError
   type t
   val list : t -> string
   val find : t -> string -> string option
   val set : t -> string -> string -> unit
end

If we write our client code as a client parametrise by a module of type AbstractKeyValueStoreService we can test the resilience of our application to network errors when using the set function by just providing a mocked service, without needing to actually create the network error:

module KeyValueStoreServiceFailingOnSet =
struct
  exception NetworkError
  type t = unit
  let list () = [ "a"; "b"]
  let find = function
    | "a" -> Some("x")
    | "b" -> Some("y")
    | _ -> None
  let set _ _ = raise NetworkError
end

If our client is written as functor parametrised by a module of type AbstractKeyValueStoreService it is easy to write tests for this software component, where a mocking service follow a more-or-less complex interaction script with the client.

Using modules as parameters might not be an “earth shaking idea” it is nevertheless important to be aware of how this idea can be used to solve important software engineering problems. This is what the authors of “real world OCaml” seem to do.

like image 76
Michaël Le Barbier Avatar answered Mar 20 '23 03:03

Michaël Le Barbier