R
Robert Dober
I am by no means qualified to judge DIin general and I have to admitRobert Dober ha scritto:
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 alternat= ive
DRY solutions to DI that guarantees loose coupling between objects?
Andrea
that my wording might have been indeed to offensive. Scusa per questo.
It is however intriguing to me when dependency injection is a good
solution. I am sure that there is plenty space for its application,
maybe even for logging in a context more specific than the one given
by OP. I believe that the main concern would be the loose coupling of
objects, IIUC you would need a Container method for each class *and*
each class would need to have an adapted initialize method, well that
just really seems too much work to me.
Maybe I shall answer Robert's question at the same time
41. A logging mixin
module MyLogger
logger =3D Logger::new $stderr
define_method :log do logger end
end
now all you have to do is to do include MyLogger in all your classes
and it is sufficient to change the MyLogger module once only, that of
course has not yet any advantage over using a global logger variable
or constant.
But you might have much more flexibility by generalizing the approach
above as follows
module MyLogger
logger1 =3D ...
logger2 =3D ...
class << self; self end.module_eval do
define_meyhod :included do |into_module|
logger =3D
case into_module
when Type1Module
logger1
when Type2Module
logger2
else
Logger::new $stderr
end
into_module.module_eval do
define_method :log do logger end
end*
That would give you pretty easy central control of logger capabilities
on one single point of your project in a more transparent way.
Actually DI makes him carry quite a have load, does it not?
Cheers
Robert
--=20
http://ruby-smalltalk.blogspot.com/
---
Les m=EAmes questions qu'on se pose
On part vers o=F9 et vers qui
Et comme indice pas grand-chose
Des roses et des orties.
-
Francis Cabrel