Expand description
LRDI — Rust Dependency Injection Framework
A dependency injection container for Rust inspired by Microsoft.Extensions.DependencyInjection (MEDI).
§Quick start
use rust_dicore::*;
use std::sync::Arc;
struct Logger(String);
struct Worker { log: Arc<Logger> }
let provider = ServiceCollection::new()
.singleton(|_| Arc::new(Logger("INFO".into())))
.singleton(|_| Arc::new(Worker { log: Arc::new(Logger("INFO".into())) }))
.build()
.unwrap();
let logger: Arc<Logger> = provider.get::<Logger>();§Features
- Three lifetimes: Singleton, Scoped, Transient
- Keyed services: multiple instances of the same type, distinguished by key
- Constructor injection:
#[derive(Inject)]on structs - Compile-time module scanning:
#[rust_dicore::module]collects service registrations - Cross-DLL support: named service registry for cdylib plugins
§Crate layout
| Module | Description |
|---|---|
ServiceCollection | Register services with a builder API |
ServiceProvider | The root DI container |
Scope / ServiceScope | Scoped container (one per scope) |
IServiceResolver | Resolution trait (implemented by ServiceProvider & Scope) |
ServiceProviderWrapper | Child-first layered container |
ServiceLifetime | Enum: Singleton, Scoped, Transient |
IServiceLocator / ServiceLocatorBridge | Cross-DLL / plugin integration |
RdiError | Error types |
§Proc-macros
#[derive(Inject)]— auto-generates constructor injection code#[rust_dicore::module]— compile-time module scanningrust_dicore::inject!(...)— declare services inside#[rust_dicore::module]
Re-exports§
pub use collection::ServiceCollection;pub use provider::ServiceProvider;pub use descriptor::ServiceDescriptor;pub use lifetime::ServiceLifetime;pub use entry::IServiceResolver;pub use scope::Scope;pub use scope::Scope as ServiceScope;pub use entry::ServiceEntry;pub use entry::ServiceFactory;pub use wrapper::ServiceProviderWrapper;pub use service_locator::IServiceLocator;pub use service_locator::INamedRegistrar;pub use bridge::RdiProvider;pub use bridge::ServiceLocatorBridge;pub use error::RdiError;pub use registration::ServiceRegistration;
Modules§
- bridge
- Bridge implementations that adapt RDI types to the
sdk::ServiceLocatortrait. - collection
- descriptor
- entry
- error
- lifetime
- provider
- registration
- scope
- service_
locator - store
- wrapper