Skip to content

Instantly share code, notes, and snippets.

@jmreidy
Forked from domenic/1-Domenic.js
Created June 12, 2013 16:18
Show Gist options
  • Save jmreidy/5766832 to your computer and use it in GitHub Desktop.
Save jmreidy/5766832 to your computer and use it in GitHub Desktop.
"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;
"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;
"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;
"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