A service factory is used to create service instances for a specific context. The service factory needs to be registered in a central service factory registry. Consumers of services require the service factory to create service instances.
The service factory base class can be used in a generic way to act as a factory for any service:
sap.ui.require([ "sap/ui/core/service/ServiceFactoryRegistry", "sap/ui/core/service/ServiceFactory", "my/Service" ], function(ServiceFactoryRegistry, ServiceFactory, MyService) { ServiceFactoryRegistry.register(new ServiceFactory(MService)); });
Additionally a concrete service factory can be implemented by extending the service factory base class if additional functionality is needed when creating new instances for a specific context:
sap.ui.define("my/ServiceFactory", [ "sap/ui/core/service/ServiceFactoryRegistry", "sap/ui/core/service/ServiceFactory", "my/Service" ], function(ServiceFactoryRegistry, ServiceFactory, MyService) { return ServiceFactory.extend("my.ServiceFactory", { createInstance: function(oServiceContext) { return Promise.resolve(new MyService(oServiceContext)); } }); });
Another option for the usage of the service factory is to provide a structured object with information about the service which will create an anonymous service internally:
sap.ui.define("my/ServiceFactory", [ "sap/ui/core/service/ServiceFactoryRegistry", "sap/ui/core/service/ServiceFactory", "my/Service" ], function(ServiceFactoryRegistry, ServiceFactory, MyService) { return new ServiceFactory({ init: function() { ... }, exit: function() { ... }, doSomething: function() { ... } }); });
As createInstance
returns a Promise
e.g. the service module can also be loaded asynchronously and resolve once the module has been loaded and instantiated.
Creates a service factory.
Accepts an object literal mSettings
that defines initial property values, aggregated and associated objects as well as event handlers. See sap.ui.base.ManagedObject#constructor for a general description of the syntax of the settings object.
This class does not have its own settings, but all settings applicable to the base type sap.ui.base.Object can be used.
new sap.ui.core.service.ServiceFactory(vService?)
Param | Type | Default Value | Description |
---|---|---|---|
vService? | function object | A constructor function of a service or a structured object with information about the service which creates an anonymous service. |
Method | Description |
---|---|
createInstance |
Creates a new instance of a service. When used as a generic service factory by providing a service constructor function it will create a new service instance otherwise the function will fail. For custom service factories this function has to be overridden and should return a |
destroy |
Lifecycle method to destroy the service factory instance. |
sap.ui.core.service.ServiceFactory.extend |
Creates a new subclass of class sap.ui.core.service.ServiceFactory with name
|
sap.ui.core.service.ServiceFactory.getMetadata |
Returns a metadata object for class sap.ui.core.service.ServiceFactory. |
Creates a new instance of a service. When used as a generic service factory by providing a service constructor function it will create a new service instance otherwise the function will fail. For custom service factories this function has to be overridden and should return a Promise
.
Param | Type | DefaultValue | Description |
---|---|---|---|
oServiceContext | object |
Context for which the service is created |
|
scopeObject | object |
Object that is in scope (e.g. component instance) |
|
scopeType | string |
Type of object that is in scope (e.g. component, ...) |
|
settings | string |
The service settings |
Creates a new subclass of class sap.ui.core.service.ServiceFactory with name sClassName
and enriches it with the information contained in oClassInfo
.
oClassInfo
might contain the same kind of information as described in sap.ui.base.Object.extend.
Param | Type | DefaultValue | Description |
---|---|---|---|
sClassName | string |
Name of the class being created |
|
oClassInfo | object |
Object literal with information about the class |
|
FNMetaImpl | function |
Constructor function for the metadata object; if not given, it defaults to the metadata implementation used by this class |