This post originated from an RSS feed registered with Java Buzz
by dion.
Original Post: Transparent Inversion of Control
Feed Title: techno.blog(Dion)
Feed URL: http://feeds.feedburner.com/dion
Feed Description: blogging about life the universe and everything tech
Dave Thomas wrote a thoughtful entry on IoC and Ruby.
He takes a look at one of the IoC implementations available for Ruby, Needle.
One of the problems that Dave has with IoC is the "magic":
One of the problems I have with the current IOC implementations is the "then a miracle happens" stuff. Youre looking at the source to a component, and do dont really know how it is set up. In the example above, you assume that somehow the cust_model attribute is set to a class that implements a customer model, but it isnt obvious. The same is true in Spring and friendsa whole lot of stuff happens behind the scenes, and some of that stuff is important.
So he reworked his example to be more explicit and:
attr_accessor :cust_model
becomes:
model :customer
as well as a few other things.
This does beg an interesting question. Is injecting dependencies with contructor/setter/getter all we want out of IoC? Or do we want to be more explicit?
Is having an @Inject annotation explicit enough?
I am personally quite happy with the level of "magic" that you get with IoC. When I write a component, I don't want to think about how a Foo is there for me to use. I just use it.
Sometimes I do find that set*() is maybe overloaded, as it can mean "here are properties that will be set via injection. assume they are" or "if you want to set a property go for it". This is where the Constructor-based becomes useful. You *know* those dependencies are set. You can't "miss" one.