-
-
Save jmreidy/5766832 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
// Domenic needs a Tweeter | |
function Domenic(tweeter) { | |
this.tweeter = tweeter; | |
} | |
Domenic.inject = ["tweeter"]; | |
Domenic.prototype.doSomethingCool = function () { | |
return this.tweeter.tweet("Did something cool!"); | |
}; | |
module.exports = Domenic; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
// Merick also needs a tweeter | |
function Merrick(tweeter) { | |
this.tweeter = tweeter; | |
} | |
Merrick.inject = ["tweeter"]; | |
Meerick.prototype.doSomethingAwesome = function () { | |
return this.tweeter.tweet("Did something awesome!"); | |
}; | |
module.exports = Merrick; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
function App(domenic, merrick) { | |
this.domenic = domenic; | |
this.merrick = merrick; | |
} | |
App.inject = ["domenic", "merrick"]; | |
App.prototype.run = function () { | |
this.domenic.doSomethingCool().done(); | |
this.merrick.doSomethingAwesome().done(); | |
}; | |
module.exports = App; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var diContainer = require("di-container"); | |
// Declaratively wire up dependencies. Note that while Domenic and Merrick both need the "tweeter" | |
// abstraction, we can choose *via configuration* to give them a difference "tweeter" concretion. | |
// They are completely decoupled from this knowledge. | |
diContainer.config({ | |
"app": require("./3-App"), | |
"domenic": { | |
constructor: require("./1-Domenic"), | |
inject: { | |
"tweeter": require("./LolSpeakTweeter-not-shown") | |
} | |
}, | |
"merrick": { | |
constructor: require("./2-Merrick"), | |
inject: { | |
"tweeter": require("./LeetSpeekTweeter-not-shown") | |
} | |
} | |
}); | |
// Construct the entire object graph, using above declarative config. | |
diContainer.get("app").run(); | |
// Should Tweet: | |
// - "LOL I HAZ DID SOMETHING COOL LOL!" | |
// - "1 d1d s0m3th1ng 4w3s0m3!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment