DI-DIY What do you think about it?

233 views
Skip to first unread message

Gianluca Padovani

unread,
Dec 1, 2010, 11:13:11 AM12/1/10
to growing-object-oriented-software
Hi,
in my team there is a discussion about this technique to apply the
DI.The technique is described in these two links:
http://blacksheep.parry.org/archives/diy-di
http://blacksheep.parry.org/wp-content/uploads/2010/03/DIY-DI.pdf

Obviously there are people who are enthusiastic and people that
aren't. The work expressed in the PDF is excellent but something
doesn't sound right, in particular when they talk about the scope, not
the idea of the scope that is good, but how this idea is applied to
code. In general what I don't like is the use of the name and the
explicit use of a class called scope. In example of the GOOS these
concepts are present but are implicit and there isn't a class called
scope that manages the scope of the object or there aren't any classes
called *inject. I would appreciate your opinion about this technique.

Thank you very much
Gianluca

P.S.
The GOOS is a fantastic book as every page is gold. For me it was
fantastic because it was the practical implementation of a lot of
theoretical study that I had made, in particular the role of the mock
object to discover the role between objects, the use of the Acceptance
Test to design the software, the non-use of a framework of DI, etc ...
I can't wait for your next book :-)

Moandji Ezana

unread,
Dec 1, 2010, 3:19:09 PM12/1/10
to growing-object-o...@googlegroups.com

I read through half of it before giving up. I really can't imagine how this tedious, ugly and uncommon framework could be seen as better, or less intrusive, than Guice.

Especially considering that in Guice 3 you can use the standard @Inject or even go annotation-less by binding directly to a constructor.

Moandji

--
www.moandjiezana.com

Sent from my Android

On 1 Dec 2010 18:25, "Gianluca Padovani" <gpad...@gmail.com> wrote:

Nat Pryce

unread,
Dec 1, 2010, 3:29:44 PM12/1/10
to growing-object-o...@googlegroups.com
In GOOS we are extremely explicit about scope! Java is a block
structured language with lexical scoping, closures and objects.
Blocks and objects are scopes. Variables declared in a block and
instance variables declared in an object are in a scope. There is no
need to re-implement (badly) what the language (compiler and VM)
already provide.

--Nat

--
http://www.natpryce.com

Stephen Haberman

unread,
Dec 1, 2010, 3:39:26 PM12/1/10
to growing-object-o...@googlegroups.com

> in my team there is a discussion about this technique to apply the
> DI.The technique is described in these two links:
> http://blacksheep.parry.org/archives/diy-di
> http://blacksheep.parry.org/wp-content/uploads/2010/03/DIY-DI.pdf

If you're looking into framework-less DI, you might also consider this
approach:

http://sonymathew.blogspot.com/2009/11/context-ioc-revisited-i-wrote-about.html

Although I consider both somewhat verbose. DIY-DI's static factory
methods do seem a bit excessive, and Context IoC's per-class interfaces
also seem like more work than necessary.

Personally, I've had success passing scope classes directly to
constructors, e.g.:

public interface ApplicationScope {
DepA getA();
}

public class MyService {
private final DepA a;
public MyService(ApplicationScope s) {
this.a = s.getA();
}
}

It is not as conceptually clean as either DIY-DI or Context IoC, both of
which avoid MyService depending on the outside ApplicationScope
interface. And it also hides the real dependencies (a in this case)
inside of the MyService's cstr, instead of in the cstr argument list.
The trade off is that wiring becomes a lot simpler, because you just
pass ApplicationScope around and let objects use what they need. In
practice, it has worked well for me but YRMV.

Though I'm sure the GOOS authors would take me to task for having
non-domain language terms like "scope" and "service" floating around the
codebase. Fair enough. :-)

- Stephen

Steve Freeman

unread,
Dec 1, 2010, 4:27:23 PM12/1/10
to growing-object-o...@googlegroups.com
I mind the naming less than I mind the bundling up of dependencies and spreading them around the code :) The point of direct constructor dependency is that an object refers to just what it needs and nothing more.

S.

On 1 Dec 2010, at 20:39, Stephen Haberman wrote:
>
> Personally, I've had success passing scope classes directly to
> constructors, e.g.:
>
> public interface ApplicationScope {
> DepA getA();
> }
>
> public class MyService {
> private final DepA a;
> public MyService(ApplicationScope s) {
> this.a = s.getA();
> }
> }
>
> It is not as conceptually clean as either DIY-DI or Context IoC, both of
> which avoid MyService depending on the outside ApplicationScope
> interface. And it also hides the real dependencies (a in this case)
> inside of the MyService's cstr, instead of in the cstr argument list.
> The trade off is that wiring becomes a lot simpler, because you just
> pass ApplicationScope around and let objects use what they need. In
> practice, it has worked well for me but YRMV.
>
> Though I'm sure the GOOS authors would take me to task for having
> non-domain language terms like "scope" and "service" floating around the
> codebase. Fair enough. :-)
>
> - Stephen
>

Steve Freeman

Winner of the Agile Alliance Gordon Pask award 2006
Book: http://www.growing-object-oriented-software.com

+44 (0) 797 179 4105
M3P Limited. http://www.m3p.co.uk
Registered office. 2 Church Street, Burnham, Bucks, SL1 7HZ.
Company registered in England & Wales. Number 03689627

Christopher Gardner

unread,
Dec 1, 2010, 11:13:43 PM12/1/10
to growing-object-o...@googlegroups.com
Moandji,

Do you have an example of going "annotation-less" construction with guice?

J. B. Rainsberger

