Fight the Future

Java言語とJVM、そしてJavaエコシステム全般にまつわること

Introducing Apache Wicketの超意訳(4)

http://www.theserverside.com/tt/articles/article.tss?l=IntroducingApacheWicket

Because of Wicket's state management
(which you can find a great description of here: http://wicket.apache.org/introduction.html),
IModels are stored in the user's session,
which means the model object contained by the IModel is also stored in the user's session.
This means our string "Chauncey Billups" takes up a bit of session storage space.
For small objects this isn't a problem, but it can quickly become a problem for large object graphs.
This is where detachable models come in.

Wicketの状態管理のために、IModelはユーザのセッションに保存します。
(詳しくは次に記述しています。http://wicket.apache.org/introduction.html)
これが意味することは、IModelが含むモデルオブジェクトもユーザのセッションに保存するということです。
これは、文字列"Chauncey Billups"はセッションのストレージスペースを少し占めるということを意味します。
小さいオブジェクトなら問題ありませんが、大きなオブジェクトグラフならすぐに問題になります。
これが分離可能なモデルの必要なところです。

Detachable models detach the model object from the model when the user's request completes,
ensuring no space is used by the model object when the models and components are stored.
Detachable models typically keep the minimal amount of information needed to reattach an object.
If you're using a persistence solution, this might be the primary key value of the object to load.
Let's rework our earlier example to use a common detachable model, LoadableDetachableModel:

分離可能なモデルは、ユーザのリクエストが完了したときに、
モデルとコンポーネントを保存するタイミングでモデルオブジェクトがスペースを使っていないことを保証して、
モデルオブジェクトからモデルを分離します。
分離可能なモデルは通常オブジェクトに再結合するために必要な情報を最小限しか保持しません。
もし永続化ソリューションを使っているなら、これはロードするオブジェクトの主キーの値かもしれません。
先ほどの例に手を加えてみましょう。共通の分離可能なモデルであるLoadableDetachableModelを使います。

IModel nameModel = new LoadableDetachableModel() {
    public Object load() {
        return playerService.getPlayerName();
    }
}

The LoadableDetachableModel implements IModel, so its usage is the same as any other model class.
Let's take a look at the getObject() method for LoadableDetachableModel to see how it all comes together:

LoadableDetachableModelはIModelを実装するので、は他のモデルクラスと同じ使い方です。
どのように連携させるのかLoadableDetachableModelのgetObject()メソッドを見てみましょう。

public Object getObject() {
    if (!attached) {
        attached = true;
        transientModelObject = load();

        if (log.isDebugEnabled()) {
           log.debug("...");
        }

        onAttach();
    }

    return transientModelObject;
}

The getObject() method is actually very simple.
If you're using Hibernate or some other persistence solution,
you'll use LoadableDetachableModels frequently to ensure your objects aren't taking up session space needlessly.

getObject()メソッドは実際のところとても簡単です。
もしHibernateやその他の永続化ソリューションを使っていないなら、
セッションのスペースを必要以上に占めないことを保証するためにLoadableDetachableModelを頻繁に使用するでしょう。