At last I can show concrete example proving that IoC may be useful, even in
dynamic languages.
I always liked the simplicity of IoC concept - "You don't have to know
anything about environment, you'll be called by someone when needed"
But all IoC implementations I saw did exactly the opposite - they clutter
the code with even more things than without it. So, I created my own IoC
that works as I'd like it to be - it stays hidden and invisible 90% of time.
It works like this - register component once in config:
app.register('db', function(){
return require('mongodb').connect(config.dbPath)
})
And use it anywhere in application
app.db.findSomething()
You can see the full component definition code (with DB Connection and
other stuff) here
https://github.com/sinizinairina/mono/blob/master/mono.coffee
This is the only place when you have to tell IoC what to do, after that all
those components will be created and wired automatically and you don't have
to see IoC specific code in your application anymore.
P.S.
It may seems stupid - use IoC when you can easily wrote something like
app.db = require('mongodb').connect(config.dbPath)
But actually it's not - one thing - in code from above your application
startup will take more time because it will wait until connection to
db has been created. With IoC it will be lazy initialisation - app started
instantly, and db initialised only when needed.
Another thing, it start to pay more when there are different configuration
environments like (dev, prod, test) and when code getting bigger and also
dynamic components (that can be created and destroyed dynamically according
to some rules) added to play, take look at this
app.register('controller', {scope: 'request'}, function(){new
Controller()})
A new controller will be initialised for every request scope and destroyed
when request will be finished. You can also add multiple components to this
scope and all of them will be automatically created and destroyed when
scope started and ended.
On Friday, 27 July 2012 18:45:48 UTC+4, Eldar wrote:
>
> Do we need this in Node?
>
> My answer is yes we need some (simple) way to specify the app level
> dependencies at runtime. Here is my take on
> this<https://github.com/eldargab/node-runtime>.
> Please checkout and let me know how do you feel about.
>
> But the idea is very simple:
>
> // inside any index.jsvar R = require('runtime').patchNative()var use =
> R(module).use
> use('fs', 'node_modules/third/party', require('./smart-fs'))
>
> That's it. Third party module just uses our smart file system
>
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.