syringeService Injection

How Injection Works

Service injection is GL Plugin's mechanism for providing dependencies to your plugins automatically. Instead of manually instantiating services inside your plugins, you declare what you need and the framework delivers it.

Plugin Service Hierarchy: Mermaid Linkarrow-up-right

Key points:

  • Services come from two sources: Handler (via create_injections) and Manager (via global_services)

  • All services are collected in the Service Registry

  • Injection happens at the __new__ level, before __init__ runs

  • By the time your __init__ executes, all services are already available as instance attributes

  • Services are identified by type, not by name


Type-Based Resolution

GL Plugin uses the class type to match services with plugin attributes. This means:

The framework looks at the type hint (CalculatorService) and finds the matching key in the injection dictionary. The attribute name (calculator) can be anything you want.


Handler Services

Handler services are defined in your handler's create_injections() method. These services are specific to plugins attached to that handler.

Basic Definition

Using Handler Properties

You can access handler properties via the instance parameter to configure services:


Global Services

Global services are shared across all plugins, regardless of which handler they're attached to. They're registered at the Manager level.

Registering Global Services

Pass services to the global_services parameter when creating the Manager:

Consuming Global Services

Declare them in your plugin just like handler services:

When to Use Global vs Handler Services

Service Type
Scope
Use Case

Handler

Plugins of one handler

Router, handler-specific utilities

Global

All plugins

Config, cache, logging, authentication

circle-info

Use global services for cross-cutting concerns that multiple plugin types need. Use handler services for functionality specific to a plugin category.

Example: Combining Both


Consuming Services in Plugins

To receive an injected service, declare it as a class attribute with a type hint:

Inheritance

When extending plugins, child classes inherit all service injections from their parents:


Example: Service Injection in Action

Let's trace through the complete flow using our Calculator example.

1

Handler Defines Services

2

Plugin Declares Dependencies

3

Manager Connects Them

When you register the plugin:

The manager:

  1. Sees AddPlugin is attached to CalculatorHandler

  2. Calls CalculatorHandler.create_injections() to get handler services

  3. Combines with global services from the Manager

  4. Scans AddPlugin (and its parents) for type-hinted attributes

  5. Matches calculator: CalculatorService with the CalculatorService key

  6. Injects the service instance during __new__

  7. Calls __init__ — service is ready to use


Multiple Services

Plugins can receive multiple services from both handler and global sources:


What Happens If a Service Is Missing?

If a plugin declares a service that neither the handler nor global services provide, the attribute will not be set. Accessing it will raise an AttributeError:

circle-exclamation

Summary

Concept
Description

Injection timing

Services are injected at __new__, before __init__

Resolution

Type-based matching (class type, not attribute name)

Declaration

Use type hints on class attributes

Inheritance

Child plugins inherit parent's service declarations

Handler services

Specific to plugins of that handler

Global services

Shared across all plugins

Last updated