unread,
Dec 2, 2010, 12:31:52 AM12/2/10
to growing-object-o...@googlegroups.com
On Thu, Dec 2, 2010 at 03:13, Gianluca Padovani <gpad...@gmail.com> wrote:
Hi,
in my team there is a discussion about this technique to apply the
DI.The technique is described in these two links:
http://blacksheep.parry.org/archives/diy-di
http://blacksheep.parry.org/wp-content/uploads/2010/03/DIY-DI.pdf

Woohoo! Another library without the principles!

It's simple: make dependencies explicit by requiring collaborators as parameters in the constructor.  Repeat until you've pushed all decisions about which objects to create into the entry point. Of course, this only applies to Services (in the DDD sense). Done.
-- 
J. B. (Joe) Rainsberger :: http://www.jbrains.ca :: http://blog.thecodewhisperer.com
Diaspar Software Services :: http://www.diasparsoftware.com
Author, JUnit Recipes
2005 Gordon Pask Award for contribution to Agile practice :: Agile 2010: Learn. Practice. Explore.

Moandji Ezana

unread,
Dec 2, 2010, 3:31:02 AM12/2/10
to growing-object-o...@googlegroups.com
On Thu, Dec 2, 2010 at 5:13 AM, Christopher Gardner <chris.r...@gmail.com> wrote:
Do you have an example of going "annotation-less" construction with guice?


Cf. the last paragraph of the overview. I'm not saying it's practical, just that it's possible.

Moandji

Gianluca Padovani

unread,
Dec 3, 2010, 3:49:53 AM12/3/10
to growing-object-oriented-software
2010/12/1 Nat Pryce <nat....@gmail.com>:

> In GOOS we are extremely explicit about scope!  Java is a block
> structured language with lexical scoping, closures and objects.
> Blocks and objects are scopes. Variables declared in a block and
> instance variables declared in an object are in a scope.

Hi Nat,
probably I don't express well what I think, I retry. In this document
there is a suggestion to create class named *Scope, and the
responsability of this type of class is to manage the different life
cycle of other object.
In the example of the GOOS there isn't anything like this and the
scope of the variables is "implicit" in the class, method, block where
the variables are defined. Do I understand correct?

In the Sniper application, for example, when you press the join
button, you create a AuctionSniper through the SniperLauncher, the
scope of the AuctionSniper start when the button is pressed, but the
sniper use object that have application scope (connection for
example). To manage this situation using the DIY-DI technique I should
create something like AuctionScope and pass it to SniperLauncher.

In the example of the pdf (BatchLineReader pag 11) the author want
process a file and for every line they want create a new
TradeProcessor and use the object tradeScopeEntrance to create
TradeProcessor. How you can manage this situation without the use of
the scope object?

Thanks in advance
Gianluca

Gianluca Padovani

unread,
Dec 3, 2010, 3:49:59 AM12/3/10
to growing-object-oriented-software
Hi Joe,
I'm not sure what you intend for "without the principles", I think it
don't express what the software should do in term of domain and
"force" the developer to create a lot of *inject and *scope class
without think about the role of the class in the domain. My fear is
that if we apply this technique for a month we full our code base of
injector and scope class but our code will not became more clear or
more easy to change.

I "feel" that this way is not good but I can't transmit these
sensation to my team, any suggestions? :-)

bye
Gianluca

Nat Pryce

unread,
Dec 3, 2010, 4:08:45 AM12/3/10
to growing-object-o...@googlegroups.com
On 3 Dec 2010, at 08:49, Gianluca Padovani <gpad...@gmail.com> wrote:
> In this document
> there is a suggestion to create class named *Scope, and the
> responsability of this type of class is to manage the different life
> cycle of other object.
> In the example of the GOOS there isn't anything like this and the
> scope of the variables is "implicit" in the class, method, block where
> the variables are defined. Do I understand correct?

That is NOT implicit! Java is a block structured language with lexical
scoping. IT ALREADY IMPLEMENTS SCOPES with well defined semantics. So
we are VERY EXPLICIT in our use of scopes and did not waste time
reimplementing something that is already provided by the language.


> In the example of the pdf (BatchLineReader pag 11) the author want
> process a file and for every line they want create a new
> TradeProcessor and use the object tradeScopeEntrance to create
> TradeProcessor. How you can manage this situation without the use of
> the scope object?

