Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [Q] dependency injection with Needle gem

Reply
Thread Tools

[Q] dependency injection with Needle gem

 
 
Chuck Remes
Guest
Posts: n/a
 
      04-09-2008
I am puzzling through a few things with needle and dependency
injection. I only know of one library that uses it which is net-ssh (v
1.1.2) since both were written by Jamis Buck.

Anyone know of another publicly available gem (or two) that uses
needle for dependency injection? The more examples I have the easier
it will be for me to figure out my development issues.

Thanks...

cr

 
Reply With Quote
 
 
 
 
Daniel Berger
Guest
Posts: n/a
 
      04-09-2008


On Apr 9, 12:39 pm, Chuck Remes <(E-Mail Removed)> wrote:
> I am puzzling through a few things with needle and dependency
> injection. I only know of one library that uses it which is net-ssh (v
> 1.1.2) since both were written by Jamis Buck.
>
> Anyone know of another publicly available gem (or two) that uses
> needle for dependency injection? The more examples I have the easier
> it will be for me to figure out my development issues.


DI has no place in Ruby. So says Jamis Buck himself:

http://weblog.jamisbuck.org/2007/7/29/net-ssh-revisited

"I've since learned my lesson and have come to understand that
although DI is a nifty technique that works well in some environments,
it is wholly unnecessary in Ruby. Ruby sports an agility that Java
will never know, and can get by without such crutches."

Regards,

Dan

 
Reply With Quote
 
 
 
 
Shawn Anderson
Guest
Posts: n/a
 
      04-09-2008
[Note: parts of this message were removed to make it a legal post.]

Chuck,
I originally started out using needle for my DI for a game I'm writing in
ruby. However, someone showed me DIY that I like a little better
Your object dependencies are mapped out in a yaml file. It might be
something worth checking out:
http://rubyforge.org/projects/atomicobjectrb/

/Shawn

On Wed, Apr 9, 2008 at 11:39 AM, Chuck Remes <(E-Mail Removed)> wrote:

> I am puzzling through a few things with needle and dependency injection. I
> only know of one library that uses it which is net-ssh (v 1.1.2) since both
> were written by Jamis Buck.
>
> Anyone know of another publicly available gem (or two) that uses needle
> for dependency injection? The more examples I have the easier it will be for
> me to figure out my development issues.
>
> Thanks...
>
> cr
>
>


 
Reply With Quote
 
Avdi Grimm
Guest
Posts: n/a
 
      04-09-2008
On Wed, Apr 9, 2008 at 4:44 PM, Daniel Berger <(E-Mail Removed)> wrote:
> DI has no place in Ruby. So says Jamis Buck himself:
>
> http://weblog.jamisbuck.org/2007/7/29/net-ssh-revisited
>
> "I've since learned my lesson and have come to understand that
> although DI is a nifty technique that works well in some environments,
> it is wholly unnecessary in Ruby. Ruby sports an agility that Java
> will never know, and can get by without such crutches."


There's a difference between dependency injection the pattern, and
dependency-injection tools/frameworks.

It's still a good idea to write classes that have their collaborators
passed in, rather than finding or creating them internally.

One of my favorite examples:

# 1. Not so great
class Whatsit
def initialize
@timestamp = Time.now
end
end

# 2. better
class Whatsit
def initialize(clock=Time)
@timestamp = clock.now
end
end

#2 is better because it's decoupled from the nondeterministic system
clock, and thus easier to test.

Sure, with ruby Mock Object libraries you can do things like:

Time.stub!(:now).and_return("TIMESTAMP")

...but this runs the risk of breaking unrelated code that also happens
to be called in the test. And no, that's not a theoretical risk;
I've had real tests fail because I needed to deterministically test
time-based code and some other code (like, say, RSpec itself) depended
on Time.now.

DI frameworks may be overkill in Ruby, but DI is still a good way of
breaking up your dependencies and keeping things decoupled.

--
Avdi

Home: http://avdi.org
Developer Blog: http://avdi.org/devblog/
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Gem dependency / installing multiple gem from a single file Nasir Khan Ruby 0 06-08-2009 09:31 PM
Is it in the C++ culture to use Dependency Injection/Inversion ofControl? Danno C++ 12 04-10-2008 08:08 PM
What is Dependency Injection? Anonieko ASP .Net 2 10-14-2007 12:03 PM
Dependency Injection in C++ Michael Feathers C++ 5 10-13-2006 08:23 AM
How to build a dependency injection framework by myself? =?iso-8859-1?B?bW9vcJk=?= Java 4 08-24-2006 06:57 PM



Advertisments