IocContainer

Struct IocContainer 

Source
pub struct IocContainer { /* private fields */ }
Expand description

Modern IoC container with proper dependency injection

Implementations§

Source§

impl IocContainer

Source

pub fn new() -> Self

Create a new IoC container

Source

pub fn from_bindings(bindings: ServiceBindings) -> Self

Create IoC container from existing bindings

Source

pub fn build(&mut self) -> Result<(), CoreError>

Build the container and prepare for service resolution

Source

pub async fn initialize_async(&mut self) -> Result<(), CoreError>

Initialize all async services

Source

pub async fn initialize_async_with_timeout( &mut self, timeout: Duration, ) -> Result<(), CoreError>

Initialize all async services with timeout

Source

pub fn create_scope(&self) -> Result<ScopeId, CoreError>

Create a new service scope

Source

pub fn create_child_scope( &self, parent_scope_id: &ScopeId, ) -> Result<ScopeId, CoreError>

Create a child scope from an existing scope

Source

pub async fn dispose_scope(&self, scope_id: &ScopeId) -> Result<(), CoreError>

Dispose of a scope and all its services

Source

pub async fn dispose_all(&mut self) -> Result<(), CoreError>

Dispose all scoped services and lifecycle managed services

Source

pub fn lifecycle_manager(&self) -> &ServiceLifecycleManager

Get a reference to the lifecycle manager

Source

pub fn lifecycle_manager_mut(&mut self) -> &mut ServiceLifecycleManager

Get a mutable reference to the lifecycle manager

Source

pub fn resolve<T: Send + Sync + 'static>(&self) -> Result<Arc<T>, CoreError>

Resolve a service by type

Source

pub fn resolve_scoped<T: Send + Sync + 'static>( &self, scope_id: &ScopeId, ) -> Result<Arc<T>, CoreError>

Resolve a scoped service by type

Source

pub fn resolve_named<T: Send + Sync + 'static>( &self, name: &str, ) -> Result<Arc<T>, CoreError>

Resolve a named service

Source

pub fn try_resolve<T: Send + Sync + 'static>(&self) -> Option<Arc<T>>

Try to resolve a service, returning None if not found

Source

pub fn try_resolve_named<T: Send + Sync + 'static>( &self, name: &str, ) -> Option<Arc<T>>

Try to resolve a named service, returning None if not found

Source

pub fn resolve_injectable<T: Injectable>(&self) -> Result<Arc<T>, CoreError>

Resolve a service using the Injectable trait (auto-wiring)

Source

pub fn resolve_trait<T: ?Sized + Send + Sync + 'static>( &self, ) -> Result<Arc<T>, CoreError>

Resolve a trait object by downcasting from a concrete implementation

Source

pub fn bind_token<Token, Impl>(&mut self) -> Result<&mut Self, CoreError>
where Token: ServiceToken, Impl: Send + Sync + Default + 'static,

Bind a service token to a concrete implementation with transient lifetime

This creates a mapping from a service token to a concrete implementation, enabling semantic dependency resolution through tokens.

§Example
use elif_core::container::{IocContainer, ServiceToken};

// Define service trait and token
trait EmailService: Send + Sync {}
struct EmailToken;
impl ServiceToken for EmailToken {
    type Service = dyn EmailService;
}

// Implementation
#[derive(Default)]
struct SmtpService;
impl EmailService for SmtpService {}

// Bind token to implementation
let mut container = IocContainer::new();
container.bind_token::<EmailToken, SmtpService>()?;
Source

pub fn bind_token_singleton<Token, Impl>( &mut self, ) -> Result<&mut Self, CoreError>
where Token: ServiceToken, Impl: Send + Sync + Default + 'static,

Bind a service token to a concrete implementation as a singleton

Source

pub fn bind_token_scoped<Token, Impl>(&mut self) -> Result<&mut Self, CoreError>
where Token: ServiceToken, Impl: Send + Sync + Default + 'static,

Bind a service token to a concrete implementation as a scoped service

Source

pub fn bind_token_with_lifetime<Token, Impl>( &mut self, lifetime: ServiceScope, ) -> Result<&mut Self, CoreError>
where Token: ServiceToken, Impl: Send + Sync + Default + 'static,

Bind a service token to a concrete implementation with a specific lifetime

Source

pub fn bind_token_named<Token, Impl>( &mut self, name: impl Into<String>, ) -> Result<&mut Self, CoreError>
where Token: ServiceToken, Impl: Send + Sync + Default + 'static,