Why not create a new object for every line and pass the line to it to
be processed? You can do that with a callback or "by hand" in a for-
loop, depending whether the processing is push- or pull-driven. The
object defines the instance variables it needs to do its work, so it
already is a scope. If you use an inner class, it can refer to the
variables in the scope in which it is defined (it's a closure). Or you
can pass its constructor references to the objects it needs, and it
can store those references in instance variables.

--Nat
>

Gianluca Padovani

unread,
Dec 3, 2010, 4:24:03 AM12/3/10
to growing-object-oriented-software
2010/12/3 Nat Pryce <nat....@gmail.com>:
> That is NOT implicit! Java is a block structured language with lexical ...

I think I'm using the word "implicit" wrong, sorry. I'm agree with you
that Java already implements the scopes with a well defined semantics
and it's better than a reinvented one. This concept (semantic) is
already presented in Java and we shouldn't reinvent it.

> Why not create a new object for every line and pass the line to it to
> be processed? You can do that with a callback or "by hand" in a for-
> loop, depending whether the processing is push- or pull-driven. The
> object defines the instance variables it needs to do its work, so it

> already is a scope. ...

Thank you for your suggestion

bye
Gianluca

J. B. Rainsberger

unread,
Dec 3, 2010, 7:51:29 AM12/3/10
to growing-object-o...@googlegroups.com
On Fri, Dec 3, 2010 at 19:49, Gianluca Padovani <gpad...@gmail.com> wrote:
 
I'm not sure what you intend for "without the principles", I think it
don't express what the software should do in term of domain and
"force" the developer to create a lot of *inject and *scope class
without think about the role of the class in the domain. My fear is
that if we apply this technique for a month we full our code base of
injector and scope class but our code will not became more clear or
more easy to change.

I mean the one primary principle: when in doubt, let your client give you your collaborators. Push the choice of the details towards the entry point. Abstractions in code; details in data.

If you follow this principle, then not only will you see more opportunities for reuse, but bad dependencies will be more obvious.

I don't know about *inject and *scope classes, because I've done dependency injection for years without knowing what those things mean.
-- 

Jussi Mononen

unread,
Dec 3, 2010, 3:07:43 PM12/3/10
to growing-object-o...@googlegroups.com
> I don't know about *inject and *scope classes, because I've done dependency
> injection for years without knowing what those things mean.

I've always injected stuff through constructors without knowing what
"injection" or "scopes" meant. I mean, it is kind of common sense.

IMO trying to figure out what happens in, for example, a thoroughly
injection dependent Spring FW project is a nightmare.

(disclaimer: I'm not a Spring expert, just haven't seen clean code done with it)

--
"Progress doesn't come from early risers — progress is made by lazy
men looking for easier ways to do things." - Robert. A. Heinlein

Lance Walton

unread,
Dec 3, 2010, 4:22:16 PM12/3/10
to growing-object-o...@googlegroups.com
I am so glad it's not just me.

Lance Walton

unread,
Dec 3, 2010, 4:26:43 PM12/3/10
to growing-object-o...@googlegroups.com

On 3 Dec 2010, at 08:49, Gianluca Padovani wrote:

>
> In the Sniper application, for example, when you press the join
> button, you create a AuctionSniper through the SniperLauncher, the
> scope of the AuctionSniper start when the button is pressed, but the
> sniper use object that have application scope (connection for
> example). To manage this situation using the DIY-DI technique I should
> create something like AuctionScope and pass it to SniperLauncher.

Is there confusion between 'scope' (which is lexical in Java and refers to variables) and 'extent', which refers to the duration of the objects existence?

J. B. Rainsberger

unread,
Dec 4, 2010, 2:17:51 AM12/4/10
to growing-object-o...@googlegroups.com
On Sat, Dec 4, 2010 at 07:07, Jussi Mononen <agile...@gmail.com> wrote:
> I don't know about *inject and *scope classes, because I've done dependency
> injection for years without knowing what those things mean.

I've always injected stuff through constructors without knowing what
"injection" or "scopes" meant. I mean, it is kind of common sense.

IMO trying to figure out what happens in, for example,  a thoroughly
injection dependent Spring FW project is a nightmare.

I have used Spring exclusively in singleton and autowire-constructor mode. No problems there. Anything else looks scary.
-- 

Nat Pryce

unread,
Dec 5, 2010, 11:15:57 AM12/5/10
to growing-object-o...@googlegroups.com
On 2 December 2010 05:31, J. B. Rainsberger <m...@jbrains.ca> wrote:
> It's simple: make dependencies explicit by requiring collaborators as
> parameters in the constructor.  Repeat until you've pushed all decisions
> about which objects to create into the entry point. Of course, this only
> applies to Services (in the DDD sense). Done.

I don't know about services in the DDD sense, but in the general
OO-design case I might amend that advice to say push all decisions
about which objects to create up as far as possible, but no further:
"Composite Simpler than the Sum of its Parts", "Outside-In", etc.

--Nat

--
http://www.natpryce.com

Esko Luontola

unread,
Dec 5, 2010, 3:40:39 PM12/5/10
to Growing Object-Oriented Software

On Dec 3, 11:08 am, Nat Pryce <nat.pr...@gmail.com> wrote:
> On 3 Dec 2010, at 08:49, Gianluca Padovani <gpadov...@gmail.com> wrote:
> >  In this document
> > there is a suggestion to create class named *Scope, and the
> > responsability of this type of class is to manage the different life
> > cycle of other object.
> > In the example of the GOOS there isn't anything like this and the
> > scope of the variables is "implicit" in the class, method, block where
> > the variables are defined. Do I understand correct?
>
> That is NOT implicit! Java is a block structured language with lexical
> scoping. IT ALREADY IMPLEMENTS SCOPES with well defined semantics. So
> we are VERY EXPLICIT in our use of scopes and did not waste time
> reimplementing something that is already provided by the language.


As far as I understand, by Gianluca talks about existence in time,
whereas Nat talks about existence in space.

In Goanluca's and for example Guice's terminology, by scope is meant
the lifetime of an object: http://code.google.com/p/google-guice/wiki/Scopes

A web application typically has request scope, session scope and
application scope (also known as singleton scope). Other common ones
are conversation scope (for example filling a form; conversation
consists of multiple related requests, but is smaller than session
scope), thread scope, transaction scope. In a Java application the
scope, also known as the context, is usually stored in a ThreadLocal,
so that the code does not need to pass the scope via method calls. For
example, in Spring methods annotated with @Transactional are executed
in a transaction scope, which is set up and torn down using AOP
proxies around the method call.

I've implemented a Guice-powered scope once and it was very handy
(that was the moment when I started using Guice in the project; before
needing scopes I did fine with manual DI). It's called "task
scope" (basically the same as request scope and transaction scope) and
the code for executing code (a Runnable instance) inside that scope
can be seen at
https://github.com/orfjackal/dimdwarf/blob/master/dimdwarf-core/src/main/java/net/orfjackal/dimdwarf/tasks/TaskExecutor.java
which uses the classes at
https://github.com/orfjackal/dimdwarf/tree/master/dimdwarf-core/src/main/java/net/orfjackal/dimdwarf/context
to implement a thread-bound scope. Each task needs its own instances
of some classes, in addition to which there are some application
scoped singleton classes of which only one instance is created.
Configuring the scopes with Guice was simple - just annotate the class
with a scope annotation (@Singleton, @TaskScoped, or no annotation if
a new instance should be created for every user of the class), and the
DI container will take care of the rest.

Nat Pryce

unread,
Dec 5, 2010, 6:57:10 PM12/5/10
to growing-object-o...@googlegroups.com
The point of OO design is to make time and space the same thing. The
object structure (space) represents how the entire system of objects
behaves (time). The structure is explicit, because every object talks
to other objects only through local references and so the structure
must be defined *outside* the objects. Therefore the behaviour of the
system is easily understood, because it is described by the explicit
structure.

I have to admit that I just never find the need to explicitly
represent scopes/lifetimes the way you describe or squirrel them away
in thread-local variables or AOP gubbins. I create an object that
needs to do something, giving it references to the objects that it
needs to do its job. I pass its reference to the sources of events
that will trigger its behaviour. When an event source will no longer
feed it events, the source "forgets" the reference. Eventually there
will be no references to the object and it gets garbage collected.
That, in turn, may cause the collection of the objects that it depends
on, and so on...

--Nat

--
http://www.natpryce.com

Steve Freeman

unread,
Dec 6, 2010, 4:12:49 AM12/6/10
to growing-object-o...@googlegroups.com, Growing Object-Oriented Software
Following on from Nat, the problem with annotating a class with its scope is that an object shouldn't really control its own lifecycle. That should be the job of its context.

S.

Steve Freeman
http://www.m3p.co.uk/blog

Written on a phone, so please allow for typos and short content.

Moandji Ezana

unread,
Dec 6, 2010, 4:41:26 AM12/6/10
to growing-object-o...@googlegroups.com
On Mon, Dec 6, 2010 at 10:12 AM, Steve Freeman <st...@m3p.co.uk> wrote:
Following on from Nat, the problem with annotating a class with its scope is that an object shouldn't really control its own lifecycle. That should be the job of its context.

But different lifecycles might lead to different designs. For example, a singleton needs to be thread-safe, a request- or thread-scoped class does not. This kind of info would be in the JavaDoc anyway, so why not have the class declare how it wants to be handled?

Moandji

Esko Luontola

unread,
Dec 6, 2010, 4:53:39 AM12/6/10
to Growing Object-Oriented Software
In Guice, the scope can be declared also without annotations - in the
bind statements: http://code.google.com/p/google-guice/wiki/Scopes

David Harvey

unread,
Dec 6, 2010, 9:19:33 AM12/6/10
to growing-object-o...@googlegroups.com
Annotations are the dandruff of Java

David
------------------
David Harvey
www.teamsandtechnology.com
www.cateams.com
@david_harvey

Steve Freeman

unread,
Dec 6, 2010, 4:18:31 PM12/6/10
to growing-object-o...@googlegroups.com

the implementation of a singleton needs to be thread-safe, but it doesn't need to know if it's the only instance. And anything but read-only singletons are a bad idea anyway, so best not to encourage them... :)

