nkohari / forge

a dependency injector for node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ability to dynamically resolve missing dependencies

Rush opened this issue · comments

Would be to nice to be able to either have a custom action on a missing dependency or simply provide one manually. I would actually seek to have a mixed pre-defined and lazy loading arrangement (plugins). This should be settable on forge's instance.

What would the interface look like, so I understand what you're trying to accomplish? Something like this?

forge = new Forge()
forge.onMissing = (name) ->
  if name is 'someComponent'
    return new ManuallyCreatedComponent()
  else
    return null

This would make the component name more dynamic. It makes me a little uncomfortable, though, because it might also make the system difficult to understand. Could you give me a more concrete example of what you want to implement?

Such interface would work yes. Basically I want to provide a modules directory in my node.js project where each module returns its constructor function. Different modules are needed depending on a text config file. The problem is that loading all the modules increases application start time and hence I would like to lazy call require('modules/' + name) whenever some component wants to use a particular module. Of course I could get the names only and provide a wrapper constructor but I would rather have a more dynamic version that I can extend in the future in cases where modules are provided from unknown location. I would like to provide this dynamic capability only for forge that has been cloned as per other feature request.