Michael Ekstrand

Dependency Injection

Published on 18 Feb 2016 and tagged with research, programming, and lenskit.

This week, Michael Ludwig and I had a paper published that we've been working on for quite some time. ‘Dependency Injection with Static Analysis and Context-Aware Policy’, describing our Grapht dependency injector, has been published in the Journal of Object Technology.

We wrote Grapht to support algorithm configuration in LensKit, our open-source recommender systems toolkit. We didn't want to write a new dependency injection framework; initially, we built LensKit on top of Guice, and then migrated to PicoContainer when Guice wasn't a good fit for our needs. But PicoContainer also made it difficult to implement the kind of configuration support we wanted to provide, so we finally broke down and wrote our own.

Grapht provides two major advantages over existing injectors:

  • It pre-computes component graphs before instantiating any objects. These graphs can then be analyzed and manipulated to implement whatever diagnostics, visualization, or configuration transformation an application desires. In LensKit, we use this to generate graphs of recommender configurations and to automatically separate pre-computable model components from those that must be instantiated at runtime.

  • It supports context-sensitive policy, making it far easier to configure object graphs that include arbitrary compositions and re-use of components. This is important in LensKit, because it allows us to create simple components and remix them in arbitrary ways to implement the full recommender.

As we were working on the design of Grapht on the GroupLens whiteboard, we frequently found ourselves describing its behavior in terms of graph operations. This lead us to the core of our paper: a mathematical formalization of the problem of dependency injection, and graph-based algorithms for constructing solutions to dependency injection problems. Using this formalization, we prove that our context-sensitive policy captures all configurations that are expressible using qualifiers in tools such as Google Guice, making it strictly more expressive.

The paper has all of the details, of course.

This is a bit of an odd paper for us. Besides being in a field that we generally don't publish in, it is a (successful!) case of turning a yak shave into published research output. I am quite pleased with that outcome.