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, Bookmark bookmark)     (6)
  T viewModel(Class<T> viewModelClass)     (7)
  T viewModel(T viewModel)     (8)
  T create(Class<T> domainClass)     (9)
}
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, Bookmark)

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

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.

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, Bookmark)

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

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.

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.