S.

J. B. Rainsberger

unread,
Dec 6, 2010, 7:58:44 PM12/6/10
to growing-object-o...@googlegroups.com
No design requires a singleton. I prefer to Just Create One.
-- 

Esko Luontola

unread,
Dec 7, 2010, 4:30:05 AM12/7/10
to Growing Object-Oriented Software
On Dec 7, 2:58 am, "J. B. Rainsberger" <m...@jbrains.ca> wrote:
> On Mon, Dec 6, 2010 at 19:41, Moandji Ezana <mwa...@gmail.com> wrote:
> > On Mon, Dec 6, 2010 at 10:12 AM, Steve Freeman <st...@m3p.co.uk> wrote:
>
> >> Following on from Nat, the problem with annotating a class with its scope
> >> is that an object shouldn't really control its own lifecycle. That should be
> >> the job of its context.
>
> > But different lifecycles might lead to different designs. For example, a
> > singleton needs to be thread-safe, a request- or thread-scoped class does
> > not. This kind of info would be in the JavaDoc anyway, so why not have the
> > class declare how it wants to be handled?
>
> No design requires a singleton. I prefer to Just Create One.
>

That's what also the DI containers do. They create just one.

J. B. Rainsberger

unread,
Dec 7, 2010, 9:09:50 PM12/7/10
to growing-object-o...@googlegroups.com
Yes, that's my understanding. As Corey Haines has blurted out over Twitter, I think that rushing to use a DI container hurts the average programmer's learning about design more than it helps.
-- 

David Harvey

unread,
Dec 8, 2010, 2:59:00 AM12/8/10
to growing-object-o...@googlegroups.com
_very_ true (GUICE considered harmful).

David
------------------
David Harvey
www.teamsandtechnology.com
www.cateams.com
@david_harvey


J. B. Rainsberger

unread,
Dec 8, 2010, 3:44:03 AM12/8/10
to growing-object-o...@googlegroups.com
On Wed, Dec 8, 2010 at 17:59, David Harvey <da...@teamsandtechnology.com> wrote:

_very_ true (GUICE considered harmful).

Be careful: I did not say that Guice is harmful. I don't mean that. I don't believe that. In fact, it's provably untrue.

I recommend not using a DI container until one has managed and wired those dependencies by hand and understood the principles that surround it.

Thanks.
-- 

David Harvey

unread,
Dec 8, 2010, 7:41:11 AM12/8/10
to growing-object-o...@googlegroups.com
I've seen too many instances of novices spraying Guice DI annotations throughout a codebase. Agree that you learn by doing it by hand first, but I don't see many people doing that. Not just Guice/DI, of course (nothing makes you appreciate ORM, session management etc more than building your own first).

