views:

662

answers:

1

I've got a page with a form in Wicket where the form requires a collaborator to get its job done. The collaborator is injected (for which I'm using Guice) and looks something like:

public class RegistrationPage extends WebPage {
    @Inject
    public RegistrationPage(RegistrationService service) {
        this.service = service;
        add(new RegistrationForm());            
    }

    private class RegistrationForm extends Form {
        public RegistrationForm() {
            // setup
        }

        protected void onSubmit() {
           service.doSomething();
        }
    }
}

I don't like the idea that the RegistrationService is injected into the RegistrationPage when it's just the RegistrationForm that needs it. I could change the RegistrationForm to receive the RegistrationService:

public RegistrationForm(RegistrationService service) {
    this.service = service;
}

and remove the field from the RegistrationPage, but the RegistrationPage is still being used to do the pass-through.

I guess what I'm asking is what the best-practise is for doing this? Is this ok to do, or would it perhaps be better to inject the RegistrationForm itself into the Page:

   public class RegistrationPage extends WebPage {
        @Inject
        public RegistrationPage(RegistrationForm form) {
            add(form);
        }
   }

   ---

   private class RegistrationForm extends Form {
        private RegistrationService service;

        @Inject
        public RegistrationForm(RegistrationService service) {
            this.service = service;
        }

        protected void onSubmit() {
           service.doSomething();
        }
    }

I'd prefer this as I'd like to have the RegistrationForm in a separate class/file. I'm quite new to Wicket so unsure of what the norm is - can someone show me the guiding light? :)

+2  A: 

the basic paradigm with wicket+ioc is: most dependencies should be injected via setter injection. constructor injection is impossible for WebPages.

components/panels/forms/pages should only be on the recieving end.

so, inject the dependency to RegistrationService happily into the RegistrationForm , then create it in the RegistrationPage with add(new RegistrationForm());

wicket has IComponentInstantiationListener - one of them is guice. they get notified during the constructor of each component/webpage. so your RegistrationForm will have its dependencies injected before any part of your code can execute.

the way i would do it: (of course RegistrationForm can be in another file)

public class RegistrationPage extends WebPage {

@Inject
public RegistrationPage() {
    add(new RegistrationForm());            
}

---
private static class RegistrationForm extends Form {
   RegistrationService service;

      @Inject
     public void setRegistrationService (RegistrationService  service){
     this.service = service;
        }
    public RegistrationForm() {
        // setup
    }

    protected void onSubmit() {
       service.doSomething();
    }
}
}

if you decide to put the RegistrationForm inside the Page as inner class, remember to declare it static! you will most likely not need any references to the enclosing class.

Andreas Petersson
Nice one Andreas, that's just what I was after. I didn't realise about the IComponentInstantiationListener.
MrWiggles