Bind a named service token to a concrete implementation

Source

pub fn resolve_by_token<Token>(&self) -> Result<Arc<Token::Service>, CoreError>
where Token: ServiceToken, Token::Service: 'static,

Resolve a service by its token type

This enables semantic dependency resolution where services are identified by tokens rather than concrete types, enabling true dependency inversion.

§Example
use std::sync::Arc;
use elif_core::container::{IocContainer, ServiceToken};

// Define service trait and token
trait EmailService: Send + Sync {
    fn send(&self, to: &str, subject: &str, body: &str) -> Result<(), String>;
}
struct EmailToken;
impl ServiceToken for EmailToken {
    type Service = dyn EmailService;
}

// Implementation
#[derive(Default)]
struct SmtpService;
impl EmailService for SmtpService {
    fn send(&self, _to: &str, _subject: &str, _body: &str) -> Result<(), String> {
        Ok(()) // Mock implementation
    }
}

// Setup and resolve
let mut container = IocContainer::new();
container.bind_token::<EmailToken, SmtpService>()?;
container.build()?;
 
// Note: Trait object resolution is not yet fully implemented
// This will be available in a future version:
// let service: Arc<dyn EmailService> = container.resolve_by_token::<EmailToken>()?;
// service.send("user@example.com", "Welcome", "Hello world!")?;
Source

pub fn resolve_by_token_named<Token>( &self, name: &str, ) -> Result<Arc<Token::Service>, CoreError>
where Token: ServiceToken, Token::Service: 'static,

Resolve a named service by its token type

Source

pub fn try_resolve_by_token<Token>(&self) -> Option<Arc<Token::Service>>
where Token: ServiceToken, Token::Service: 'static,

Try to resolve a service by its token type, returning None if not found

Source

pub fn try_resolve_by_token_named<Token>( &self, name: &str, ) -> Option<Arc<Token::Service>>
where Token: ServiceToken, Token::Service: 'static,

Try to resolve a named service by its token type, returning None if not found

Source

pub fn resolve_by_token_scoped<Token>( &self, scope_id: &ScopeId, ) -> Result<Arc<Token::Service>, CoreError>
where Token: ServiceToken, Token::Service: 'static,

Resolve a scoped service by its token type

This resolves services within a specific scope, maintaining the lifecycle and cleanup patterns expected by the existing scope management system.

Source

pub fn resolve_by_token_named_scoped<Token>( &self, name: &str, scope_id: &ScopeId, ) -> Result<Arc<Token::Service>, CoreError>
where Token: ServiceToken, Token::Service: 'static,

Resolve a named scoped service by its token type

Source

pub fn try_resolve_by_token_scoped<Token>( &self, scope_id: &ScopeId, ) -> Option<Arc<Token::Service>>
where Token: ServiceToken, Token::Service: 'static,

Try to resolve a scoped service by its token type, returning None if not found

Source

pub fn try_resolve_by_token_named_scoped<Token>( &self, name: &str, scope_id: &ScopeId, ) -> Option<Arc<Token::Service>>
where Token: ServiceToken, Token::Service: 'static,

Try to resolve a named scoped service by its token type, returning None if not found

Source

pub fn contains_token<Token: ServiceToken>(&self) -> bool

Check if a token is registered

Source

pub fn contains_token_named<Token: ServiceToken>(&self, name: &str) -> bool

Check if a named token is registered

Source

pub fn token_stats(&self) -> TokenRegistryStats

Get token registry statistics

Source

pub fn contains<T: 'static>(&self) -> bool

Check if a service is registered

Source

pub fn contains_named<T: 'static>(&self, name: &str) -> bool

Check if a named service is registered

Source

pub fn service_count(&self) -> usize

Get the number of registered services

Source

pub fn registered_services(&self) -> Vec<ServiceId>

Get all registered service IDs

Source

pub fn is_built(&self) -> bool

Check if the container is built and ready

Source

pub fn validate(&self) -> Result<(), CoreError>

Validate the container configuration

Source

pub fn resolve_all<T: Send + Sync + 'static>( &self, ) -> Result<Vec<Arc<T>>, CoreError>

Resolve all implementations of an interface as a vector

Source

pub fn resolve_all_named<T: Send + Sync + 'static>( &self, ) -> Result<HashMap<String, Arc<T>>, CoreError>

Resolve all implementations of an interface as a HashMap with their names

