FactoryService

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 or creates an instance of requiredType , with injection points resolved and any life-cycle callback processed.

2 get(Class)

Gets a Spring managed bean of requiredType .

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, initialized with given bookmark (if any) then resolves any injection points and calls post-construct (if any).

7 viewModel(Class)

Creates a new ViewModel instance, with injection points resolved, post-construct called and defaults applied.

8 viewModel(T)

Resolves injection points for and calls post-construct on given view-model instance.

9 create(Class)

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

Members

getOrCreate(Class)

Gets or creates an instance of requiredType , with injection points resolved and any life-cycle callback processed.

Maps onto one of the specialized factory methods #get(Class) or #create(Class) based on the type’s meta-data.

get(Class)

Gets a Spring managed bean of requiredType .

detachedEntity(Class)

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

The entity will not yet be persisted, in other words: its not yet known to the persistence layer.

detachedEntity(T)

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

The entity will not yet be persisted, in other words: its not yet known to the persistence layer.

mixin(Class, Object)

Creates a new Mixin instance, with injection points resolved.

viewModel(Class, Bookmark)

Creates a new ViewModel instance, initialized with given bookmark (if any) then resolves any injection points and calls post-construct (if any).

viewModel(Class)

Creates a new ViewModel instance, with injection points resolved, post-construct called and defaults applied.

viewModel(T)

Resolves injection points for and calls post-construct on given view-model instance.

create(Class)

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

Implementation

The core framework provides a default implementation of this service (o.a.c.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.