David


------------------
David Harvey
www.teamsandtechnology.com
www.cateams.com
@david_harvey


Moandji Ezana

unread,
Dec 8, 2010, 8:51:32 AM12/8/10
to growing-object-o...@googlegroups.com
On Wed, Dec 8, 2010 at 1:41 PM, David Harvey <da...@teamsandtechnology.com> wrote:
I've seen too many instances of novices spraying Guice DI annotations throughout a codebase.

But have you seen instances of novices writing a great DI system from scratch? Or even of novices creating a nice, expressive, testable codebase?

Moandji

David Harvey

unread,
Dec 8, 2010, 9:25:08 AM12/8/10
to growing-object-o...@googlegroups.com
No, and no. But it seems to me that one of the best ways of stopping being a novice is to try. Wouldn't expect a great, or even production-quality, DI framework to be the outcome. I would expect a bit more appreciation of the issues it's intended to address, which are 99% of the time at a level of (paradoxically) both abstraction and detail that most developers never suspect exists. Even if it's just on a small personal project it's worth doing.

David


------------------
David Harvey
www.teamsandtechnology.com
www.cateams.com
@david_harvey


J. B. Rainsberger

unread,
Dec 8, 2010, 7:21:20 PM12/8/10
to growing-object-o...@googlegroups.com
On Wed, Dec 8, 2010 at 22:41, David Harvey <da...@teamsandtechnology.com> wrote:

I've seen too many instances of novices spraying Guice DI annotations throughout a codebase. Agree that you learn by doing it by hand first, but I don't see many people doing that. Not just Guice/DI, of course (nothing makes you appreciate ORM, session management etc more than building your own first).

Absolutely agree. Yet another Evil Wizard.
-- 

J. B. Rainsberger

unread,
Dec 8, 2010, 7:21:51 PM12/8/10
to growing-object-o...@googlegroups.com
No, but if they don't learn the principles behind improving the design, then they'll never stop being novices.
-- 

Moandji Ezana

unread,
Dec 9, 2010, 4:04:31 AM12/9/10
to growing-object-o...@googlegroups.com
On Thu, Dec 9, 2010 at 1:21 AM, J. B. Rainsberger <m...@jbrains.ca> wrote:
No, but if they don't learn the principles behind improving the design, then they'll never stop being novices.

Agreed. I just don't want them writing their framework on my project. So we'll use Spring/Guice/etc.

Arguably, though, a small framework like Guice can aid in learning the design principles, since you can concentrate on collaborators, scopes and constructors without getting muddled up in the plumbing. In my experience, Spring is too complex to be a good learning tool.

Moandji

Ben Butler-Cole

unread,
Dec 9, 2010, 4:24:10 AM12/9/10
to growing-object-o...@googlegroups.com
On 9 December 2010 09:04, Moandji Ezana <mwa...@gmail.com> wrote:
On Thu, Dec 9, 2010 at 1:21 AM, J. B. Rainsberger <m...@jbrains.ca> wrote:
No, but if they don't learn the principles behind improving the design, then they'll never stop being novices.

Agreed. I just don't want them writing their framework on my project. So we'll use Spring/Guice/etc.

If you're absolutely determined to use a DI framework, then I recommend you take a look at Newie (http://github.com/guilhermesilveira/newie); it's written in Ruby, but I believe there is a underway to Java. I'd say it was ideal for novices who need to learn the principles behind dependency injection without having to learn the ins and outs of a complex framework.

-Ben

Moandji Ezana

unread,
Dec 9, 2010, 4:35:12 AM12/9/10
to growing-object-o...@googlegroups.com
On Thu, Dec 9, 2010 at 10:24 AM, Ben Butler-Cole <b...@bridesmere.com> wrote:
 

If you're absolutely determined to use a DI framework,

It's not so much that I'm determined, but that I find that DI + scoping (beyond singletons) leads to nicer code and APIs and I don't see the point of rewriting my own version of what Guice does extremely well and in a way I find pleasing.
 
