views:

64

answers:

1

How do I convert the following example to use one of the wicket/IOC integration packages?

I want to learn and understand the DI/IOC pattern. I have read a lot about it, but I've found it a little hard to grasp without digging into a code example. I have an example of a complete, working Wicket panel, which I think I want to convert to using DI.

The panel is basically a web UI editor for a list. It takes an IPanelFactory as a constructor argument. The constructor creates a list (DefaultRefreshingView), and the panel factory knows how to create a ViewPanel for each row. The panelFactory also knows how to create an add form, for introducing a new item into the list.

I will cut out a lot of code to get to the heart of what I think is relevant:

public interface IPanelFactory<T extends DomainEntity, U extends DomainEntity>
        extends IDetachable {
    IAddItemPanel<T, U> newAddItemPanel(String id);

    Component newEditPanel(ViewPanel.ViewPanelParameters<T> parms);

    Component newListPanel(String id, IModel<Collection<T>> listModel);
}

public class ListEditorPanel2<T extends DomainEntity, U extends DomainEntity>
        extends Panel implements IHeaderContributor {
    private static final long serialVersionUID = 1L;

    private final IPanelFactory<T, U> panelFactory;

    public ListEditorPanel2(String id, IModel<Collection<T>> listModel,
            final IPanelFactory<T, U> panelFactory) {
        super(id, listModel);
        this.panelFactory = panelFactory;

        final AjaxCell listCell = new AjaxCell("listCell");
        DefaultRefreshingView<T> itemList = new DefaultRefreshingView<T>(
                "itemList", listModel) {
            @Override
            protected void populateItem(final Item<T> item) {
                Component editPanel = ListEditorPanel2.this.panelFactory
                        .newEditPanel(new ViewPanelParameters<T>("viewPanel",
                                item.getModel(), new EditPanelCallback(item)));
                item.add(editPanel);
            }
        };
        listCell.add(itemList);
        add(listCell);

        final IAddItemPanel<T, U> addItemPanel = panelFactory
                .newAddItemPanel("addItemForm");
        add((Component) addItemPanel);
        Form<T> form = addItemPanel.getForm();
    }
}

Each implementation of IPanelFactory provides different components for the View and Add sections. There can be multiple instances of ListEditorPanel2 that use different factory implementations in the same page hierarchy.

My thinking is that I want IPanelFactory to be an injected dependency. So...

  • Is this a good use case for DI/IOC, or does it not make sense to use it here?
  • How would I do this "by hand" without using an IOC container? (Am I doing it already?)
  • Which wicket-IOC integration should I use? I am using wicket 1.4.8 at present. I was going to try wicket-spring-annot, but it looks like that project is out of date.
  • How do I set up the IOC container to do this for my wicket components?
A: 

Hi,

I dont quite understand your use case but i will try to answer your question anyway. Wicket has great support for the 2 most popular DI containers namely Spring and Guice. Personally i prefer using guice since it i find it less bloated and easier to use. You need to understand how guice works and how guice modules are created. To intergrate you wicket application with guice, you do something like below on your wicket application init method:

Injector injector = Guice.createInjector(new   AppModule(),PersistenceService.usingJpa().across(UnitOfWork.REQUEST).buildModule());
addComponentInstantiationListener(new GuiceComponentInjector(this, injector));

Here am instantiating a guice injector with 2 modules, namely appModule and Persistence module (ignore the persistenceModule details . am using warp persist for transactions)

In a guice module, you bind all the components that you would like to be injecting. I mostly bind my Daos and my services. I can then inject the same to my panels, pages and any other wicket component. I can also bind panels and other custom components so that i can inject them to other components as neccessary. I have however not found a situation where i will need to bind and inject a user interface component.

I hope this helps.

Kind regards. Josh

joshua
Thanks! I have figured out a wicket-spring config that lets me inject DAOs. I am playing around with that a bit, I will probably do a little reading up on Guice and see how it compares.
RMorrisey