A microservice is a set of loosely coupled components, each of which serves a specific purpose, such as logging events, reading records from a database, or connecting to a 3rd party service.
One of the roles of the microservice’s container is to correctly initialize all internal components, each of which can have its own lifecycle. For example, loading its own configuration, running certain functional processes, and even waiting for results from other components. The order in which component lifecycle management methods are called is as follows:
A flexible and, at the same time, standardized approach was developed in the Pip.Services Toolkit for initializing components. This approach preserves the conceptual integrity of the whole microservice, while keeping the source code clean, coherent, and testable. A component’s lifecycle is determined by which of the following interfaces it implements:
Microservice developers are free to implement just the interfaces needed by their components. When the container is started, all implemented methods will be called in the previously mentioned order.
For example:
The Pip.Service’s Toolkit also includes a few utilities that can be used during microservice development:
- Opener – initiates the functional processes of selected components,
- Closer – stops the functional processes of selected components,
- Executor – runs the functional processes of selected components,
- Notifier - sends event notifications for selected components.
- Cleaner – cleans the current state of selected components.
For example:
await Opener.OpenAsync(correlationId, _references.GetAll());
…
await Closer.CloseAsync(correlationId, _references.GetAll());
…
Microservice developers are free to implement just the interfaces needed by their components. When the container is started, all implemented methods will be called in the previously mentioned order.
For example:
The Pip.Service’s Toolkit also includes a few utilities that can be used during microservice development:
- Opener – initiates the functional processes of selected components,
- Closer – stops the functional processes of selected components,
- Executor – runs the functional processes of selected components,
- Notifier - sends event notifications for selected components.
- Cleaner – cleans the current state of selected components.
For example:
await Opener.open(correlationId, references.getAll());
…
await Closer.close(correlationId, references.getAll());
…