then I recommend you take a look at Newie (http://github.com/guilhermesilveira/newie); it's written in Ruby, but I believe there is a underway to Java. I'd say it was ideal for novices who need to learn the principles behind dependency injection without having to learn the ins and outs of a complex framework.

David Harvey

unread,
Dec 9, 2010, 4:43:45 AM12/9/10
to growing-object-o...@googlegroups.com
IMO there's a neat core to GUICE which got obscured when its developers succumbed to the curse of the cool (as in - "hey, if I add this we could do this ... Cooooool!)

David
------------------
David Harvey
www.teamsandtechnology.com
www.cateams.com
@david_harvey


On 9 December 2010 09:04, Moandji Ezana <mwa...@gmail.com> wrote:

David Peterson

unread,
Dec 9, 2010, 5:08:53 AM12/9/10
to growing-object-o...@googlegroups.com
Love it.

Gianluca Padovani

unread,
Dec 9, 2010, 6:10:55 AM12/9/10
to growing-object-oriented-software
2010/12/5 Esko Luontola <esko.l...@gmail.com>:

>
> As far as I understand, by Gianluca talks about existence in time,
> whereas Nat talks about existence in space.

> A web application typically has request scope, session scope and


> application scope (also known as singleton scope). Other common ones
> are conversation scope (for example filling a form; conversation
> consists of multiple related requests, but is smaller than session
> scope), thread scope, transaction scope

Exactly, you have clearly explained what I want to say
Thank you Esko!!

P.S.
I don't Like this technique and I'm trying to understand how to
resolve the problem raised in the PDF in another way.

J. B. Rainsberger

unread,
Dec 9, 2010, 6:16:28 AM12/9/10
to growing-object-o...@googlegroups.com
On Thu, Dec 9, 2010 at 19:04, Moandji Ezana <mwa...@gmail.com> wrote:
On Thu, Dec 9, 2010 at 1:21 AM, J. B. Rainsberger <m...@jbrains.ca> wrote:
No, but if they don't learn the principles behind improving the design, then they'll never stop being novices.

Agreed. I just don't want them writing their framework on my project. So we'll use Spring/Guice/etc.

So you want the people on your project to learn these principles, but not while they're on your project? I would think that, while they're on my project, I'd want them to learn these things. Of course, ideally they'd already know them, but if they don't, and they're here, then I'd want them to learn while I'm around to guide them.

Arguably, though, a small framework like Guice can aid in learning the design principles, since you can concentrate on collaborators, scopes and constructors without getting muddled up in the plumbing. In my experience, Spring is too complex to be a good learning tool.

I believe you can do the same simple things with both, if you restrict yourself to those features.

Gianluca Padovani

unread,
Dec 9, 2010, 6:28:29 AM12/9/10
to growing-object-oriented-software
2010/12/9 J. B. Rainsberger <m...@jbrains.ca>:

> I believe you can do the same simple things with both, if you restrict
> yourself to those features.

Hi Joe,
I don't understand what you suggested, let me try to recap:

- Don't use DI framework if you are a novice.
- Use DI framework only if you know the principle behind the framework

Is it correct?

I think the discussion is not only about the PDF that I sent but also
about the use of framework for DI.

The PDF, I think, suggests to use some principles/ideas (the scope and
the injector class) that could be used instead of a framework.

Isn't that what you are suggesting Joe?

bye
Gianluca

Moandji Ezana

unread,
Dec 9, 2010, 8:32:59 AM12/9/10
to growing-object-o...@googlegroups.com
On Thu, Dec 9, 2010 at 12:16 PM, J. B. Rainsberger <m...@jbrains.ca> wrote:
So you want the people on your project to learn these principles, but not while they're on your project?

No, I don't want them writing the framework (I'd rather not write my own framework on my project). I'm happy for people to learn principles while on a project (I do so all the time). Then, I'd rather use a well-known and robust implementation of those principles than a buggy and perpetually half-finished one. As a side benefit, you might run into that framework again on another project.

I just came across this, which is close to my own feelings: http://www.jonarcher.com/2010/12/perils-of-rolling-your-own.html


Arguably, though, a small framework like Guice can aid in learning the design principles, since you can concentrate on collaborators, scopes and constructors without getting muddled up in the plumbing. In my experience, Spring is too complex to be a good learning tool.
I believe you can do the same simple things with both, if you restrict yourself to those features.
Of course, but in my experience of teaching people, getting up and running in Spring has always been a little complex, while doing so in Guice is trivial.

Moandji


Matteo Vaccari

unread,
Dec 9, 2010, 9:36:47 AM12/9/10
to growing-object-o...@googlegroups.com
On Thu, Dec 9, 2010 at 2:32 PM, Moandji Ezana <mwa...@gmail.com> wrote:
On Thu, Dec 9, 2010 at 12:16 PM, J. B. Rainsberger <m...@jbrains.ca> wrote:
So you want the people on your project to learn these principles, but not while they're on your project?

No, I don't want them writing the framework (I'd rather not write my own framework on my project). I'm happy for people to learn principles while on a project (I do so all the time). Then, I'd rather use a well-known and robust implementation of those principles than a buggy and perpetually half-finished one. As a side benefit, you might run into that framework again on another project.

I disagree.  Not using a framework is not the same as "then I will write my own framework".  If I work on implementing just the features that I need for my customer, I will not need to write a framework.  I will write just enough infrastructure to solve my customer's problem.  This in general will be vastly less complicated than writing a framework.

I too worked with people who wanted to write a framework to do X instead of just doing X.  It's overengineering.  TDD is supposed to help avoid this problem.

Matteo

 

J. B. Rainsberger

unread,
Dec 9, 2010, 7:02:04 PM12/9/10
to growing-object-o...@googlegroups.com
On Thu, Dec 9, 2010 at 21:28, Gianluca Padovani <gpad...@gmail.com> wrote:
2010/12/9 J. B. Rainsberger <m...@jbrains.ca>:
> I believe you can do the same simple things with both, if you restrict
> yourself to those features.

Hi Joe,
I don't understand what you suggested, let me try to recap:

- Don't use DI framework if you are a novice.
- Use DI framework only if you know the principle behind the framework

Is it correct?

Without knowing more about a specific situation, yes. I think these two make good guidelines.
 
I think the discussion is not only about the PDF that I sent but also
about the use of framework for DI.

The PDF, I think, suggests to use some principles/ideas (the scope and
the injector class) that could be used instead of a framework.

Isn't that what you are suggesting Joe?

I don't see the use of the scope and injector classes. I've been designing with dependency injection for many years and I didn't understand what the scope and injector classes were for. As far as I can tell, there are only two important guidelines for dependency injection: 

1. Tell your client to give you the dependencies you need.
2. Depend on abstractions instead of details.

I don't see why I'd need scope and injector classes if I followed these guidelines.
-- 

J. B. Rainsberger

unread,
Dec 9, 2010, 7:13:00 PM12/9/10
to growing-object-o...@googlegroups.com
On Thu, Dec 9, 2010 at 23:32, Moandji Ezana <mwa...@gmail.com> wrote:
On Thu, Dec 9, 2010 at 12:16 PM, J. B. Rainsberger <m...@jbrains.ca> wrote:
So you want the people on your project to learn these principles, but not while they're on your project?

