Dependency injection and modules?
The module system was designed to make it as easy as possible to use, both for general ease of use and to encourage modular programming. Once you move to an approach where programmers have to write their own linking specifications, it tends to get much more complicated. When you make modules more complicated to use, people don't use them.
Module loaders do give you the ability to do dependency injection, though, by changing the semantics of module URL's.
I'm not sure how this would be done best or how useful it would be, but: has dependency injection been considered as a use case for modules (e.g. to swap modules during testing)?
One possibility might be to give modules parameters that module clients have to provide.
The module system was designed to make it as easy as possible to use, both for general ease of use and to encourage modular programming. Once you move to an approach where programmers have to write their own linking specifications, it tends to get much more complicated. When you make modules more complicated to use, people don't use them.
Yes, I’m all for simplicity.
Module loaders do give you the ability to do dependency injection, though, by changing the semantics of module URL's.
Right. This should give one everything one needs.
Could you, or someone else, please explain what changing the semantics of module URLs means? DI is an important issue and I'd like to understand how it is intended to be achieved in the future module system.
My understanding is limited, but here it goes:
You load modules in ES.next as follows:
module JSON = require('json.org/modules/json2.js');
A custom module loader (loosely related to a Java class loader) can be used to intercept the module loading and return a different module. That is, you would configure a custom loader thusly:
INSTEAD OF 'json.org/modules/json2.js' LOAD 'testmodules/json2.js'
Corrections? Comments?
Greetings,
Axel
Yes, that's the idea.
Best,
I'm not sure how this would be done best or how useful it would be, but: has dependency injection been considered as a use case for modules (e.g. to swap modules during testing)?
One possibility might be to give modules parameters that module clients have to provide.
Axel