Skip to content

LazyModel

svenmeier edited this page Feb 19, 2013 · 10 revisions

A type-safe model implementation for Apache Wicket, supporting:

  • lazy evaluation of method invocations
  • arbitrary parameters
  • generics
  • collections
  • interfaces
    A a = ...;
    
    IModel<B> model = model(from(a).getB());

... or starting from a model:

    IModel<A> a = ...;
    
    IModel<List<String>> model = model(from(a).getB().getStrings());

... or as a LoadableDetachableModel:

    IModel<A> a = ...;
    
    IModel<String> model = model(from(a).getB().getStrings().get("key")).loadableDetachable();

Evaluations can be nested too:

    IModel<A> a = ...;
    IModel<D> d = ...;
    
    IModel<C> model = model(from(a1).getB().getCs().get( from(d).getIndex() ));

DataTables columns can be lazily evaluated too:

    column = new LazyColumn<A, B>(header, from(A.class).getB());

LazyModel generates proxies for each method invocation to be able to 'record' the evaluation. Method invocations cannot be evaluated lazily on primitive and final classes, since these cannot be subclassed. Let getF() a method returning an instance of a final class, then this works:

    model(from(a).getF());

... but accessing a property on the final class doesn't work:

    model(from(a).getF().getString()); // throws NullPointerException !!

All this proxying comes at a performance cost: depending on your usage LazyModel is 2 to 5 times slower than PropertyModel. If you're concerned about this you might want to cache lazy evaluations:

    private static final LazyModel<B> B = model(from(A.class).getB());

    IModel<A> a = ...;

    add(new TextField<B>("b", B.bind(a));

Caching model instances will increase performance for LazyModel to be slightly faster than PropertyModel.

Inspired by:

Using:

Clone this wiki locally