On Fri, Apr 15, 2011 at 8:43 PM, Marek Šabo wrote:
> Hi all,
>
> I would like to ask for some examples where use of AROM comes in handy. We
> had a sort of dispute in one project whether it is better to provide "static
> text, no i18n" labels with Model.of(Object o) or
>
>final Object o;
>new AbstractReadOnlyModel(){
>
>getObject() {
>return o;
>};
> }
>
Functionally, your two models are equivalent. They both have a private
instance variable that contains the object that will be returned by
getObject.
Could someone who uses them in fitting scenarion provide some advice? It
> shows up in our code more and more often and I'm not convinced of that being
> right. That argument was augmented by ResourceModel being sublclass of AROM.
>
I see people use AROM all over the place for all the wrong reasons. Doing
things like the following are typically *bad*:
// this should typically be a LoadableDetachableModel:
new AbstractReadOnlyModel { public Foo getObject() { return
database.getFoo(); } }
// this is almost always from a wrong understanding of what models are:
(in a ListView):
newItem(ListItem item) {
final Foo obj = item.getModelObject();
add(new Label("label", new AbstractReadOnlyModel() {
public String getObject() {
return obj.getSomeString();
}
});
// see guys! I'm awesome! I used a MODEL
}
AbstractReadOnly models are good to use when your use meets all the
following criteria:
1 - you're using it for a read-only component (label, etc)
2 - whatever you must store in it is okay to be serialized (remember that if
your getObject method references a final variable declared outside of the
AROM, that entire object will be serialized into your AROM as a private
field by the compiler)
3 - whatever you are returning is cheap to retrieve (if it's not, like you
must get it from the database, you will typically want a
LoadableDetachableModel)
Acceptable use:
add(new Label("username", new AbstractReadOnlyModel() {
public String getObject() {
return getSession().getUser().getUsername();
}
});
In this example:
1 - I'm using it on a label
2 - I'm not stroring any state in my AROM, because I have no fields in the
anonymous class, and I'm not accessing a final variable declared outside the
class
3 - Assuming the user is kept in session memory, I'm not doing a DB lookup
or other expensive operation to get that string in my AROM
Of course, I could have also done this:
add(new Label("username", new PropertyModel(this,
"session.user.username")));
Honestly, I don't use AROM a whole lot. LDM and PropertyModel are generally
the most common. But I'm sure Igor will prove me wrong :)
--
Jeremy Thomerson
http://wickettraining.com
*Need a CMS for Wicket? Use Brix! http://brixcms.org*