pub struct ServiceContainer { /* private fields */ }Expand description
Service container for dependency injection and service management.
The service container holds references to core services and provides a centralized way to access them throughout the application. It manages the lifecycle of services and ensures proper dependency injection.
§Design Principles
- Single Source of Truth: All services are managed through the container
- Dependency Injection: Components receive dependencies explicitly
- Configuration Isolation: Services are decoupled from global configuration
- Test Friendliness: Easy to mock and test individual components
§Examples
use subx_cli::core::ServiceContainer;
use subx_cli::config::ProductionConfigService;
use std::sync::Arc;
let config_service = Arc::new(ProductionConfigService::new()?);
let container = ServiceContainer::new(config_service)?;
// Access services through container
let config_service = container.config_service();
let factory = container.component_factory();Implementations§
Source§impl ServiceContainer
impl ServiceContainer
Sourcepub fn new(config_service: Arc<dyn ConfigService>) -> Result<Self>
pub fn new(config_service: Arc<dyn ConfigService>) -> Result<Self>
Sourcepub fn config_service(&self) -> &Arc<dyn ConfigService>
pub fn config_service(&self) -> &Arc<dyn ConfigService>
Get a reference to the configuration service.
Returns a reference to the configuration service managed by this container.
Sourcepub fn component_factory(&self) -> &ComponentFactory
pub fn component_factory(&self) -> &ComponentFactory
Get a reference to the component factory.
Returns a reference to the component factory that can create configured instances of core components.
Sourcepub fn reload(&mut self) -> Result<()>
pub fn reload(&mut self) -> Result<()>
Reload all services and components.
This method triggers a reload of the configuration service and recreates the component factory with the updated configuration. This is useful for dynamic configuration updates.
§Errors
Returns an error if configuration reloading or factory recreation fails.