pub struct ComponentContainer { /* private fields */ }Expand description
The central IoC container for the Verdure ecosystem
ComponentContainer serves as the heart of the Verdure ecosystem’s dependency injection system.
It provides the runtime infrastructure that enables all Verdure modules - from web frameworks
to data access layers - to work together through declarative component management.
This container powers the entire Verdure ecosystem by providing:
- Cross-module Integration: Components from different Verdure modules can seamlessly depend on each other
- Ecosystem Coherence: Unified component management across verdure-web, verdure-data, verdure-security, etc.
§Features
- Thread-safe: Uses concurrent data structures for safe multi-threaded access
- Dependency Resolution: Automatically resolves and injects component dependencies
- Circular Dependency Detection: Prevents infinite dependency loops during resolution
- Lifecycle Events: Publishes events during container and component lifecycle operations
- Statistics Tracking: Maintains creation and access statistics for monitoring
§Examples
use verdure_ioc::{ComponentContainer, ComponentFactory};
use std::sync::Arc;
#[derive(Debug)]
struct DatabaseService {
connection_string: String,
}
// Create container
let container = ComponentContainer::new();
// Register a component
let db_service = Arc::new(DatabaseService {
connection_string: "postgres://localhost:5432/mydb".to_string(),
});
container.register_component(db_service);
// Retrieve the component
let retrieved: Option<Arc<DatabaseService>> = container.get_component();
assert!(retrieved.is_some());
// Initialize automatic components (from #[derive(Component)])
let result = container.initialize();
assert!(result.is_ok());Implementations§
Source§impl ComponentContainer
impl ComponentContainer
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty ComponentContainer
§Examples
use verdure_ioc::ComponentContainer;
let container = ComponentContainer::new();Sourcepub fn initialize(&self) -> Result<(), ContainerError>
pub fn initialize(&self) -> Result<(), ContainerError>
Initializes the container by discovering and creating all registered components
This method scans for all components registered via the #[derive(Component)] macro
and creates instances of them, resolving their dependencies automatically.
It also publishes lifecycle events during the initialization process.
§Returns
Ok(())- If initialization completed successfullyErr(ContainerError)- If there was an error during initialization (e.g., circular dependencies)
§Examples
use verdure_ioc::ComponentContainer;
let container = ComponentContainer::new();
match container.initialize() {
Ok(_) => println!("Container initialized successfully"),
Err(e) => eprintln!("Initialization failed: {}", e),
}Sourcepub fn register_component(&self, instance: ComponentInstance)
pub fn register_component(&self, instance: ComponentInstance)
Registers a pre-created component instance with the container
This method allows manual registration of component instances that have been created outside the container’s automatic initialization process.
§Arguments
instance- The component instance to register
§Examples
use verdure_ioc::ComponentContainer;
use std::sync::Arc;
#[derive(Debug)]
struct ConfigService {
config_path: String,
}
let container = ComponentContainer::new();
let config = Arc::new(ConfigService {
config_path: "/etc/myapp/config.toml".to_string(),
});
container.register_component(config);Sourcepub fn register_component_by_type_id(
&self,
type_id: TypeId,
instance: ComponentInstance,
)
pub fn register_component_by_type_id( &self, type_id: TypeId, instance: ComponentInstance, )
Registers a component instance with the container using a specific TypeId
This method is useful when you need to register a component with a different type identity than its concrete type.
§Arguments
type_id- The TypeId to use for component registrationinstance- The component instance to register