Dependency Injection
config.coffee
inject = require 'pminject' injectbind templaterenderer: require 'nun'injectbind contenttypes: require './contenttypes/text' require './contenttypes/image'
cms.coffee
inject = require 'pminject' contenttypes = injectmany 'contenttypes'templaterenderer = injectone 'templaterenderer' for contenttype in contenttypes templaterendererrender contenttype
What is the problem?
I'd like to build systems that can easily be extended, enhanced, or replaced.
How injectinto solves this problem
- Extension points are defined through strings, for example 'templateengine'
- In configuration these extension points are bound to actual objects
- A module asks for an object via it's string, for example 'templateengine'
- Some modules can expect an array of objects
Goals
- Simple
- Work with async
- Allow multiple registrations