No, I don't want them writing the framework (I'd rather not write my own framework on my project). I'm happy for people to learn principles while on a project (I do so all the time). Then, I'd rather use a well-known and robust implementation of those principles than a buggy and perpetually half-finished one. As a side benefit, you might run into that framework again on another project.

Why would they write a framework at all? Why not just follow two simple guidelines (Demand your dependencies from your client; Only depend on abstractions)?

If your people write buggy software, then isn't a framework just going to help them write even more buggy software?

If your people never finish anything they write, then isn't a framework just going to help them write even more half-finished software?

I agree that one shouldn't rewrite software if one can reuse it, but I balance that against the Evil Wizard problem: if I don't know the principles behind it, then I'm not ready to use it yet.

I just came across this, which is close to my own feelings: http://www.jonarcher.com/2010/12/perils-of-rolling-your-own.html

I find the analogy to smoking a bit specious. I understand the author's perspective, and would like to bring to the surface an unfounded assumption he makes.

He assumes that, if I roll my own, then I'm never going to switch to something else. I have the opposite experience. In 2002-2003 I found myself in a similar situation on a project: I couldn't tell which of the many Web MVC frameworks in Javaland supported TDD well. I knew Struts 1 didn't, as it forced me to create extension points by inheriting implementation (subclassing). Rather than jump into another framework and hoping for the best, I spent ten weeks building a web application starting with a Servlet and ruthlessly refactoring. By the end of that time, I saw that Spring Web MVC matched my ideas most closely. It took only two weeks to migrate existing code to use Spring Web MVC. I've seen teams waste much more than three months fighting with frameworks they didn't get to evaluate before they used them. The first difference? I designed simply, so moving away from my home-grown libraries was painless and I could do so incrementally. The other difference? I decided to move away and did it. I didn't half-ass it. I just did it.

So yes, if you roll your own and allow yourself to lock in to your own solution, then you might run into trouble. Therefore, do not lock yourself in to your own solution.

I believe you can do the same simple things with both, if you restrict yourself to those features.
Of course, but in my experience of teaching people, getting up and running in Spring has always been a little complex, while doing so in Guice is trivial.

I don't see benefit in making a dubious step easier. To each his own.
-- 

Moandji Ezana

unread,
Dec 10, 2010, 3:00:54 PM12/10/10
to growing-object-o...@googlegroups.com
J.B.,

I read your last couple of mails this morning and let them sink in for the rest of the day before responding. I found them really valuable, so wanted to respond in kind.

1. Tell your client to give you the dependencies you need.
2. Depend on abstractions instead of details.

I love the concise way you've expressed this. I like to think that I apply (or at least try to apply) these principles, but I'd never boiled them down so well. 

On Fri, Dec 10, 2010 at 1:13 AM, J. B. Rainsberger <m...@jbrains.ca> wrote:
I have the opposite experience. In 2002-2003 (...)

Thanks for that story. I'm kind of biased here because I've written my own little MVC framework based on Guice. After reading your emails, I thought a little more about 1) Could I write it without Guice? and 2) Why am I using Guice?

Thinking about the first question, I guess I could, fairly easily. Annotations aside, there's nothing that fundamentally necessitates a framework: I have interfaces and implementations broken down according to fairly logical, self-contained chunks of functionality. For example, a Responder is an interface for something that knows how to write some data to the HTTP response. One implementation is the JsonResponder, that takes a JSONObject in its constructor. Other interfaces handle looking up the correct request handler, convert request parameters, etc. Were I to rip Guice out, I guess I'd have to move the wiring and knowledge of scopes into the servlet that receives and dispatches all requests (or a collaborator). That wouldn't be too difficult, but it would stop being a framework.

To answer the second question, I like that Guice avoids my having to write out all the wiring, I continue to find it more logical to put scope requirements on the class itself and finally it makes it easier for people to integrate with and extend the framework. For example, I can get sensible defaults by putting @ImplementedBy(SomeClass.class) on an interface to get default behaviour, which can then be overridden by the user if necessary.

Thanks for the thought-provoking conversation,

Moandji

David Harvey

unread,
Dec 10, 2010, 3:17:19 PM12/10/10
to growing-object-o...@googlegroups.com
>> @ImplementedBy(SomeClass.class)
I've spent a happy afternoon getting rid of this from a large codebase. Mysterious defaults, scattered around all your interface files: not good, IMHO, and one of the bits of Guice that I wish people would stay away from. 

David

------------------
David Harvey
www.teamsandtechnology.com
www.cateams.com
@david_harvey


Moandji Ezana

unread,
Dec 10, 2010, 3:25:13 PM12/10/10
to growing-object-o...@googlegroups.com
On Fri, Dec 10, 2010 at 9:17 PM, David Harvey <da...@teamsandtechnology.com> wrote:
>> @ImplementedBy(SomeClass.class)
I've spent a happy afternoon getting rid of this from a large codebase. Mysterious defaults, scattered around all your interface files: not good, IMHO, and one of the bits of Guice that I wish people would stay away from. 

I wouldn't use it in application code, but I think it makes sense in framework code that's aiming for convention over configuration.

Moandji 

Raoul Duke

unread,
Dec 10, 2010, 3:25:59 PM12/10/10
to growing-object-o...@googlegroups.com
On Fri, Dec 10, 2010 at 12:17 PM, David Harvey
<da...@teamsandtechnology.com> wrote:
>>> @ImplementedBy(SomeClass.class)
> I've spent a happy afternoon getting rid of this from a large codebase.
> Mysterious defaults, scattered around all your interface files: not good,
> IMHO, and one of the bits of Guice that I wish people would stay away from.

it is sorta amusing/odd/weird/annoying to me how java started out
with, i think, a very "be clear and explicit and just live with the
boilerplate" and then has been shoved over time ever more towards
"sprinkle some magic on".

