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.spe...@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 ><mgrigo...@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 <wspe...@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-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org