You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by William Speirs <ws...@apache.org> on 2012/11/30 03:18:04 UTC
Pages, Panels, and Dependency Injection
I'm having trouble understanding how to inject components into a page so
that the page will be easy to unit test later. Say I have a page that
contains two panels. I can easily use constructor injection to inject these
panels into the page:
class MyPage extends WebPage {
@Inject
public MyPage(PanelA a, PanelB b) { ... }
}
The problem is that all Panels require an id during construction.[1] How do
I supply the id to my Panels? I could simply construct every PanelA with an
id of "panela" and every PanelB with an id of "panel", but that doesn't
seem very flexible. What do other people do in this situation? The hope
would be to pass mocked panels into the page during unit testing,
the separately test each panel.
What if instead of a panel it was a button where the onSubmit method must
be specified by overriding the method. How does one go about injecting such
a component so that it's still easy to test later in unit tests?
All thoughts and/or best practices are greatly welcomed. For reference I'm
using Guice as my dependency injection framework and
GuiceWebApplicationFactory to inject components into pages.
Thanks...
Bill-
[1]
http://ci.apache.org/projects/wicket/apidocs/6.0.x/org/apache/wicket/markup/html/panel/Panel.html
Re: Pages, Panels, and Dependency Injection
Posted by William Speirs <ws...@apache.org>.
@Martin sounds like session is the way to go because passing something as a
page parameter means that the user could just simply change the ID to
whatever they want... never trust the client :-)
@Chris interesting project, I'll have to take a look.
Thank you both!
Bill-
On Tue, Jan 29, 2013 at 8:13 PM, Chris Colman
<ch...@stepaheadsoftware.com>wrote:
> Webapps are excellent candidates for injecting into the 'thread' rather
> than providing every injectable class with its own special constructor
> (a lot of boring, mechanical work and problem not as run time efficient)
> and then setting up XML or annotations to configure the inject
> mechanisms.
>
> A simple servlet filter "injects" (attaches to) the thread on the way in
> to servicing the request and detaches from the thread on the way out.
>
> We created a very lightweight framework that uses thread injection for
> this purpose and it works magically and requires no special changes to
> UI or model code to work. It's called expojo (expose POJOs) and it's at
> expojo.com
>
> A 'ModelExposer' object is always available to any active thread via a
> call to ModelExposer.get(). The ModelExposer provides repositories (kind
> of like 'look up' DAOs that contain your query methods for your
> particular ORM) and services (DAOs that can make changes to the model).
> So from anywhere in your code you have access to all the repository and
> services you will every need.
>
> It's not the way Spring or Guice do it but it works really well and
> doesn't need any XML or annotations at all and doesn't require you
> change your model or UI objects.
>
> The exPOJO servlet also implements 'open session/persistence manager in
> view' so you can avoid all those nasty 'lazy load' exceptions with ease.
>
>
>
> >-----Original Message-----
> >From: Bill Speirs [mailto:bill.speirs@gmail.com]
> >Sent: Tuesday, 29 January 2013 12:28 PM
> >To: users@wicket.apache.org
> >Subject: Re: Pages, Panels, and Dependency Injection
> >
> >Still struggling with how to do this. Martin, I understand that most
> people
> >will simply call new PanelA and new PanelB inside their MyPage code
> instead
> >of trying to inject them. However, how does one setup a page that
> requires
> >some service (DAO for example) and that might optionally require a
> >UserBean.
> >
> >So without using DI, I'd simply have 2 constructors:
> >
> >MyPage(DAO myDao)
> >
> >MyPage(DAO myDao, UserBean currentUser)
> >
> >But I cannot create that second constructor as I don't have anything to
> >bind (using Guice terms) UserBean to.
> >
> >Am I required to make a factory for MyPage at that point? Is it
> bad/wrong
> >to pass the UserBean into the constructor via a response page:
> >
> >setResponsePage(new MyPage(new DAO(), currentUser));
> >
> >Thanks...
> >
> >Bill-
> >
> >
> >On Fri, Nov 30, 2012 at 3:35 AM, Martin Grigorov
> ><mg...@apache.org>wrote:
> >
> >> Hi,
> >>
> >> Most of the time people inject services to their components.
> >>
> >> To test just components you can use
> WicketTester#startComponentInPage()
> >> methods.
> >>
> >>
> >> On Fri, Nov 30, 2012 at 3:18 AM, William Speirs <ws...@apache.org>
> >> wrote:
> >>
> >> > I'm having trouble understanding how to inject components into a
> page
> >so
> >> > that the page will be easy to unit test later. Say I have a page
> that
> >> > contains two panels. I can easily use constructor injection to
> inject
> >> these
> >> > panels into the page:
> >> >
> >> > class MyPage extends WebPage {
> >> > @Inject
> >> > public MyPage(PanelA a, PanelB b) { ... }
> >> > }
> >> >
> >> > The problem is that all Panels require an id during
> construction.[1]
> >How
> >> do
> >> > I supply the id to my Panels? I could simply construct every PanelA
> >with
> >> an
> >> > id of "panela" and every PanelB with an id of "panel", but that
> doesn't
> >> > seem very flexible. What do other people do in this situation? The
> hope
> >> > would be to pass mocked panels into the page during unit testing,
> >> > the separately test each panel.
> >> >
> >> > What if instead of a panel it was a button where the onSubmit
> method
> >must
> >> > be specified by overriding the method. How does one go about
> injecting
> >> such
> >> > a component so that it's still easy to test later in unit tests?
> >> >
> >> > All thoughts and/or best practices are greatly welcomed. For
> reference
> >> I'm
> >> > using Guice as my dependency injection framework and
> >> > GuiceWebApplicationFactory to inject components into pages.
> >> >
> >> > Thanks...
> >> >
> >> > Bill-
> >> >
> >> > [1]
> >> >
> >> >
> >>
> >http://ci.apache.org/projects/wicket/apidocs/6.0.x/org/apache/wicket/ma
> rkup
> >/html/panel/Panel.html
> >> >
> >>
> >>
> >>
> >> --
> >> Martin Grigorov
> >> jWeekend
> >> Training, Consulting, Development
> >> http://jWeekend.com <http://jweekend.com/>
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
RE: Pages, Panels, and Dependency Injection
Posted by Chris Colman <ch...@stepaheadsoftware.com>.
Webapps are excellent candidates for injecting into the 'thread' rather
than providing every injectable class with its own special constructor
(a lot of boring, mechanical work and problem not as run time efficient)
and then setting up XML or annotations to configure the inject
mechanisms.
A simple servlet filter "injects" (attaches to) the thread on the way in
to servicing the request and detaches from the thread on the way out.
We created a very lightweight framework that uses thread injection for
this purpose and it works magically and requires no special changes to
UI or model code to work. It's called expojo (expose POJOs) and it's at
expojo.com
A 'ModelExposer' object is always available to any active thread via a
call to ModelExposer.get(). The ModelExposer provides repositories (kind
of like 'look up' DAOs that contain your query methods for your
particular ORM) and services (DAOs that can make changes to the model).
So from anywhere in your code you have access to all the repository and
services you will every need.
It's not the way Spring or Guice do it but it works really well and
doesn't need any XML or annotations at all and doesn't require you
change your model or UI objects.
The exPOJO servlet also implements 'open session/persistence manager in
view' so you can avoid all those nasty 'lazy load' exceptions with ease.
>-----Original Message-----
>From: Bill Speirs [mailto:bill.speirs@gmail.com]
>Sent: Tuesday, 29 January 2013 12:28 PM
>To: users@wicket.apache.org
>Subject: Re: Pages, Panels, and Dependency Injection
>
>Still struggling with how to do this. Martin, I understand that most
people
>will simply call new PanelA and new PanelB inside their MyPage code
instead
>of trying to inject them. However, how does one setup a page that
requires
>some service (DAO for example) and that might optionally require a
>UserBean.
>
>So without using DI, I'd simply have 2 constructors:
>
>MyPage(DAO myDao)
>
>MyPage(DAO myDao, UserBean currentUser)
>
>But I cannot create that second constructor as I don't have anything to
>bind (using Guice terms) UserBean to.
>
>Am I required to make a factory for MyPage at that point? Is it
bad/wrong
>to pass the UserBean into the constructor via a response page:
>
>setResponsePage(new MyPage(new DAO(), currentUser));
>
>Thanks...
>
>Bill-
>
>
>On Fri, Nov 30, 2012 at 3:35 AM, Martin Grigorov
><mg...@apache.org>wrote:
>
>> Hi,
>>
>> Most of the time people inject services to their components.
>>
>> To test just components you can use
WicketTester#startComponentInPage()
>> methods.
>>
>>
>> On Fri, Nov 30, 2012 at 3:18 AM, William Speirs <ws...@apache.org>
>> wrote:
>>
>> > I'm having trouble understanding how to inject components into a
page
>so
>> > that the page will be easy to unit test later. Say I have a page
that
>> > contains two panels. I can easily use constructor injection to
inject
>> these
>> > panels into the page:
>> >
>> > class MyPage extends WebPage {
>> > @Inject
>> > public MyPage(PanelA a, PanelB b) { ... }
>> > }
>> >
>> > The problem is that all Panels require an id during
construction.[1]
>How
>> do
>> > I supply the id to my Panels? I could simply construct every PanelA
>with
>> an
>> > id of "panela" and every PanelB with an id of "panel", but that
doesn't
>> > seem very flexible. What do other people do in this situation? The
hope
>> > would be to pass mocked panels into the page during unit testing,
>> > the separately test each panel.
>> >
>> > What if instead of a panel it was a button where the onSubmit
method
>must
>> > be specified by overriding the method. How does one go about
injecting
>> such
>> > a component so that it's still easy to test later in unit tests?
>> >
>> > All thoughts and/or best practices are greatly welcomed. For
reference
>> I'm
>> > using Guice as my dependency injection framework and
>> > GuiceWebApplicationFactory to inject components into pages.
>> >
>> > Thanks...
>> >
>> > Bill-
>> >
>> > [1]
>> >
>> >
>>
>http://ci.apache.org/projects/wicket/apidocs/6.0.x/org/apache/wicket/ma
rkup
>/html/panel/Panel.html
>> >
>>
>>
>>
>> --
>> Martin Grigorov
>> jWeekend
>> Training, Consulting, Development
>> http://jWeekend.com <http://jweekend.com/>
>>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Pages, Panels, and Dependency Injection
Posted by Martin Grigorov <mg...@apache.org>.
Hi,
Most people use the session approach. Keep an id (private key) as a field
in the Wicket Session and load the real object on demand when needed.
Similar solution is to make your bean Session scoped. The DI framework
cares to extract it from the session for you.
Some people prefer to be as stateless as possible and avoid using the
session if possible. In this case you can use setResponsePage(Class,
PageParameters) and transfer the id as a request parameter from page to
page.
The "problem" with setResponsePage(Page) is that it makes the page stateful
and the "ugly" ?pageId appears in the url. If this doesn't bother you then
it is very simple solution for your problem.
On Tue, Jan 29, 2013 at 2:40 PM, Bill Speirs <bi...@gmail.com> wrote:
> OK... but how do I pass the current UserBean from one page to another? I
> cannot bind it to Guice as I don't know what it is when I'm setting up my
> Guice Module.
>
> Currently I used setRedirectPage(new MyPage(currentUser)). From what I
> understand you're saying I can switch to setRedirectPage(MyPage.class), but
> then how do I set the UserBean "property" for that instance?
>
> Should these types of values be passed via the session instead? So I simply
> use setRedirectPage(MyPage.class), then inside that class get the current
> user from the session?
>
> The overall question is, what is the best practice for passing values to a
> WebPage from another WebPage via setRedirectPage()? Should you create a new
> instance of the page, or let Wicket do it via reflection? And if Wicket is
> doing it, then how do I "inject" the value?
>
> Thanks...
>
> Bill-
>
>
> On Tue, Jan 29, 2013 at 2:50 AM, Martin Grigorov <mgrigorov@apache.org
> >wrote:
>
> > Hi,
> >
> > You can do something like:
> >
> > class MyPage extends WebPage {
> >
> > @Inject private DAO dao;
> >
> > @Inject @Nullable private UserBean currentUser;
> >
> > ... // use dao or currentUser anywhere in your class
> >
> > }
> >
> > And Guice will try to inject these beans if they are defined in a Guice
> > module.
> > See https://code.google.com/p/google-guice/wiki/UseNullable
> >
> >
> > On Tue, Jan 29, 2013 at 2:27 AM, Bill Speirs <bi...@gmail.com>
> > wrote:
> >
> > > Still struggling with how to do this. Martin, I understand that most
> > people
> > > will simply call new PanelA and new PanelB inside their MyPage code
> > instead
> > > of trying to inject them. However, how does one setup a page that
> > requires
> > > some service (DAO for example) and that might optionally require a
> > > UserBean.
> > >
> > > So without using DI, I'd simply have 2 constructors:
> > >
> > > MyPage(DAO myDao)
> > >
> > > MyPage(DAO myDao, UserBean currentUser)
> > >
> > > But I cannot create that second constructor as I don't have anything to
> > > bind (using Guice terms) UserBean to.
> > >
> > > Am I required to make a factory for MyPage at that point? Is it
> bad/wrong
> > > to pass the UserBean into the constructor via a response page:
> > >
> > > setResponsePage(new MyPage(new DAO(), currentUser));
> > >
> > > Thanks...
> > >
> > > Bill-
> > >
> > >
> > > On Fri, Nov 30, 2012 at 3:35 AM, Martin Grigorov <mgrigorov@apache.org
> > > >wrote:
> > >
> > > > Hi,
> > > >
> > > > Most of the time people inject services to their components.
> > > >
> > > > To test just components you can use
> WicketTester#startComponentInPage()
> > > > methods.
> > > >
> > > >
> > > > On Fri, Nov 30, 2012 at 3:18 AM, William Speirs <ws...@apache.org>
> > > > wrote:
> > > >
> > > > > I'm having trouble understanding how to inject components into a
> page
> > > so
> > > > > that the page will be easy to unit test later. Say I have a page
> that
> > > > > contains two panels. I can easily use constructor injection to
> inject
> > > > these
> > > > > panels into the page:
> > > > >
> > > > > class MyPage extends WebPage {
> > > > > @Inject
> > > > > public MyPage(PanelA a, PanelB b) { ... }
> > > > > }
> > > > >
> > > > > The problem is that all Panels require an id during
> construction.[1]
> > > How
> > > > do
> > > > > I supply the id to my Panels? I could simply construct every PanelA
> > > with
> > > > an
> > > > > id of "panela" and every PanelB with an id of "panel", but that
> > doesn't
> > > > > seem very flexible. What do other people do in this situation? The
> > hope
> > > > > would be to pass mocked panels into the page during unit testing,
> > > > > the separately test each panel.
> > > > >
> > > > > What if instead of a panel it was a button where the onSubmit
> method
> > > must
> > > > > be specified by overriding the method. How does one go about
> > injecting
> > > > such
> > > > > a component so that it's still easy to test later in unit tests?
> > > > >
> > > > > All thoughts and/or best practices are greatly welcomed. For
> > reference
> > > > I'm
> > > > > using Guice as my dependency injection framework and
> > > > > GuiceWebApplicationFactory to inject components into pages.
> > > > >
> > > > > Thanks...
> > > > >
> > > > > Bill-
> > > > >
> > > > > [1]
> > > > >
> > > > >
> > > >
> > >
> >
> http://ci.apache.org/projects/wicket/apidocs/6.0.x/org/apache/wicket/markup/html/panel/Panel.html
> > > > >
> > > >
> > > >
> > > >
> > > > --
> > > > Martin Grigorov
> > > > jWeekend
> > > > Training, Consulting, Development
> > > > http://jWeekend.com <http://jweekend.com/>
> > > >
> > >
> >
> >
> >
> > --
> > Martin Grigorov
> > jWeekend
> > Training, Consulting, Development
> > http://jWeekend.com <http://jweekend.com/>
> >
>
--
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com <http://jweekend.com/>
Re: Pages, Panels, and Dependency Injection
Posted by Bill Speirs <bi...@gmail.com>.
OK... but how do I pass the current UserBean from one page to another? I
cannot bind it to Guice as I don't know what it is when I'm setting up my
Guice Module.
Currently I used setRedirectPage(new MyPage(currentUser)). From what I
understand you're saying I can switch to setRedirectPage(MyPage.class), but
then how do I set the UserBean "property" for that instance?
Should these types of values be passed via the session instead? So I simply
use setRedirectPage(MyPage.class), then inside that class get the current
user from the session?
The overall question is, what is the best practice for passing values to a
WebPage from another WebPage via setRedirectPage()? Should you create a new
instance of the page, or let Wicket do it via reflection? And if Wicket is
doing it, then how do I "inject" the value?
Thanks...
Bill-
On Tue, Jan 29, 2013 at 2:50 AM, Martin Grigorov <mg...@apache.org>wrote:
> Hi,
>
> You can do something like:
>
> class MyPage extends WebPage {
>
> @Inject private DAO dao;
>
> @Inject @Nullable private UserBean currentUser;
>
> ... // use dao or currentUser anywhere in your class
>
> }
>
> And Guice will try to inject these beans if they are defined in a Guice
> module.
> See https://code.google.com/p/google-guice/wiki/UseNullable
>
>
> On Tue, Jan 29, 2013 at 2:27 AM, Bill Speirs <bi...@gmail.com>
> wrote:
>
> > Still struggling with how to do this. Martin, I understand that most
> people
> > will simply call new PanelA and new PanelB inside their MyPage code
> instead
> > of trying to inject them. However, how does one setup a page that
> requires
> > some service (DAO for example) and that might optionally require a
> > UserBean.
> >
> > So without using DI, I'd simply have 2 constructors:
> >
> > MyPage(DAO myDao)
> >
> > MyPage(DAO myDao, UserBean currentUser)
> >
> > But I cannot create that second constructor as I don't have anything to
> > bind (using Guice terms) UserBean to.
> >
> > Am I required to make a factory for MyPage at that point? Is it bad/wrong
> > to pass the UserBean into the constructor via a response page:
> >
> > setResponsePage(new MyPage(new DAO(), currentUser));
> >
> > Thanks...
> >
> > Bill-
> >
> >
> > On Fri, Nov 30, 2012 at 3:35 AM, Martin Grigorov <mgrigorov@apache.org
> > >wrote:
> >
> > > Hi,
> > >
> > > Most of the time people inject services to their components.
> > >
> > > To test just components you can use WicketTester#startComponentInPage()
> > > methods.
> > >
> > >
> > > On Fri, Nov 30, 2012 at 3:18 AM, William Speirs <ws...@apache.org>
> > > wrote:
> > >
> > > > I'm having trouble understanding how to inject components into a page
> > so
> > > > that the page will be easy to unit test later. Say I have a page that
> > > > contains two panels. I can easily use constructor injection to inject
> > > these
> > > > panels into the page:
> > > >
> > > > class MyPage extends WebPage {
> > > > @Inject
> > > > public MyPage(PanelA a, PanelB b) { ... }
> > > > }
> > > >
> > > > The problem is that all Panels require an id during construction.[1]
> > How
> > > do
> > > > I supply the id to my Panels? I could simply construct every PanelA
> > with
> > > an
> > > > id of "panela" and every PanelB with an id of "panel", but that
> doesn't
> > > > seem very flexible. What do other people do in this situation? The
> hope
> > > > would be to pass mocked panels into the page during unit testing,
> > > > the separately test each panel.
> > > >
> > > > What if instead of a panel it was a button where the onSubmit method
> > must
> > > > be specified by overriding the method. How does one go about
> injecting
> > > such
> > > > a component so that it's still easy to test later in unit tests?
> > > >
> > > > All thoughts and/or best practices are greatly welcomed. For
> reference
> > > I'm
> > > > using Guice as my dependency injection framework and
> > > > GuiceWebApplicationFactory to inject components into pages.
> > > >
> > > > Thanks...
> > > >
> > > > Bill-
> > > >
> > > > [1]
> > > >
> > > >
> > >
> >
> http://ci.apache.org/projects/wicket/apidocs/6.0.x/org/apache/wicket/markup/html/panel/Panel.html
> > > >
> > >
> > >
> > >
> > > --
> > > Martin Grigorov
> > > jWeekend
> > > Training, Consulting, Development
> > > http://jWeekend.com <http://jweekend.com/>
> > >
> >
>
>
>
> --
> Martin Grigorov
> jWeekend
> Training, Consulting, Development
> http://jWeekend.com <http://jweekend.com/>
>
Re: Pages, Panels, and Dependency Injection
Posted by Martin Grigorov <mg...@apache.org>.
Hi,
You can do something like:
class MyPage extends WebPage {
@Inject private DAO dao;
@Inject @Nullable private UserBean currentUser;
... // use dao or currentUser anywhere in your class
}
And Guice will try to inject these beans if they are defined in a Guice
module.
See https://code.google.com/p/google-guice/wiki/UseNullable
On Tue, Jan 29, 2013 at 2:27 AM, Bill Speirs <bi...@gmail.com> wrote:
> Still struggling with how to do this. Martin, I understand that most people
> will simply call new PanelA and new PanelB inside their MyPage code instead
> of trying to inject them. However, how does one setup a page that requires
> some service (DAO for example) and that might optionally require a
> UserBean.
>
> So without using DI, I'd simply have 2 constructors:
>
> MyPage(DAO myDao)
>
> MyPage(DAO myDao, UserBean currentUser)
>
> But I cannot create that second constructor as I don't have anything to
> bind (using Guice terms) UserBean to.
>
> Am I required to make a factory for MyPage at that point? Is it bad/wrong
> to pass the UserBean into the constructor via a response page:
>
> setResponsePage(new MyPage(new DAO(), currentUser));
>
> Thanks...
>
> Bill-
>
>
> On Fri, Nov 30, 2012 at 3:35 AM, Martin Grigorov <mgrigorov@apache.org
> >wrote:
>
> > Hi,
> >
> > Most of the time people inject services to their components.
> >
> > To test just components you can use WicketTester#startComponentInPage()
> > methods.
> >
> >
> > On Fri, Nov 30, 2012 at 3:18 AM, William Speirs <ws...@apache.org>
> > wrote:
> >
> > > I'm having trouble understanding how to inject components into a page
> so
> > > that the page will be easy to unit test later. Say I have a page that
> > > contains two panels. I can easily use constructor injection to inject
> > these
> > > panels into the page:
> > >
> > > class MyPage extends WebPage {
> > > @Inject
> > > public MyPage(PanelA a, PanelB b) { ... }
> > > }
> > >
> > > The problem is that all Panels require an id during construction.[1]
> How
> > do
> > > I supply the id to my Panels? I could simply construct every PanelA
> with
> > an
> > > id of "panela" and every PanelB with an id of "panel", but that doesn't
> > > seem very flexible. What do other people do in this situation? The hope
> > > would be to pass mocked panels into the page during unit testing,
> > > the separately test each panel.
> > >
> > > What if instead of a panel it was a button where the onSubmit method
> must
> > > be specified by overriding the method. How does one go about injecting
> > such
> > > a component so that it's still easy to test later in unit tests?
> > >
> > > All thoughts and/or best practices are greatly welcomed. For reference
> > I'm
> > > using Guice as my dependency injection framework and
> > > GuiceWebApplicationFactory to inject components into pages.
> > >
> > > Thanks...
> > >
> > > Bill-
> > >
> > > [1]
> > >
> > >
> >
> http://ci.apache.org/projects/wicket/apidocs/6.0.x/org/apache/wicket/markup/html/panel/Panel.html
> > >
> >
> >
> >
> > --
> > Martin Grigorov
> > jWeekend
> > Training, Consulting, Development
> > http://jWeekend.com <http://jweekend.com/>
> >
>
--
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com <http://jweekend.com/>
Re: Pages, Panels, and Dependency Injection
Posted by Bill Speirs <bi...@gmail.com>.
Still struggling with how to do this. Martin, I understand that most people
will simply call new PanelA and new PanelB inside their MyPage code instead
of trying to inject them. However, how does one setup a page that requires
some service (DAO for example) and that might optionally require a UserBean.
So without using DI, I'd simply have 2 constructors:
MyPage(DAO myDao)
MyPage(DAO myDao, UserBean currentUser)
But I cannot create that second constructor as I don't have anything to
bind (using Guice terms) UserBean to.
Am I required to make a factory for MyPage at that point? Is it bad/wrong
to pass the UserBean into the constructor via a response page:
setResponsePage(new MyPage(new DAO(), currentUser));
Thanks...
Bill-
On Fri, Nov 30, 2012 at 3:35 AM, Martin Grigorov <mg...@apache.org>wrote:
> Hi,
>
> Most of the time people inject services to their components.
>
> To test just components you can use WicketTester#startComponentInPage()
> methods.
>
>
> On Fri, Nov 30, 2012 at 3:18 AM, William Speirs <ws...@apache.org>
> wrote:
>
> > I'm having trouble understanding how to inject components into a page so
> > that the page will be easy to unit test later. Say I have a page that
> > contains two panels. I can easily use constructor injection to inject
> these
> > panels into the page:
> >
> > class MyPage extends WebPage {
> > @Inject
> > public MyPage(PanelA a, PanelB b) { ... }
> > }
> >
> > The problem is that all Panels require an id during construction.[1] How
> do
> > I supply the id to my Panels? I could simply construct every PanelA with
> an
> > id of "panela" and every PanelB with an id of "panel", but that doesn't
> > seem very flexible. What do other people do in this situation? The hope
> > would be to pass mocked panels into the page during unit testing,
> > the separately test each panel.
> >
> > What if instead of a panel it was a button where the onSubmit method must
> > be specified by overriding the method. How does one go about injecting
> such
> > a component so that it's still easy to test later in unit tests?
> >
> > All thoughts and/or best practices are greatly welcomed. For reference
> I'm
> > using Guice as my dependency injection framework and
> > GuiceWebApplicationFactory to inject components into pages.
> >
> > Thanks...
> >
> > Bill-
> >
> > [1]
> >
> >
> http://ci.apache.org/projects/wicket/apidocs/6.0.x/org/apache/wicket/markup/html/panel/Panel.html
> >
>
>
>
> --
> Martin Grigorov
> jWeekend
> Training, Consulting, Development
> http://jWeekend.com <http://jweekend.com/>
>
Re: Pages, Panels, and Dependency Injection
Posted by Martin Grigorov <mg...@apache.org>.
Hi,
Most of the time people inject services to their components.
To test just components you can use WicketTester#startComponentInPage()
methods.
On Fri, Nov 30, 2012 at 3:18 AM, William Speirs <ws...@apache.org> wrote:
> I'm having trouble understanding how to inject components into a page so
> that the page will be easy to unit test later. Say I have a page that
> contains two panels. I can easily use constructor injection to inject these
> panels into the page:
>
> class MyPage extends WebPage {
> @Inject
> public MyPage(PanelA a, PanelB b) { ... }
> }
>
> The problem is that all Panels require an id during construction.[1] How do
> I supply the id to my Panels? I could simply construct every PanelA with an
> id of "panela" and every PanelB with an id of "panel", but that doesn't
> seem very flexible. What do other people do in this situation? The hope
> would be to pass mocked panels into the page during unit testing,
> the separately test each panel.
>
> What if instead of a panel it was a button where the onSubmit method must
> be specified by overriding the method. How does one go about injecting such
> a component so that it's still easy to test later in unit tests?
>
> All thoughts and/or best practices are greatly welcomed. For reference I'm
> using Guice as my dependency injection framework and
> GuiceWebApplicationFactory to inject components into pages.
>
> Thanks...
>
> Bill-
>
> [1]
>
> http://ci.apache.org/projects/wicket/apidocs/6.0.x/org/apache/wicket/markup/html/panel/Panel.html
>
--
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com <http://jweekend.com/>