Robert Dober ha scritto:
> On Wed, Jun 25, 2008 at 11:22 AM, Robert Klemme
> <shortcutter / googlemail.com> wrote:
>
>> 2008/6/24 Iñáki Baz Castillo <ibc / aliax.net>:
>>
>>> El Martes, 24 de Junio de 2008, Andrea Fazzi escribióº
>>>
>>>> require 'logger'
>>>>
>>>> class Container
>>>> def logger
>>>> @logger ||= Logger.new STDOUT
>>>> end
>>>> def foo
>>>> Foo.new(logger)
>>>> end
>>>> end
>>>>
>>>> class Foo
>>>> def initialize(logger)
>>>> @logger = logger
>>>> end
>>>> def bar
>>>> @logger.info('Foo#bar invoked.')
>>>> end
>>>> end
>>>>
>>>> c = Container.new
>>>> c.foo.bar
>>>>
>>> That's a cool solution :)
>>>
>> I do not think so. Reasons: it clutters every instance with a
>> reference which can have a significant impact on memory if there are a
>> lot objects. Then, you have to change a class's #initialize signature
>> for all classes that do want to do logging and also manually code the
>> assignment inside the class. Accessing a logger from a global context
>> (whichever way you do it) saves memory and is less tedious.
>>
>> Kind regards
>>
>> robert
>>
>>
>>
>> --
>> use.inject do |as, often| as.you_can - without end
>>
>>
>>
>
> Could not agree more with you. May I add some other reasons.
> This approach vioaltes principles we regard rather highly on this list
> It is not DRY, you are really repeating yourself and in case of the
> metaprogramming solutions you let Ruby repeat itself.
> It is just much less simple than necessary.
> You spread code dependencies all over the place, as a matter of fact
> the expression "dependency injection" says it allready it is almost as
> putting a virus (with constructive behavior) into your code, but can
> you imagine how much more work refactoring will become?
>
> My order of preference would be
> 1. $logger or $LOGGER
> 2. Logger
>
> 43. Kernel::log (or Object.log)
>
> Cheers
>
>
>
>
>
Hi Robert,
I would known if, in your opinion, DI is not DRY in general or you are
referring to the particular case of logging. Moreover, which are
alternative DRY solutions to DI that guarantees loose coupling between
objects?
Andrea