On 5/19/07, Allen Gilliland <[EMAIL PROTECTED]> wrote:
Dave wrote:
> On 5/19/07, Allen Gilliland <[EMAIL PROTECTED]> wrote:
> If a DI framework requires classes to have public constructors, even
> those that are singletons -- you would completely rule out the usage
> of that framework? I think that rules out both Spring IOC and Guice
> and I'm not ready to rule out either.
I am not saying we have to rule them out, I am simply saying that from
an api point of view I think it's best to truly enforce constraints such
as the singleton pattern when they are critical. Truth be told, I don't
think there is any real reason why many of our manager classes and even
the Roller class needs to be a singleton, we mainly do that for
performance reasons because there is no benefit to constructing them
over and over again. So for that reason I don't think it's such a big
deal to rely on Guice or any other DI framework to handle the singleton
functionality for us, and I would be okay with the public constructor.
That's a very good point: except for performance reasons, not all of
our singletons need to be singletons.
However, in situations where it's imperative that the singleton pattern
be properly enforced I think it's best that we actually do so in the
code both for consistency and to make the code read the way it's
supposed to work. An example would be the HibernatePersistenceStrategy
class, which *must* be a singleton because we don't want multiple
hibernate SessionFactory instances floating around on accident. In that
case I think we should abandon the public constructor and use the
singleton pattern the way it's meant to be enforced. This also does not
prevent us from using Guice because from the docs it says you can bind
instances to classes just as easily as you bind anything else, so you
could use this in the HibernateModule ...
binder.bind(HibernatePersistenceStrategy.class).toInstance(
HibernatePersistenceStrategy.getInstance());
Sweet!
So the two issues you raised can be addressed:
- Want immutable Roller object: use constructor injection
- Want an enforced singleton: use bind-to-instance.
What else could be improved in the roller_guice experiment?
Does it really take full advantage of DI?
- Dave