Functional Composition with Dependency Injection (IN/Clojure 2016)
Premise for first-class initialization stage in apps and a notion of dependency-passing approach via dependency injection for app initialization while adhering to the time tested Higher-order Functions philosophy.
of “Clojure High Performance Programming” • Open Source contributor: https://github.com/ kumarshantanu • Using Clojure since early 2009 • @kumarshantanu on Twitter
underlying layer’s implementation detail (cache, connection pool)? • What exactly is the “contract” between the caller and the underlying layer? Is the contract explicit? • How do you test the service layer for failures? Can failures be simulated? Can tests be run in parallel? • How easy is it to instrument or wrap an underlying layer implementation? (resilience, asynchrony etc.)
from definition and state • Runtime split into initialization and run • Opportunity for composition, even performance optimization • Dependency Injection is well suited for initialization phase
make partials) • Visually distinguish dependencies vs runtime args • Pet (fns making partials) vs Cattle (using library) • Focus on business logic, not on incidental code
nil) (def de-init nil) • Update #’injected with all injected objects • Update #’de-init with fn to de-initialize the injected • ((get injected :expose-key) runtime-args…) • Reload support via clojure/tools.namespace
injection of dependencies • Declarative, hence visualization is possible • Easy to simulate dependencies for testing • Amenable to parallel testing • Can be invoked late, at initialization time
Emacs M-. may not work (true for any HoF) • Exposed names should be namespaced for clarity • Automated cascading injection may be hard to grasp • CLJS support (though CLJS has reified vars/ns now) • Lack of tooling for navigating exposed names