#![allow(warnings)]
use injectable_rs::*;
use std::sync::Arc;
#[injectable]
#[derive(Default, Clone, Debug)]
pub struct Config;
#[injectable]
#[derive(Default, Debug)]
pub struct Database;
#[injectable]
#[derive(Default, Debug)]
pub struct Cache;
pub struct InjectParamService {
db: Inject<Database>,
cache: Inject<Cache>,
}
#[injectable]
impl InjectParamService {
#[injectable(ctor)]
fn new(db: Inject<Database>, cache: Inject<Cache>) -> Self {
println!(" Constructing InjectParamService");
Self { db, cache }
}
fn describe(&self) -> String {
"InjectParamService: db + cache via Inject<T>".to_string()
}
}
pub struct ArcParamService {
db: Arc<Database>,
cache: Arc<Cache>,
}
#[injectable]
impl ArcParamService {
#[injectable(ctor)]
fn new(
#[injectable(inject)] db: Arc<Database>,
#[injectable(inject)] cache: Arc<Cache>,
) -> Self {
println!(" Constructing ArcParamService");
Self { db, cache }
}
fn describe(&self) -> String {
"ArcParamService: db + cache via Arc<T>".to_string()
}
}
pub struct ArcConfigService {
config: Arc<Config>,
}
#[injectable]
impl ArcConfigService {
#[injectable(ctor)]
fn new(#[injectable(inject)] config: Arc<Config>) -> Self {
println!(" Constructing ArcConfigService");
Self { config }
}
fn describe(&self) -> String {
"ArcConfigService: config via Arc<T>".to_string()
}
}
pub struct MixedParamService {
db: Inject<Database>,
config: Arc<Config>,
cache: Arc<Cache>,
}
#[injectable]
impl MixedParamService {
#[injectable(ctor)]
fn new(
db: Inject<Database>,
#[injectable(inject)] config: Arc<Config>,
#[injectable(inject)] cache: Arc<Cache>,
) -> Self {
println!(" Constructing MixedParamService");
Self { db, config, cache }
}
fn describe(&self) -> String {
"MixedParamService: Inject<T> + Arc<T> + Arc<T>".to_string()
}
}
pub struct NoDepService {
started_at: String,
}
#[injectable]
impl NoDepService {
#[injectable(ctor)]
fn new() -> Self {
println!(" Constructing NoDepService");
Self {
started_at: "2024-01-01T00:00:00Z".to_string(),
}
}
fn describe(&self) -> String {
format!("NoDepService: started_at={}", self.started_at)
}
}
pub struct AsyncCtorService {
db: Inject<Database>,
}
#[injectable]
impl AsyncCtorService {
#[injectable(ctor)]
async fn new(db: Inject<Database>) -> Self {
println!(" Constructing AsyncCtorService (async)");
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
Self { db }
}
fn describe(&self) -> String {
"AsyncCtorService: async constructor with Inject<Database>".to_string()
}
}
#[tokio::main]
async fn main() {
println!("=== Constructor Injection Example ===\n");
let container = Container::builder()
.build()
.await
.expect("container should build");
println!("1. Inject<T> parameters:");
let svc = container.resolve::<InjectParamService>().await.unwrap();
println!(" {}\n", svc.describe());
println!("2. Arc<T> parameters:");
let svc = container.resolve::<ArcParamService>().await.unwrap();
println!(" {}\n", svc.describe());
println!("3. Arc<T> config parameter:");
let svc = container.resolve::<ArcConfigService>().await.unwrap();
println!(" {}\n", svc.describe());
println!("4. Mixed parameter types:");
let svc = container.resolve::<MixedParamService>().await.unwrap();
println!(" {}\n", svc.describe());
println!("5. No dependencies:");
let svc = container.resolve::<NoDepService>().await.unwrap();
println!(" {}\n", svc.describe());
println!("6. Async constructor:");
let svc = container.resolve::<AsyncCtorService>().await.unwrap();
println!(" {}\n", svc.describe());
println!("=== Example Complete ===");
}