I'm not exactly sure what makes me uncomfortable about it. I think
that it is the lack of symmetry; if you want 0 parameters you define 0
parameters, if you want n you define n + 2.
Well, actually, if you want 0 parameters, you can define 2 parameters,
so the symmetry is still there. It is just that you can optionally
leave the parameters out of the block if you don't need to use them.
But I understand what you are saying, and there is something to be said
about that. However, I still feel like adding an option to specify that
the c/p parameters are optional complicates the API. I'll do some
thinking about it.
2) The dynamic nature of Ruby means it isn't as hard to 'roll your
own' DI solution. If Needle got some more publicity I reckon people
would use it more.
I sure did my best to push Copland about a year ago, and Needle in the
few months after RubyConf '04. But as you said, most Ruby developers
are working on smaller projects, and so it was difficult to demonstrate
the value of DI in those situations. I know a few developers were/are
using Needle in their software, though--Christian Neukirchen is using
it in his Nukumi2 blog software, and the RPA team was using it
(although RPA seems to have become defunct). I used Needle in Net::SSH.
It would help if DI wasn't so hard to explain.
Jim Weirich did an
excellent job in his dependency injection article of several months
ago, but I think the whole concept still leaves a lot of people
scratching (and shaking) their heads.
Something I would like to see would be integration of Needle into
Rails. I seem to recall quite a few threads on the Rails list along
the lines of 'How do I make something available to all of my
controllers'? The answer normally involves inheritance of
environment.rb. If ActionController::Base had a registry associated
with it or even if there was a (heaven forfend) global rails registry
it would provide a single definitive place to put these kind of
services. Rails itself could provide ActionMail, Logging or other
services via this registry as well.
David and I actually discussed this possibility quite seriously a while
ago, but for several reasons Needle is not a very good fit inside Rails
itself. I know several people are using it on top of Rails, which I
think is great, but adding Needle support to Rails itself is not
necessarily adding value. A few tweaks to your environment.rb and you
can add the Needle support yourself (thanks to Ruby's ability to reopen
classes). Or, you can do like I did
(
http://ruby.jamisbuck.org/rails-injected.html) and work out a more
object-oriented solution, which is what I originally proposed to David.
(That link, incidentally, builds on a pre-0.9 version of Rails, so you
may need to tweak it considerably to get it to work in modern releases.
Also, the document describes how Rails now solves the problems I was
addressing).
IMHO dependency injection is a good thing and Needle is an excellent
example of a DI framework. With lots of non OO developers coming in to
Ruby via Rails it would be an excellent opportunity to spread the word
about a valuable and powerful technique. I know that Rails values
convention over configuration ... but sometimes configuration is
necessary. When is it Needle provides a solid, flexible solution.
Thank-you, Rob. I'm glad you have found Needle useful. What I think
would really help the understanding of DI in Ruby is if you could maybe
write a brief article about how, specifically, you are using DI in your
projects. I'd be happy to host that article on my blog if you'd like,
or perhaps even Chad could host it on RubyGarden.
- Jamis