Service 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.

Key points:
Services come from two sources: Handler (via
create_injections) and Manager (viaglobal_services)All services are collected in the Service Registry
Injection happens at the
__new__level, before__init__runsBy the time your
__init__executes, all services are already available as instance attributesServices 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
Handler
Plugins of one handler
Router, handler-specific utilities
Global
All plugins
Config, cache, logging, authentication
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.
Manager Connects Them
When you register the plugin:
The manager:
Sees
AddPluginis attached toCalculatorHandlerCalls
CalculatorHandler.create_injections()to get handler servicesCombines with global services from the Manager
Scans
AddPlugin(and its parents) for type-hinted attributesMatches
calculator: CalculatorServicewith theCalculatorServicekeyInjects the service instance during
__new__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:
Always ensure your handler or global services provide all services that your plugins expect. Missing services will cause runtime errors when accessed.
Summary
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