Skip to main content

Module di

Module di 

Source
Expand description

Dependency Injection Container (optional utility)

Primary DI in RustAPI: pass Arc<Service> directly to RouterPipeline::mount::<Controller>(Arc::new(MyService::new())). No container is needed for the standard use case.

This module provides an optional Container — a type-safe service registry useful for larger applications with dynamic or plugin-driven service graphs, where you want late-binding resolution by type.

§When to Use

  • You have a plugin system that registers services dynamically
  • You want to swap implementations at runtime (e.g. test doubles via trait objects)
  • You have many services and prefer centralized registration over explicit wiring

§When NOT to Use

For most APIs: construct Arc<MyService> directly in main and pass it to pipeline.mount(). This is explicit, compile-time-checked, and requires no type-map machinery.

§Immutability Discipline

Services registered in the container are stored as Arc<T> and shared read-only across the application. Service types should be immutable after construction — model state changes via Atomic* primitives or channels, not Mutex<T> fields. The framework enforces this by only providing shared (Arc) references, never exclusive (Arc<Mutex<T>>) ones.

§Async Initialization

Use Container::register_async_factory for services that require async initialization (database connections, config loading, etc.). The async effect is contained at container-construction time; all subsequent framework operations remain synchronous.

Alternatively, without a container:

let svc = Arc::new(MyService::connect("postgres://...").await?);
RouterPipeline::new().mount::<MyController>(svc).build()?

Structs§

Container
Dependency injection container

Traits§

Injectable
Trait that all injectable services must implement