== Example

: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.
:page-partial:

For example:

[source,java]
----
@DomainObject(
    auditing=Auditing.ENABLED,
    autoCompleteRepository=CustomerRepository.class
    editing=Editing.ENABLED,
    updatedLifecycleEvent=Customer.UpdatedEvent.class

)
public class Customer {
    ...
}
----



[#view-models]
== View Models

The `@DomainObject(nature=VIEW_MODEL)` annotation, applied to a class, indicates 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).


For example:

[source,java]
----
@DomainObject(nature=VIEW_MODEL)
public class CustomerViewModel {
    public CustomerViewModel() {}
    public CustomerViewModel(String firstName, int lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    ...
}
----

Although there are several ways to instantiate a view model, we recommend that they are instantiated using an N-arg constructor that initializes all relevant state.
The xref:refguide:applib:index/services/registry/ServiceRegistry.adoc[ServiceRegistry] can then be used to inject dependencies into the view model.
For example:

[source,java]
----
Customer cust = ...
CustomerViewModel vm = factoryService.viewModel(
    new CustomerViewModel(cust.getFirstName(), cust.getLastName()));
----

[TIP]
====
See this xref:userguide:btb:hints-and-tips/view-model-instantiation.adoc[tip] for further discussion about instantiating view models.
====

View models must have a no-arg constructor; this is used internally by the framework for subsequent "recreation".

The view model's memento will be derived from the value of the view model object's properties.
Any properties annotated with xref:refguide:applib:index/annotation/Programmatic.adoc[@Programmatic] will be excluded from the memento.
Properties that are merely xref:refguide:applib:index/annotation/Property.adoc#hidden[hidden] _are_ included in the memento.

View models when defined using `@DomainObject(nature=VIEW_MODEL)` have some limitations:

* view models cannot hold collections other view models (simple properties _are_ supported, though)
* collections (of either view models or entities) are ignored.
* not every data type is supported,

However, these limitations do _not_ apply to xref:userguide:fun:view-models.adoc#jaxb[JAXB] view models.
If you are using view models heavily, you may wish to restrict yourself to just the JAXB flavour.


== Usage Notes


