You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Alfredo Aleandri <al...@logobject.ch> on 2009/05/15 15:01:02 UTC

Application scope vs Singleton

Hi,
I have a doubt about application-scoped objects.
What's the pro and cons of setting an object instance into my 
WebApplication class or define that class as singleton (using a static 
method to access it) ?

Thank you

alf


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Application scope vs Singleton

Posted by James Carman <jc...@carmanconsulting.com>.
On Sat, May 16, 2009 at 2:39 PM, nino martinez wael
<ni...@gmail.com> wrote:
> you can mock statict classes with power mock , but i'vé come to the
> konklusion that it is a code smell going that way ioc are much better.

Right, I didn't say it was impossible.  I said it would be more
difficult.  Dependency injection is definitely the best way to go, but
the "lookupable" services on the application object would work also
(how those service implementations get initialized is a different
story, though).

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Application scope vs Singleton

Posted by nino martinez wael <ni...@gmail.com>.
you can mock statict classes with power mock , but i'vé come to the
konklusion that it is a code smell going that way ioc are much better.

-from my htc

2009/5/15, James Carman <jc...@carmanconsulting.com>:
> On Fri, May 15, 2009 at 12:18 PM, alf.redo
> <al...@logobject.ch> wrote:
>>
>> Thank you to all for your precious suggestions.
>>
>> My question is not for a real need.
>> Supposing to discard the injection "strategy", I would like to know if the
>> cache of an object into my WebApplication class during application startup
>> has the same result if I make this object Singleton (and not store it in
>> WebApplication). Can be some problems about "thread-safety" or other issue
>> in one case rather than the other?
>
> Making the object a "singleton" would make unit testing more difficult,
> IMHO.
>
> Thread-safety is all up to how you implement the class, really.  You'd
> have to be aware of threading issues either way.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

-- 
Sendt fra min mobile enhed

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Application scope vs Singleton

Posted by James Carman <jc...@carmanconsulting.com>.
On Fri, May 15, 2009 at 12:18 PM, alf.redo
<al...@logobject.ch> wrote:
>
> Thank you to all for your precious suggestions.
>
> My question is not for a real need.
> Supposing to discard the injection "strategy", I would like to know if the
> cache of an object into my WebApplication class during application startup
> has the same result if I make this object Singleton (and not store it in
> WebApplication). Can be some problems about "thread-safety" or other issue
> in one case rather than the other?

Making the object a "singleton" would make unit testing more difficult, IMHO.

Thread-safety is all up to how you implement the class, really.  You'd
have to be aware of threading issues either way.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Application scope vs Singleton

Posted by "alf.redo" <al...@logobject.ch>.
Thank you to all for your precious suggestions.

My question is not for a real need. 
Supposing to discard the injection "strategy", I would like to know if the
cache of an object into my WebApplication class during application startup
has the same result if I make this object Singleton (and not store it in
WebApplication). Can be some problems about "thread-safety" or other issue
in one case rather than the other? 

I'm sorry if this seems to be a stupid question...

alf



-- 
View this message in context: http://www.nabble.com/Application-scope-vs-Singleton-tp23559402p23563036.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Application scope vs Singleton

Posted by James Carman <jc...@carmanconsulting.com>.
On Fri, May 15, 2009 at 11:26 AM, Jeremy Thomerson
<je...@wickettraining.com> wrote:
> Use injection.  The difference is that there is no way to override the
> implementation of the static accessor / singleton for testing
> functionality.  If you instead use an IoC container (Spring / Guice)
> and injection, you are not statically tying yourself to a single
> class.  Work off the interface, and all the places in your code that
> use that interface can be changed to use a different implementation
> without changing each of them - just change the config as to which one
> is injected.

You could always use a mock application object in your tests that has
mock objects for the singletons defined there.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Application scope vs Singleton

Posted by Jeremy Thomerson <je...@wickettraining.com>.
Use injection.  The difference is that there is no way to override the
implementation of the static accessor / singleton for testing
functionality.  If you instead use an IoC container (Spring / Guice)
and injection, you are not statically tying yourself to a single
class.  Work off the interface, and all the places in your code that
use that interface can be changed to use a different implementation
without changing each of them - just change the config as to which one
is injected.

--
Jeremy Thomerson
http://www.wickettraining.com




On Fri, May 15, 2009 at 10:21 AM, alf.redo
<al...@logobject.ch> wrote:
>
> Hi James,
> I would like to know what is the difference between a Singleton class with a
> static accessor method and POJO stored into my WebApplication class (with
> proper getter).
> What is the preferred way to set an application scoped object?
>
> Thank you again...
>
>
> --
> View this message in context: http://www.nabble.com/Application-scope-vs-Singleton-tp23559402p23562038.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Application scope vs Singleton

Posted by "alf.redo" <al...@logobject.ch>.
Hi James,
I would like to know what is the difference between a Singleton class with a
static accessor method and POJO stored into my WebApplication class (with
proper getter). 
What is the preferred way to set an application scoped object?

Thank you again...


-- 
View this message in context: http://www.nabble.com/Application-scope-vs-Singleton-tp23559402p23562038.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Application scope vs Singleton

Posted by James Carman <jc...@carmanconsulting.com>.
On Fri, May 15, 2009 at 9:01 AM, Alfredo Aleandri
<al...@logobject.ch> wrote:
> Hi,
> I have a doubt about application-scoped objects.

What is your doubt?

> What's the pro and cons of setting an object instance into my WebApplication
> class or define that class as singleton (using a static method to access it)
> ?

If you always access it through the "getter" method on the
application, I don't think there's any real problem with that.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Application scope vs Singleton

Posted by Jan Torben Heuer <jt...@mail2004.dnsalias.org>.
Alfredo Aleandri wrote:

> I have a doubt about application-scoped objects.
> What's the pro and cons of setting an object instance into my
> WebApplication class or define that class as singleton (using a static
> method to access it) ?

Pro: Singletons are easy to use
Contra: They make Unit tests and re-usability difficult.

I personally use dependency-injection with wicket-guice.

Jan



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org