Skip to main content

Module di

Module di 

Source
Available on crate feature di only.
Expand description

Compile-time dependency injection infrastructure.

Build dependency graphs that are resolved at compile time for zero runtime overhead.

§Example

use allframe::di::{ContainerBuilder, Provider};

let container = ContainerBuilder::new()
    .register::<DatabasePool>()
    .register::<UserRepository>()
    .build();

Dependency Injection Infrastructure

This module provides compile-time dependency injection with support for:

  • Async initialization
  • Explicit dependency declaration
  • Singleton and transient scoping
  • Environment-based configuration

§Example

use allframe_core::di::{Provider, Scope, DependencyError};
use allframe_macros::di_container;

#[di_container]
struct AppContainer {
    #[provide(from_env)]
    config: Config,

    #[provide(singleton, async)]
    #[depends(config)]
    database: DatabasePool,

    #[provide(transient)]
    service: MyService,
}

// Generated async build method
let container = AppContainer::build().await?;

Re-exports§

pub use lazy::*;

Modules§

lazy
Lazy initialization for DI containers

Structs§

ContainerBuilder
Builder for constructing dependency containers with explicit ordering
DependencyRegistry
A type-erased container for storing dependencies

Enums§

DependencyError
Error type for dependency injection operations
Scope
Scope determines the lifecycle of a dependency

Traits§

AsyncInit
Trait for async initialization
AsyncInitWith
Trait for types that can be initialized with dependencies
FromEnv
Trait for types that can be loaded from environment variables
Provider
Trait for types that can provide dependencies

Functions§

env_var
Helper to load a value from an environment variable
env_var_opt
Helper to load an optional value from an environment variable
env_var_or
Helper to load a value from an environment variable with a default
env_var_parse
Helper to parse a value from an environment variable