Service provider implementation for dependency injection.
This module provides a centralized service provider that manages
dependencies and services used throughout the application.
-
class ml_research_tools.core.service_provider.ServiceProvider(config)[source]
Bases: object
A service provider that manages dependencies and services.
This class implements the service locator pattern, allowing services
to be registered and retrieved. It supports lazy initialization of
services and singleton instances.
Initialize the service provider with a configuration.
- Parameters:
config (Config
) – The application configuration
-
__init__(config)[source]
Initialize the service provider with a configuration.
- Parameters:
config (Config
) – The application configuration
-
register(name, instance)[source]
Register a service instance with the provider.
- Parameters:
-
- Return type:
None
-
register_factory(name, factory)[source]
Register a factory function for lazy initialization of a service.
- Parameters:
name (str
) – The name to register the service under
factory (Callable
[[], Any
]) – A callable that creates the service when needed
- Return type:
None
-
get(name)[source]
Get a service by name.
- Parameters:
name (str
) – The name of the service to retrieve
- Return type:
Any
- Returns:
The service instance
- Raises:
KeyError – If the service is not registered
-
has(name)[source]
Check if a service is registered.
- Parameters:
name (str
) – The name of the service to check
- Return type:
bool
- Returns:
True if the service is registered, False otherwise
-
get_config()[source]
Get the configuration object.
- Return type:
Config
- Returns:
The configuration object
-
get_or_create(name, factory)[source]
Get a service by name, or create it if it doesn’t exist.
- Parameters:
name (str
) – The name of the service to retrieve
factory (Callable
[[], TypeVar
(T
)]) – A callable that creates the service if needed
- Return type:
TypeVar
(T
)
- Returns:
The service instance
-
get_typed(name, expected_type)[source]
Get a service by name with type checking.
- Parameters:
-
- Return type:
TypeVar
(T
)
- Returns:
The service instance
- Raises:
TypeError – If the service is not of the expected type