<commit_before>[[_rgant-ViewModel]]
= `@ViewModel`
:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
:_basedir: ../../
:_imagesdir: images/



The `@ViewModel` annotation, applied to a class, is the simplest way to indicate that the class is a view model.

View models are not persisted to the database, instead their state is encoded within their identity (ultimately
represented in the URL). As such, view models are immutable.

For example:

[source,java]
----
@ViewModel
public class MyViewModel {
    public MyViewModel() {}   // <1>
    ...
}
----
<1> must have a no-arg constructor for subsequent "recreation" by the framework.


To instantiate a view model, you can either instantiate directly using its constructor, or indirectly using `DomainObjectContainer#newTransientInstance()`.  If you use the former, also programmatically call `DomainObjectContainer#injectServicesInto(...)` to ensure that any dependencies are injected into the service.


[TIP]
====
Note that there is a `DomainObjectContainer#newViewModelInstance(.)`; this is for view models that implement `ViewModel` interface and can be safely ignored.
====


The view model's memento will be derived from the value of the view model object's properties. Any xref:../rgant/rgant.adoc#_rgant-Property_notPersisted[`@Property#notPersisted()`] properties will be excluded from the memento, as will any xref:../rgant/rgant.adoc#_rgant-Programmatic[`@Programmatic`] properties. Properties that are merely xref:../rgant/rgant.adoc#_rgant-Property_hidden[hidden] _are_ included in the memento.

Only properties supported by the configured xref:../rgsvc/rgsvc.adoc#_rgsvc_integration-api_MementoService[`MementoService`] can be used. The default implementation supports all the value types and persisted entities.

(As of 1.8.0) there are some limitations:
* view models cannot hold collections other view models (simple properties _are_ supported, though)
* collections (of either view models or entities) are ignored.


[WARNING]
====
The `@ViewModel` does not allow the objectType to be specified, meaning that it is incompatible with the metamodel validation check ennabled by the xref:../rgcfg/rgcfg.adoc#__rgcfg_configuring-core_metamodel-validation[`explicitObjectType`] configuration property.

Instead, use xref:../rgant/rgant.adoc#_rgant_DomainObject_nature[`@DomainObject#nature()`] with `Nature.VIEW_MODEL`, and specify xref:../rgant/rgant.adoc#_rgant_DomainObject_objectType[`@DomainObject#objectType()`].
====<commit_msg>ISIS-1712: Added link to the new 'View Model Instantiation' section to the ViewModel annotation guide<commit_after>[[_rgant-ViewModel]]
= `@ViewModel`
:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
:_basedir: ../../
:_imagesdir: images/



The `@ViewModel` annotation, applied to a class, is the simplest way to indicate that the class is a view model.

View models are not persisted to the database, instead their state is encoded within their identity (ultimately
represented in the URL). As such, view models are immutable.

For example:

[source,java]
----
@ViewModel
public class MyViewModel {
    public MyViewModel() {}   // <1>
    ...
}
----
<1> must have a no-arg constructor for subsequent "recreation" by the framework.

View models should be instantiated using their constructor, using xref:../rgsvc/rgsvc.adoc#_rgsvc_metadata-api_ServiceRegistry[`ServiceRegistry`] to inject dependencies into the view model as per xref:../ugbtb/ugbtb.adoc#_ugbtb_hints-and-tips_view-model-instantiation[View Model Instantiation].

The view model's memento will be derived from the value of the view model object's properties. Any xref:../rgant/rgant.adoc#_rgant-Property_notPersisted[`@Property#notPersisted()`] properties will be excluded from the memento, as will any xref:../rgant/rgant.adoc#_rgant-Programmatic[`@Programmatic`] properties. Properties that are merely xref:../rgant/rgant.adoc#_rgant-Property_hidden[hidden] _are_ included in the memento.

Only properties supported by the configured xref:../rgsvc/rgsvc.adoc#_rgsvc_integration-api_MementoService[`MementoService`] can be used. The default implementation supports all the value types and persisted entities.

(As of 1.8.0) there are some limitations:
* view models cannot hold collections other view models (simple properties _are_ supported, though)
* collections (of either view models or entities) are ignored.


[WARNING]
====
The `@ViewModel` does not allow the objectType to be specified, meaning that it is incompatible with the metamodel validation check ennabled by the xref:../rgcfg/rgcfg.adoc#__rgcfg_configuring-core_metamodel-validation[`explicitObjectType`] configuration property.

Instead, use xref:../rgant/rgant.adoc#_rgant_DomainObject_nature[`@DomainObject#nature()`] with `Nature.VIEW_MODEL`, and specify xref:../rgant/rgant.adoc#_rgant_DomainObject_objectType[`@DomainObject#objectType()`].
====
{'fraction_non_alphanumeric': 0.08546059933407325, 'fraction_numerical': 0.003488187727921357, 'mean_word_length': 5.2393669634025715, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4231871', 'n_tokens_mistral': 1749, 'n_tokens_neox': 1648, 'n_words': 719}