You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by "Brown, Berlin [GCG-PFS]" <Be...@Primerica.com> on 2010/06/15 21:11:35 UTC

Newbie question anti-patterns and wicket, constructor component building

I am sorry, am just getting used to Wicket but I notice a lot of use of
calling a lot of code in the constructor.  Does it really matter?  I
mention it because this kind of style makes it difficult to test code
because code in the constructor may fail and the object won't be
created.
 
Should I just create a method and call that method in the constructor?

Re: Newbie question anti-patterns and wicket, constructor component building

Posted by Jeremy Thomerson <je...@wickettraining.com>.
>
>
> Well, the second version uses constructer injection.  Some frameworks
> prefer that approach.
>
> But, I see your point.
>

You should not be calling a service or dao directly in your constructor,
regardless of whether you are using dependency injection or not.  This is
bad.  That's what models are for.  Load data in models - not while
constructing an object.

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

Re: Newbie question anti-patterns and wicket, constructor component building

Posted by Jeremy Thomerson <je...@wickettraining.com>.
>
>
> Well, the second version uses constructer injection.  Some frameworks
> prefer that approach.
>
> But, I see your point.
>

You should not be calling a service or dao directly in your constructor,
regardless of whether you are using dependency injection or not.  This is
bad.  That's what models are for.  Load data in models - not while
constructing an object.

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

RE: Newbie question anti-patterns and wicket, constructor component building

Posted by "Brown, Berlin [GCG-PFS]" <Be...@Primerica.com>.
 

-----Original Message-----
From: Jeremy Thomerson [mailto:jeremy@wickettraining.com] 
Sent: Tuesday, June 15, 2010 3:25 PM
To: users@wicket.apache.org
Subject: Re: Newbie question anti-patterns and wicket, constructor
component building

On Tue, Jun 15, 2010 at 2:11 PM, Brown, Berlin [GCG-PFS] <
Berlin.Brown@primerica.com> wrote:

> I am sorry, am just getting used to Wicket but I notice a lot of use 
> of calling a lot of code in the constructor.  Does it really matter?  
> I mention it because this kind of style makes it difficult to test 
> code because code in the constructor may fail and the object won't be 
> created.
>
> Should I just create a method and call that method in the constructor?
>

The constructor is supposed to construct the object.  In Wicket, this
also
(typically) means constructing the component hierarchy.  So, your
constructor should be doing things like add(new FooPanel("foo", new
SomeModel()));  It should NOT be doing things like:

List<Foo> foos = SomeDao.loadAll();
new FooPanel("foo", foos);

That's the kind of code I see newbs putting in their constructor all the
time - and it should NOT be there.

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

-----

Well, the second version uses constructer injection.  Some frameworks
prefer that approach.

But, I see your point.


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


Re: Newbie question anti-patterns and wicket, constructor component building

Posted by Jeremy Thomerson <je...@wickettraining.com>.
On Tue, Jun 15, 2010 at 2:11 PM, Brown, Berlin [GCG-PFS] <
Berlin.Brown@primerica.com> wrote:

> I am sorry, am just getting used to Wicket but I notice a lot of use of
> calling a lot of code in the constructor.  Does it really matter?  I
> mention it because this kind of style makes it difficult to test code
> because code in the constructor may fail and the object won't be
> created.
>
> Should I just create a method and call that method in the constructor?
>

The constructor is supposed to construct the object.  In Wicket, this also
(typically) means constructing the component hierarchy.  So, your
constructor should be doing things like add(new FooPanel("foo", new
SomeModel()));  It should NOT be doing things like:

List<Foo> foos = SomeDao.loadAll();
new FooPanel("foo", foos);

That's the kind of code I see newbs putting in their constructor all the
time - and it should NOT be there.

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