Skip to main content

ComponentContainer

Struct ComponentContainer 

Source
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

Source

pub fn new() -> Self

Creates a new empty ComponentContainer

§Examples
use verdure_ioc::ComponentContainer;

let container = ComponentContainer::new();
Source

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 successfully
  • Err(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),
}
Source

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);
Source

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 registration
  • instance - The component instance to register

Trait Implementations§

Source§

impl ComponentFactory for ComponentContainer

Source§

fn get_component_by_type_id( &self, type_id: TypeId, ) -> Option<Arc<dyn Any + Send + Sync>>

Retrieves a component instance by its TypeId Read more
Source§

fn get_component<T: Any + Send + Sync>(&self) -> Option<Arc<T>>

Retrieves a component instance with compile-time type safety Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.