PAK Core – IContext
What you will learn
In this short summary you will get an idea what the IContext
is for and how to create your own.
1. Basics
This interface is a central part of the PAK Core. It basically has the ability to access all registered services (through the IServiceProvider
) and can store and find values from the IPersistenceService
.
It gets passed through all components used during the engine execution and allows them to use its functions.
2. Signature
Before giving a short example on how the interface should be used, this section describes the signature of the IContext
interface.
|
Tries to find the object mapped to the key from the managed |
|
Stores the given object into the managed |
|
Returns the currently managed |
3. Usage
This section briefly outlines a few examples demonstrating how the implementing IContext
should be used.
3.1. As Part of the Engine Builder
The EngineBuilder
requires an IContext
to build an engine. You can either provide your own implementation or use the ContextBuilder
to build a default one.
...;
final IContext context = new YourContext(); (1)
// Finishing the builder
final IEngine engine = new EngineBuilder()...setContext(context).build(); (2)
...;
1 | Create your own context or use the builder. |
2 | Set the context to the engine builder and create a default engine from that. |
3.2. In the components during engine execution.
The most common usage of IContext
is inside the components during the engine execution. Each component gets access to the context and with it access to its functionalities.
With that you can access any service or write into the IPersistenceService
from any component.
In the example below we wrote a small component which holds the IContext
and shall look into the persistence to find a value and write into the persistence if it is a json.
class EngineComponent{
private final IContext context; (1)
public void makeEngineStuff(final String key){
final Object obj = this.context.find(key); (2)
final IDataTransformer dt = this.context.getServiceProvider.find(IDataTransformer.class); (3)
this.context.store("isJson"+key,dt.isJson(obj)); (4)
}
}
1 | The context is preset through the constructor in this case. |
2 | You can use the context to find objects in the managed IPersistenceService |
3 | You can use the context to fetch the IServiceProvider and their registered services. |
4 | You can use the context to store an object into the IPersistenceService |
4. Default Implementations
-
Currently, only one default implementation is available. You can get an instance of it by using the
ContextBuilder
. It uses theIPersistenceService
found from theIServiceProvider
given.