powered by ASAP

What you will learn

In this short summary you will get an idea how to use a factory to create an IServiceProvider.

1. Basics

The IServiceProviderFactory shall help with the creation of IServiceProvider and is primarily used during the Engine Builder process. It is especially useful when services need to be registered at construction time of the IServiceProvider.

2. Signature

Before giving a short example on how the interface should be used, this section describes the signature of the IServiceProviderFactory interface, as well as an overview on which methods have to be implemented.

IServiceProvider newInstance(IContext context)

Creates a new instance of an IServiceProvider, can use the given IContext to help with the construction.

3. Usage

This section briefly outlines a few examples demonstrating how the implementing IServiceProviderFactory should be used.

3.1. Used for the Engine Builder

Suppose that we have written an IServiceProvider which only allows to register services at construction time. Of course you could use the constructor directly like shown in the usages here or you can pull the registration of services away from configuring the provider to use. The example below shows you a possible way to use the factory interface.

final ContextBuilder cb = new ContextBuilder(); (1)
// Setting up factory
final SimpleServiceProviderFactory factory = new SimpleServiceProviderFactory();
factory.addService(IPersistenceService.class,new SimplePersistenceService()); (2)
factory.addService(IMappingService.class, new MappingService()); (2)
// Finishing engine builder
cb.setServiceProviderFactory(factory); (3)
final IContext context = cb.build();
final IEngine engine = new EngineBuilder()...setContext(context).build();
1 The IServiceProviderFactory can be set to the ContextBuilder as IServiceProvider is part of the IContext.
2 We register two services to the factory which will be given to the IServiceProvider once it is constructed.
3 We give the Context Builder the IServiceProviderFactory to use to create a new IContext.

The resulting IContext will contain the IServiceProvider created by the factory with all the services preregistered already.

4. Default Implementations

  • de.asap.pak.core.simple.context.SimpleServiceProviderFactory : Prototype implementation of the interface, which returns the SimpleServiceProvider. Allows to preregister services in a map which is given to the provider at construction time.

  • de.asap.pak.extra.guice.GuiceServiceProviderFactory : Creates a GuiceServiceProvider which uses the functionalities of guice to register services. Allows registering whole guice modules before provider creation.