Conversation
|
The idea is you can do this the following way: In app/myliteral.js: To create the literal: To inject the literal: |
|
This is all a bit in flux at the moment, so let me know if you had some other idea. |
|
Hmmm. Let me show you a bit of the use-case I am wrestling with for context? What I've got is an asynchronous config loader that returns a configuration object to the app, and then several of the core dependencies are built from that configuration. Here's a (very simplified) 0.0.6-style example: configLoader
.load(path)
.then(function(config) {
var IoC = require('electrolyte');
IoC.singleton('log4js', require('log4js-config')(config.logging));
IoC.singleton('mongoose', require('mongoose-config')(config.db.host, config.db.port, config.db.database));
IoC.loader(IoC.node('lib'));
var app = IoC.create('app');
var server = http.createServer(app);
...
})So in a circumstance like this, I don't really know any way to build a source that synchronously returns the literal object, because there's no way to synchronously require the configuration into the source. That kinda boxed me in to having to manually create the objects directly, and then inject them into the container explicitly (which is where the old Honestly, it kinda reminds me of many a discussion I've had in Spring about when it is appropriate to annotate a class as a Or... I could just be missing something and overcomplicating the heck of this. It wouldn't be the first time... |
|
Cool. So, this is still somewhat experimental, but here's how I'm intending to handle this. See this file what I'm doing in my applications: https://github.com/bixbyjs/bixby-sd-etcd/blob/master/lib/registry.js Another example: The idea is that there is some settings external to the application (in a file, or whatever), which become a object which can be injected. Other components can then use those settings to return configured instances to the application. Does something like that look reasonable for your use case? |
|
So... I've chewed on this for a day trying to figure out what I'm missing, but I'm just not seeing it. Feeling dense! The model you describe doesn't seem to address the challenge - how do I make a settings object available in the container so that it can be injected into modules that need it if the settings are built asynchronously? In the case of The only way I can think to do this without manually injecting a literal bean would be something much more complicated like this: settings.js(function() {
var settings = {};
var exports = module.exports = function() {
this.init = function(newSettings) {
settings = newSettings;
};
this.getSettings = function() {
return settings;
};
}
exports['@singleton'] = true;
}).call(this);www.jsconfigLoader
.load(path)
.then(function(config) {
var settingsRepo = require('./lib/settings')();
settingsRepo.init(config);
var IoC = require('electrolyte');
IoC.loader(IoC.node('lib'));
var app = IoC.create('app');
var server = http.createServer(app);
...
})And then I could Am I just misunderstanding? |
Hi there. I ran into issues converting from 0.0.6 to 0.1.0 when dealing with literal components. In 0.6.0, I was able to use
IoC.singleton(key, value)to inject dependencies, but with 0.1.0 this functionality went away. I noticed you had an existing test that was broken for doing singleton injection, so I thought I'd take a swing at it.Basically I've followed your "loaders" convention to create a new
IoC.literal(key, value)loader usable with theuse()method. So similar to theIoC.use(IoC.node(somePath))syntax, you would useIoC.use(IoC.literal(id, component))to inject a literal component.Not sure if that's the way you had in mind for solving this issue - and if there's a different approach you'd prefer let me know and I'll work that up and submit it instead. Not a big coding effort - so reworks are cheap ; )