Re: AngularJS inspired dependency injection in Node (feedback?)

1,229 views
Skip to first unread message

Paul Spaulding

unread,
Apr 24, 2013, 10:03:41 AM4/24/13
to nod...@googlegroups.com
Cool.  Could you post a complete example, not just fragments?

On Saturday, April 20, 2013 1:19:27 PM UTC-4, Scott Corgan wrote:
Not sure what kind of backlash I'll get for doing things slightly different than the "Node" way, but I wrote what I believe is the easiest and lightest solution I've seen for dependency injection for Node.


The idea is that you add a comment at the top of the file "// inject", then these files are automatically loaded and ready for injection. I've found it MUCH easier to inject mocks for tests. Also, when using this module, directory structure is only for visual organization for the coder. No need to worry about where the file is located, especially if you move the file to a different directory. Normally I'd just use another module, but I didn't find anything that was just easy. A lot of how the injection works is based off of the AngularJS source code.

Anyways, would love some feedback. Always looking to improve the coding experience for developers.

Scott Corgan

unread,
Apr 25, 2013, 11:09:44 AM4/25/13
to nod...@googlegroups.com
Welcome. Would love to see how you're using and any feedback on how to make it better!

On Tuesday, April 23, 2013 11:05:42 PM UTC-7, Donald Pipowitch wrote:
Hi Scott,

I'm also a big fan of Angulars dependency injection and looked exactly for a solution like yours. Thank you very much for sharing this.

Scott Corgan

unread,
Apr 25, 2013, 11:10:50 AM4/25/13
to nod...@googlegroups.com
Hi Paul,

I've set up a small and quick example on the repo. You can view the example here: http://sctt.cc/Ae7c

Let me know if you need any help with it.

Addison Higham

unread,
Apr 25, 2013, 12:52:36 PM4/25/13
to nod...@googlegroups.com
Not to thread hijack, but we have been using this for a while where I work: github.com/idottv/dependable

It expects a single function to be exported in module.exports. It looks at the names of the parameters and then resolves the dependencies either based on what you have registered manually or in directories you have registered.




--
--
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 nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
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 nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Scott Corgan

unread,
Apr 25, 2013, 12:59:09 PM4/25/13
to nod...@googlegroups.com
Looks like a great module, and works well.

My only issue with it is having to register each module individually. Injector aims to make that easier by registering entire directories. Then, in that directory, you can specify which files are actually injectable modules.

Thanks for the heads up for the awesome module!

Nuno Job

unread,
Apr 25, 2013, 1:28:15 PM4/25/13
to nod...@googlegroups.com
Is this similar to https://github.com/3rd-Eden/canihaz ?

Nuno


--

Scott Corgan

unread,
Apr 25, 2013, 1:36:50 PM4/25/13
to nod...@googlegroups.com
Not entirely. It seems that canihaz seems to solve resolving Node dependencies lazily to reduce code bloat. This would seem to resolve the task of having to install all dependencies when the node_modules folder is ignored by git.

Injector allows you to create local modules and include them in other injectoable modules without any knowledge of which folder/subfolder they reside in (soon to include the node_modules folder). This is based off the way Angular handles dependency injection in controllers/services/directives/etc.

The idea is to create tidy modules that are easily mocked/stubbed for testing while easily useable in a modular Node application.

Nuno Job

unread,
Apr 25, 2013, 1:38:30 PM4/25/13
to nod...@googlegroups.com
Interesting. Would be awesome to read on a blog post and compared to just plain old require. For us non angular guys this is not obvious :)

Nuno

Scott Corgan

unread,
Apr 25, 2013, 1:45:52 PM4/25/13
to nod...@googlegroups.com
Great idea!

I'll right something up and post link in this thread.

Scott Corgan

unread,
Apr 25, 2013, 3:04:19 PM4/25/13
to nod...@googlegroups.com
But together a quick and rough blog post describing the problem I wam solving and the "why" behind Injector: http://sctt.cc/6Mue

Scott Corgan

unread,
Apr 25, 2013, 3:04:59 PM4/25/13
to nod...@googlegroups.com
Put together a quick blog post describing the problem I am solving and the "why" behind Injector: http://sctt.cc/6Mue

Scott Corgan

unread,
Apr 25, 2013, 3:05:32 PM4/25/13
to nod...@googlegroups.com
Ahhh. Sorry for the typos.

Martin Cooper

unread,
Apr 25, 2013, 7:38:05 PM4/25/13
to nod...@googlegroups.com
The piece I'm still missing is, how do I inject modules I didn't write? For instance, how would I inject 'fs' or 'http'? Or perhaps 'request'?

--
Martin Cooper


--

Scott Corgan

unread,
Apr 25, 2013, 7:48:57 PM4/25/13
to nod...@googlegroups.com

You would include them in the arguments like any other module. Just published an update for that feature.


Typos brought to you by my iPhone

autoric

unread,
Apr 26, 2013, 9:27:57 AM4/26/13
to nod...@googlegroups.com
Another similar module - https://github.com/autoric/nject

Obviously there is interest in this topic.

Scott Corgan

unread,
Apr 26, 2013, 1:25:32 PM4/26/13
to nod...@googlegroups.com
Very true! There is the argument that that's what the "node_modules" folder is for, and that may be true. But, there's something just beautiful about being able to control the definition or your dependencies when writing testable code!

Eldar

unread,
Apr 26, 2013, 6:02:57 PM4/26/13
to nod...@googlegroups.com
You might find easy-app also interesting.

Scott Corgan

unread,
Apr 26, 2013, 7:36:44 PM4/26/13
to nod...@googlegroups.com
Looks great!

Scott Corgan

On Friday, April 26, 2013 at 3:02 PM, Eldar wrote:

You might find easy-app also interesting.

--

Scott Corgan

unread,
May 8, 2013, 12:49:35 PM5/8/13
to nod...@googlegroups.com
FYI, 0.4.0 was released a few days ago. Updates include loading NPM and Core modules via injection and support for multiple directions!

Thomas Fritz

unread,
Jul 23, 2013, 5:08:45 PM7/23/13
to nod...@googlegroups.com
Looks very interesting.

I came across Architect https://npmjs.org/package/architect

What do you guys think about that? Have someone used it?

Regards

Scott Corgan

unread,
Jul 23, 2013, 5:12:52 PM7/23/13
to nod...@googlegroups.com
Architect is what inspired me to make Injector. I loved architect because it removed the pain of knowing how many "../../../" to put when requiring a file.

Love it.

Thomas Fritz

unread,
Jul 24, 2013, 4:03:38 AM7/24/13
to nod...@googlegroups.com
What differs injector from Architect and what do you think where Injector is better than Architect or when should on choose one over the other?

Who uses Injector currently? In Production?

Thanks for answering.
Kind regards

Scott Corgan

unread,
Jul 31, 2013, 10:27:58 AM7/31/13
to nod...@googlegroups.com
Architect requires defining each module in a config file. In that file, you are required to know the exact location of the module. The goal with Injector is to create a way that I could move modules around as I organize my app, and I wouldn't need to update my references to it anywhere; it happens automatically.

As far as Injector in production, I use it for various personal projects. I know that Sevenly.org uses extensively for internal applications.

Thanks for the great questions!


--
--
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 nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
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 a topic in the Google Groups "nodejs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nodejs/cRrQEjL7xBE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nodejs+un...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.





--
- Scott
Reply all
Reply to author
Forward
0 new messages