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


== Usage

The benefits of using this method (instead of simply using the Java `new` keyword) are:

* any services will be injected into the object immediately (otherwise they will not be injected until the framework becomes aware of the object, typically when it is persisted through the xref:refguide:applib:index/services/repository/RepositoryService.adoc[RepositoryService]

* the default value for any properties (usually as specified by `defaultXxx()` supporting methods) or from the value type itself will be set and the `created()` callback will be called.

An alternative idiom is to just `new` up the object and then use xref:refguide:applib:index/services/inject/ServiceInjector.adoc[ServiceInjector] domain service can be used to inject services into the domain object.
Note though that no default values will be set on the created object.


== Example

For example:

[source,java]
----
Customer cust = factoryService.detachedEntity(Customer.class);
cust.setFirstName("Freddie");
cust.setLastName("Mercury");
repositoryService.persist(cust);
----


== Related Services

See also:

* The xref:refguide:applib:index/services/repository/RepositoryService.adoc[RepositoryService] is often used in conjunction with the `FactoryService`, to persist domain objects after they have been instantiated and populated.
