… or how to leverage AMD’s require/define semantics for fine-grained dependency injection.
update: code now available on gist.github.com: https://gist.github.com/4089076
update2: better implementation of calling the service constructor in resolveInstance() to follow operator new semantics.
i’ve also become a big fan of Ninject for its lightweight but very effective dependency injection library for .Net.
this inspired me to implement a dependency injection container using define()/require()-like semantics, including support for circular dependency resolution.
you can download the source files for the ServiceKernel class described in this post, including unit tests, from here: https://gist.github.com/4089076
note: the source code provided has a few dependencies on ECMAScript 5 features, such as Array.isArray, Array.prototype.indexOf, and Function.prototype.bind. You can easily find replacements for these functions should you need to target an older platform.