Class
caplin.core

ServiceRegistry

The ServiceRegistry is used to allow a given application access to application services.

Services are typically registered or requested using an alias name, but older applications may still register and request using interfaces, which is also still supported. Applications that use aliases don't normally need to manually register services as these are created lazilly upon first request, but will still need to manually register services that can't be created using a zero-arg constructor.

The ServiceRegistry is initialized as follows:

  1. The application invokes caplin.core.ServiceRegistry.initializeServices which causes all delayed readiness services to be created.
  2. Once caplin.core.ServiceRegistry.initializeServices has finished (once one of the call-backs fire), the application should then register any services that can't be created lazilly using zero-arg constructors.
  3. The application can now start doing it's proper work.

Because blades aren't allowed to depend directly on classes in other blades, interface definitions are instead created for particular pieces of functionality, and blades can choose to register themselves as being providers of that functionality. The ServiceRegistry and the caplin.core.event.EventHub are both useful in this regard:

  • Many-To-One dependencies are resolved by having a single service instance available via the ServiceRegistry.
  • Many-To-Many dependencies are resolved by having zero or more classes register with the caplin.core.event.EventHub.

Constructor Summary

Attributes Name and Description
caplin.core.ServiceRegistry()

The ServiceRegistry is a static class and does not need to be constructed.

Method Summary

Attributes Name and Description
<static> void clear()

Resets the ServiceRegistry back to its initial state.

<static> void deregisterService(String sIdentifier)

De-register a service that is currently registered in the ServiceRegistry.

<static> Object getService(String sIdentifier)

Retrieve the service linked to the identifier within the application.

<static> void initializeServices(Function fSuccessCallback, Function fErrorCallback)

Registering with this method will notify when all services that implement caplin.core.DelayedReadinessService are ready to use.

<static> boolean isServiceRegistered(String sIdentifier)

Determine whether a service has been registered for a given identifier.

<static> void registerService(String sIdentifier, Object oServiceInstance)

Register an object that will be responsible for implementing the given interface within the application.

Constructor Detail

caplin.core.ServiceRegistry()

The ServiceRegistry is a static class and does not need to be constructed.

Method Detail

<static> void clear()

Resets the ServiceRegistry back to its initial state.

This method isn't normally called within an application, but is called automatically before each test is run.

See
caplin.core.AliasRegistry.clear

<static> void deregisterService(String sIdentifier)

De-register a service that is currently registered in the ServiceRegistry.

Parameters
String sIdentifier The alias or interface name used to uniquely identify the service.

<static> Object getService(String sIdentifier)

Retrieve the service linked to the identifier within the application. The identifier could be a service alias or a service interface.

Parameters
String sIdentifier The alias or interface name used to uniquely identify the service.
Throws
caplin.core.Error
If no service could be found for the given identifier.

<static> void initializeServices(Function fSuccessCallback, Function fErrorCallback)

Registering with this method will notify when all services that implement caplin.core.DelayedReadinessService are ready to use.

Parameters
Function fSuccessCallback the function that is called back when all services implementing caplin.core.DelayedReadinessService are ready for use.
Function fErrorCallback the function that is called back if there is an error during initialization of any of the services, a string reason will be passed in as argument.
Throws
caplin.core.Error.ILLEGAL_STATE
if this method is called more than once.

<static> boolean isServiceRegistered(String sIdentifier)

Determine whether a service has been registered for a given identifier.

Parameters
String sIdentifier The alias or interface name used to uniquely identify the service.

<static> void registerService(String sIdentifier, Object oServiceInstance)

Register an object that will be responsible for implementing the given interface within the application. Note: If the application is using aliases to access services this method is not relevant.

Parameters
String sIdentifier The alias or interface name used to uniquely identify the service.
Object oServiceInstance The object responsible for providing the service.
Throws
caplin.core.Error
If a service has already been registered for the given interface or if no instance object is provided.