You never know whether the given model is ready for its #getObject() to be called. For example ResourceModel needs its owning component to be attached to a page to function properly.

>"we have to extract it anyway, just somewhere else."

I'd phrase it "we have to extract it anyway, just somewhat later".

But you don't have to be overzealous about this. If it works for you, it's fine.

Best regards
Sven



On 04/10/2013 05:54 PM, Patrick Davids wrote:
Thank you. :-)
Interesting concept...

Unfortunately still have the question:
Why is it recommended not to extract a given model (1), if it is just
needed to create another empty model (2) via a servicecall in a
constructor (I do not hold a reference to model (1) as member in the
page or component)?

And why should I explicitly not do an extract in a constructor?

I'm asking because our discussion always ends into "we have to extract
it anyway, just somewhere else."

regards
Patrick


Am 10.04.2013 17:04, schrieb Sven Meier:
public abstract class CreateAndSetIfNull<T> extends
AbstractReadOnlyModel<T> {

    private T model;

    public CreateAndSetIfNull(IModel<T> model) {
      this.model = model;
    }

    public T getObject() {
      T t = model.getObject();
      if (t == null) {
        t = onCreate();
        model.setObject(t);
      }
      return t;
    }

    public void detach() {
      model.detach();
    }

    protected abstract T onCreate();
}


On 04/10/2013 04:47 PM, Patrick Davids wrote:
Hi Sven,
and who calls onCreate() and when is it called?

regards
Patrick

Am 10.04.2013 16:20, schrieb Sven Meier:
extracting model [objects] in constructors of components.
Best practices says: DONT
How about creating the address lazily?

     IModel<Address> address = new CreateAndSetIfNull(new
PropertyModel<Address>(person, "address")) {
       protected Address onCreate() {
          return addressService.newAddress();
       }
     };

Regards
Sven



On 04/10/2013 03:56 PM, Patrick Davids wrote:
Hi together,

I'm in a discussion with my collegues about extracting models in
constructors of components.

Best practices says: DONT, because of accidently holding references of
extrated objects / out of mems / etc.

But, if we want to have a component containing a form is responsable to
create its own emtpy model, if not created yet.

Is it ok, to extract a model "temporary"?

here an axample:

A person has an address.
An extra component panel with form fields for creating/editing
addresses
exists (nested forms pattern).
The AdressComponent gets a model of person.

In case of: person does not have an address, yet.

Is it allowed to extract the person model, to make a service call
somewhere to let create an empty address object for this particular
person and then setting the model object on my own?

We would like to have the knowledge about creation and saving addresses
put toghether in the AddressComponent instead of having the knowledge
about saving inside (onSubmit() of form), and the knowledge about
creation outside the AddressComponent.

kind regards
Patrick
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to