Source

pub fn resolve_default<T: Send + Sync + 'static>( &self, ) -> Result<Arc<T>, CoreError>

Get default implementation for a type (marked with is_default in BindingConfig)

Source

pub fn get_service_info<T: 'static>(&self) -> Option<String>

Get service information for debugging

Source

pub fn get_registered_services(&self) -> Vec<String>

Get all registered service IDs for debugging

Source

pub fn validate_all_services(&self) -> Result<(), Vec<CoreError>>

Validate that all registered services can be resolved

Source

pub fn get_statistics(&self) -> ServiceStatistics

Get service statistics

Trait Implementations§

Source§

impl Debug for IocContainer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for IocContainer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl DependencyResolver for IocContainer

Source§

fn resolve<T: Send + Sync + 'static>(&self) -> Result<Arc<T>, CoreError>

Resolve a service by type
Source§

fn resolve_named<T: Send + Sync + 'static>( &self, name: &str, ) -> Result<Arc<T>, CoreError>

Resolve a named service
Source§

fn try_resolve<T: Send + Sync + 'static>(&self) -> Option<Arc<T>>

Try to resolve a service, returning None if not found
Source§

fn try_resolve_named<T: Send + Sync + 'static>( &self, name: &str, ) -> Option<Arc<T>>

Try to resolve a named service, returning None if not found
Source§

impl ServiceBinder for IocContainer

Source§

fn add_service_descriptor( &mut self, descriptor: ServiceDescriptor, ) -> Result<&mut Self, CoreError>

Add a pre-built service descriptor
Source§

fn bind<TInterface: ?Sized + 'static, TImpl: Send + Sync + Default + 'static>( &mut self, ) -> &mut Self

Bind an interface to an implementation
Source§

fn bind_singleton<TInterface: ?Sized + 'static, TImpl: Send + Sync + Default + 'static>( &mut self, ) -> &mut Self

Bind an interface to an implementation with singleton lifetime
Source§

fn bind_transient<TInterface: ?Sized + 'static, TImpl: Send + Sync + Default + 'static>( &mut self, ) -> &mut Self

Bind an interface to an implementation with transient lifetime
Source§

fn bind_factory<TInterface: ?Sized + 'static, F, T>( &mut self, factory: F, ) -> &mut Self
where F: Fn() -> Result<T, CoreError> + Send + Sync + 'static, T: Send + Sync + 'static,

Bind a service using a factory function
Source§

fn bind_instance<TInterface: ?Sized + 'static, TImpl: Send + Sync + Clone + 'static>( &mut self, instance: TImpl, ) -> &mut Self

Bind a pre-created instance
Source§

fn bind_named<TInterface: ?Sized + 'static, TImpl: Send + Sync + Default + 'static>( &mut self, name: &str, ) -> &mut Self

Bind a named service
Source§

fn bind_injectable<T: Injectable>(&mut self) -> &mut Self

Bind an Injectable service with auto-wiring
Source§

fn bind_injectable_singleton<T: Injectable>(&mut self) -> &mut Self

Bind an Injectable service as singleton with auto-wiring
Source§

fn bind_with<TInterface: ?Sized + 'static, TImpl: Send + Sync + Default + 'static>( &mut self, ) -> AdvancedBindingBuilder<TInterface>

Advanced bind with fluent configuration - returns builder for chaining
Source§

fn with_implementation<TInterface: ?Sized + 'static, TImpl: Send + Sync + Default + 'static>( &mut self, config: BindingConfig, ) -> &mut Self

Complete advanced binding with implementation
Source§

fn bind_lazy<TInterface: ?Sized + 'static, F, T>( &mut self, factory: F, ) -> &mut Self
where F: Fn() -> T + Send + Sync + 'static, T: Send + Sync + 'static,

Bind a lazy service using factory that gets called only when needed
Source§

fn bind_parameterized_factory<TInterface: ?Sized + 'static, P, F, T>( &mut self, factory: F, ) -> &mut Self
where F: Fn(P) -> Result<T, CoreError> + Send + Sync + 'static, T: Send + Sync + 'static, P: Send + Sync + 'static,

Bind with parameterized factory
Source§

fn bind_collection<TInterface: ?Sized + 'static, F>( &mut self, configure: F, ) -> &mut Self
where F: FnOnce(&mut CollectionBindingBuilder<TInterface>),

Bind a collection of services using a closure-based configuration

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more