Expand description
Derive macros for dependency-injector
This crate provides the #[derive(Inject)] macro for automatic
dependency injection at compile time.
§Example
ⓘ
use dependency_injector::{Container, Inject};
use std::sync::Arc;
#[derive(Clone)]
struct Database {
url: String,
}
#[derive(Clone)]
struct Cache {
size: usize,
}
#[derive(Inject)]
struct UserService {
#[inject]
db: Arc<Database>,
#[inject]
cache: Arc<Cache>,
// Non-injected fields use Default
request_count: u64,
}
let container = Container::new();
container.singleton(Database { url: "postgres://localhost".into() });
container.singleton(Cache { size: 1024 });
let service = UserService::from_container(&container).unwrap();Derive Macros§
- Inject
- Derive macro for automatic dependency injection.