Services

A common pattern when developing with KOS is the use of services, which act as managers or coordinators responsible for specific functionality. Services are generally singletons, and KOS core itself contains numerous services, such as LoggingService, ApplicationService, FuseService and others. A service should ideally have a single, well-defined purpose, and developers are encouraged to convert any singleton objects into a service so that they can take advantage of the KOS service infrastructure.

Services extend AbstractService or AbstractConfigurableService, which provides a substantial amount of functionality for free:

  1. Autowiring: Services often depend on other services, and autowiring ensures these dependencies are properly discovered and injected. To learn more about dependency injection in KOS read this article: link.
  2. Ready: Another built-in feature is the ability for services to indicate when they are fully initialized and ready to be used. Many services perform asynchronous work during startup, while others must wait until this work is complete before interacting with them. Extending the service base classes automatically connects the service to the ready protocol, allowing it to signal readiness and listen for the readiness of its dependencies. More details on this can be found in the article on readiness: link.
  3. Configurations: AbstractConfigurableService also provides a framework for attaching configuration data to a service. This cleanly separates functionality from configurations, while also providing callbacks for any updates to the configs. An article describing the configuration system in detail is available here: link.
  4. Handles: Services automatically inherit handle-paths based on the context they are placed in. This allows services to be referenced, exposed, and interacted with consistently across different application layers. More information about handles can be found here: link.

Therefore, extending the appropriate base class and placing your service in the application context gives you a wide range of powerful features. The service framework handles dependency wiring, readiness sequencing, configuration lifecycle management, handle resolution, and more, so developers can focus on the actual logic their service is meant to provide.