FactoryService (interface)

Collects together methods for instantiating domain objects, also injecting them with any domain services and calling lifecycle methods if defined.

API

FactoryService.java
interface FactoryService {
  T getOrCreate(Class<T> requiredType)     (1)
  T get(Class<T> requiredType)     (2)
  T detachedEntity(Class<T> domainClass)     (3)
  T detachedEntity(T entity)     (4)
  T mixin(Class<T> mixinClass, Object mixedIn)     (5)
  T viewModel(Class<T> viewModelClass, String mementoStr)     (6)
  T viewModel(Class<T> viewModelClass)     (7)
  T viewModel(T viewModel)     (8)
  T create(Class<T> domainClass)     (9)
  T instantiate(Class<T> domainClass)     (10)
}
1 getOrCreate(Class)

Gets an instance (possibly shared or independent) of the specified type, with injection points resolved and any life-cycle callback processed.

2 get(Class)

Gets an instance (possibly shared or independent) of the specified requiredType , with injection points resolved and any life-cycle callback processed.

3 detachedEntity(Class)

Creates a new detached entity instance, with injection points resolved and defaults applied.

4 detachedEntity(T)

Creates a new detached entity instance, with injection points resolved.

5 mixin(Class, Object)

Creates a new Mixin instance, with injection points resolved.

6 viewModel(Class, String)

Creates a new ViewModel instance, with injection points resolved, and initialized according to the given mementoStr

7 viewModel(Class)

Creates a new ViewModel instance, with injection points resolved and defaults applied.

8 viewModel(T)

Resolves injection points for given ViewModel instance.

9 create(Class)

Creates a new instance of the specified class, with injection points resolved and defaults applied.

10 instantiate(Class)

deprecated:

with semantic changes since 2.0 previous behavior is no longer guaranteed, instead consider use of @ #detachedEntity(Class) or #getOrCreate(Class) if applicable

Creates a new instance of the specified class, but does not persist it.

Members

getOrCreate(Class)

Gets an instance (possibly shared or independent) of the specified type, with injection points resolved and any life-cycle callback processed.

get(Class)

Gets an instance (possibly shared or independent) of the specified requiredType , with injection points resolved and any life-cycle callback processed.

detachedEntity(Class)

Creates a new detached entity instance, with injection points resolved and defaults applied.

The entity will be detacted, in other words not yet persisted.

detachedEntity(T)

Creates a new detached entity instance, with injection points resolved.

mixin(Class, Object)

Creates a new Mixin instance, with injection points resolved.

viewModel(Class, String)

Creates a new ViewModel instance, with injection points resolved, and initialized according to the given mementoStr

viewModel(Class)

Creates a new ViewModel instance, with injection points resolved and defaults applied.

viewModel(T)

Resolves injection points for given ViewModel instance.

create(Class)

Creates a new instance of the specified class, with injection points resolved and defaults applied.

instantiate(Class)

deprecated:

with semantic changes since 2.0 previous behavior is no longer guaranteed, instead consider use of @ #detachedEntity(Class) or #getOrCreate(Class) if applicable

Creates a new instance of the specified class, but does not persist it.

It is recommended that the object be initially instantiated using this method, though the framework will also handle the case when the object is simply new() ed up. The benefits of using #instantiate(Class) 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 RepositoryService#persist(Object) persist ed

  • the default value for any properties (usually as specified by default Xxx () supporting methods) will (since 2.0) be used

  • the created() callback will not be called.

The corollary is: if your code never uses default Xxx () supporting methods or the created() callback, then you can alternatively just new() up the object rather than call this method.

Implementation

The core framework provides a default implementation of this service (o.a.i.core.metamodel.services.factory.FactoryServiceDefault).

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

Customer cust = factoryService.detachedEntity(Customer.class);
cust.setFirstName("Freddie");
cust.setLastName("Mercury");
repositoryService.persist(cust);

See also:

  • The RepositoryService is often used in conjunction with the FactoryService, to persist domain objects after they have been instantiated and populated.