Moandji Ezana

unread,
Dec 10, 2010, 3:32:18 PM12/10/10
to growing-object-o...@googlegroups.com
On Fri, Dec 10, 2010 at 9:25 PM, Raoul Duke <rao...@gmail.com> wrote:
it is sorta amusing/odd/weird/annoying to me how java started out
with, i think, a very "be clear and explicit and just live with the
boilerplate" and then has been shoved over time ever more towards
"sprinkle some magic on".

I think it's a trade-off between explicitness vs. productivity and concision. I get the impression Ruby on Rails really changed the balance that people were willing to live with.

Moandji

David Harvey

unread,
Dec 10, 2010, 4:20:45 PM12/10/10
to growing-object-o...@googlegroups.com
Ah, but rails favours _convention_ as a means to achieve _concision_. The magic fairy dust that is @ImplementedBy could be sprinkled by wizards (or more likely trainee elves) anywhere they like. The _convention_ would be to place all bindings in modules in a well-known package. But even that ain't necessarily done...

David
------------------
David Harvey
www.teamsandtechnology.com
www.cateams.com
@david_harvey


J. B. Rainsberger

unread,
Dec 10, 2010, 7:38:30 PM12/10/10
to growing-object-o...@googlegroups.com
On Sat, Dec 11, 2010 at 06:00, Moandji Ezana <mwa...@gmail.com> wrote:

I read your last couple of mails this morning and let them sink in for the rest of the day before responding. I found them really valuable, so wanted to respond in kind.

I really appreciate that, Moandji. Very kind of you.
 
I love the concise way you've expressed this. I like to think that I apply (or at least try to apply) these principles, but I'd never boiled them down so well. 

I find that some people do this well, but then go past it to more than they need.
 
Thanks for that story. I'm kind of biased here because I've written my own little MVC framework based on Guice. After reading your emails, I thought a little more about 1) Could I write it without Guice? and 2) Why am I using Guice?

Thinking about the first question, I guess I could, fairly easily. Annotations aside, there's nothing that fundamentally necessitates a framework: I have interfaces and implementations broken down according to fairly logical, self-contained chunks of functionality. For example, a Responder is an interface for something that knows how to write some data to the HTTP response. One implementation is the JsonResponder, that takes a JSONObject in its constructor. Other interfaces handle looking up the correct request handler, convert request parameters, etc. Were I to rip Guice out, I guess I'd have to move the wiring and knowledge of scopes into the servlet that receives and dispatches all requests (or a collaborator). That wouldn't be too difficult, but it would stop being a framework.

To answer the second question, I like that Guice avoids my having to write out all the wiring, I continue to find it more logical to put scope requirements on the class itself and finally it makes it easier for people to integrate with and extend the framework. For example, I can get sensible defaults by putting @ImplementedBy(SomeClass.class) on an interface to get default behaviour, which can then be overridden by the user if necessary.

To me, this last bit defeats the purpose of injecting dependencies, because it refers to a detail (an implementation). I don't believe in "sensible defaults" for an interface; indeed, that's the point of an interface: to capture the essential collaboration between a client and supplier without constraining how the supplier does its work. As soon as we talk about "sensibel default implementations", we introduce unhealthy dependencies.
 
Thanks for the thought-provoking conversation,

I hope to continue it!
-- 

Moandji Ezana

unread,
Dec 11, 2010, 5:17:59 AM12/11/10
to growing-object-o...@googlegroups.com

About the sensible defaults, doesn't it make sense in a framework, though? Like how rails or grails provide a default persistence mechanism, which you can change if you wish. Imagine if instead you had to specify the implementation of every single module the framework used. It would be a nightmare.

In my case, I don't see the value, from a usability/friendliness point of view, of forcing the user to choose a routing mechansim every single time. Yes, it's impure from a DI standpoint, but makes frameworks easier to use.

Perhaps an alternative could be to bundle up a set of bindings and make that a "one-stop" configuration.

Moandji

--
www.moandjiezana.com

Sent from my phone

On 11 Dec 2010 01:38, "J. B. Rainsberger" <m...@jbrains.ca> wrote:

J. B. Rainsberger

unread,
Dec 11, 2010, 7:10:29 AM12/11/10
to growing-object-o...@googlegroups.com
On Sat, Dec 11, 2010 at 18:17, Moandji Ezana <mwa...@gmail.com> wrote:

About the sensible defaults, doesn't it make sense in a framework, though? Like how rails or grails provide a default persistence mechanism, which you can change if you wish.

Neither Rails nor Grails uses a dependency injection mechanism and neither Rails nor Grails extracts interfaces for those services.

Also, choosing an implementation in a particular application is different from assigning a globally default implementation for an interface.

Imagine if instead you had to specify the implementation of every single module the framework used. It would be a nightmare.

Those two aren't the only choices. At any rate, sprinkling the choice of implementation for a specific application throughout the code base creates the very problem that the dependency inversion principle is meant to avoid. 

In my case, I don't see the value, from a usability/friendliness point of view, of forcing the user to choose a routing mechansim every single time. Yes, it's impure from a DI standpoint, but makes frameworks easier to use.

I don't understand the "every single time" bit. Do you mean once per application as "every single time"? Do you mean that it's unfriendly to ask a programmer to specify the implementation of an interface once per application? I don't agree with that. 

Perhaps an alternative could be to bundle up a set of bindings and make that a "one-stop" configuration.

Well, yes. That's what the entry point of an application does when one injects dependencies following the guidelines I mentioned earlier in this thread. It bundles up all the bindings in one place. Scattering them throughout the code creates the problem that injecting dependencies is supposed to solve.

I think we're on the same page, now.
-- 
Reply all
Reply to author
Forward
0 new messages