Dependency injection and modules?

# Axel Rauschmayer (14 years ago)

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

# David Herman (14 years ago)

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.

# Axel Rauschmayer (14 years ago)

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.

# Nathan Stott (14 years ago)

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.

# Axel Rauschmayer (14 years ago)

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

# David Herman (14 years ago)

Yes, that's the idea.

Best,