Enrique Comba Riepenhausen
6/8/2007 9:46:00 PM
On 8 Jun 2007, at 21:05, Mark Volkmann wrote:
> On Jun 8, 2007, at 1:55 PM, sweetchuck74@gmail.com wrote:
>
>> Nowadays, DI (Dependency Inversion) is one of the widely used design
>> pattern.
>> It provides several benefits, such as (1) loose coupling between
>> component (2) effective and easy testing (component testing or unit
>> testing)
>>
>> What are the drawbacks or disadvantages of using DI?
>
> I'm not anti-DI, but I think one disadvantage is that it can be
> hard to follow the code. You see a method in a class, you know
> something is calling it to inject a value, but you're not sure
> where it happens or what value is injected.
Actually Dependency Injection comes from less dynamic languages where
you have the notion of interfaces, etc.
In Ruby though this does not exist at such a level (like Smalltalk an
the like), but it is still advantageous to use in most cases.
Dependency Injection follows the Hollywood principle: "You won't call
us, we will call you".
Let's suppose you have a method that calls a method from a command
(it doesn't matter much what this command does; and sure, the example
is pretty silly). You could create an instance of the particular
command in the method like:
class CommandCaller
def initialize
@command = MySuperCommand.new
end
def call_command()
@command.execute
end
end
or, using a DI approach you would pass the fully initialized command
itself to the method:
class CommandCaller
def initialize(command)
@command = command
end
def call_command(command)
command.execute
end
end
Obviously one of the strengths of DI (or IoC; Inversion of Control)
is that you normally wire those objects dynamically on startup of
your application...
I hope that helped a bit.
Cheers,
----
Enrique Comba Riepenhausen
ecomba@mac.com
I always thought Smalltalk would beat Java, I just didn't know it
would be called 'Ruby' when it did.
-- Kent Beck