Context Class

The net.democritus.sys.Context class is a means to pass contextual information with every method call. This class was added in release 201712 to enrich the ParameterContext object.

ParameterContext

All parameters in the expanded code are wrapped with a ParameterContext-object. This allows us to pass context about the execution in a version transparent way. For example, the ParameterContext object holds a UserContext object that contains information about the user.

To construct a ParameterContext object use the factory method or use another ParameterContext object to construct it:

ParameterContext<CarDetails> param1 = ParameterContext.create(userContext, carDetails);
ParameterContext<CarDetails> param2 = param1.construct(carDetails);

UserContext

In order to retrieve information about the current user, it is possible to use the UserContext object that is passed with each ParameterContext object.

UserContext userContext = param.getUserContext();
Long userId = userContext.getId();
String language = userContext.getLanguage();
String userName = userContext.getUserName();

ProcessingContext

If a task is being executed, a ProcessingContext object is added to the ParameterContext.

Agents

The agent class provides a factory method to create a new agent based on a userContext:

UserContext userContext = parameterContext.getUserContext();
InvoiceAgent invoiceAgent = InvoiceAgent.getAgent(userContext);

The userContext is then passed to each method call performed on the agent. However, other context information, such as the ProcessingContext, is lost this way. Expanders 3.2.0 adds a new constructor based on the new Context class:

Context context = parameterContext.getContext();
InvoiceAgent invoiceAgent = InvoiceAgent.getAgent(context);

This context object contains all context information of the parameterContext and can be used to create new parameterContexts:

ParameterContext<InvoiceDetails> invoiceParameter = context.withParameter(invoiceDetails);

Custom context

The Context class is designed to be extendable. It is possible to add additional context objects.

First, create a context class that implements the ContextGroup interface:

public class PersonContext implements ContextGroup {
  // information on Person ...
}

Then extend the context with your new context-object:

Context context = parameterContext.getContext();
context = context.extend(new PersonContext(/*...*/));


OrderDetails orderDetails = new OrderDetails();
// ...
orderAgent.create(context.withParameter(orderDetails));

You can then query the context where needed:

Context context = parameterContext.getContext();
Option<PersonContext> personContext = context.getContext(PersonContext.class);

if (personContext.isDefined()) {
  // do something with personContext
} else {
  // handle other cases
}

Version

Release Expander version Change
3.0.0 3.0.0 ParameterContext
201712 3.2.0 Created Context object, created context-based constructors for agents