diff options
Diffstat (limited to 'examples/async/bootstrap.rs')
-rw-r--r-- | examples/async/bootstrap.rs | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/examples/async/bootstrap.rs b/examples/async/bootstrap.rs index 7e1d2cd..51af067 100644 --- a/examples/async/bootstrap.rs +++ b/examples/async/bootstrap.rs @@ -2,17 +2,20 @@ use std::sync::Arc; use anyhow::Result; use syrette::async_di_container::AsyncDIContainer; +use syrette::declare_default_factory; +use syrette::ptr::TransientPtr; -// Concrete implementations use crate::animals::cat::Cat; use crate::animals::dog::Dog; use crate::animals::human::Human; -// -// Interfaces +use crate::food::Food; use crate::interfaces::cat::ICat; use crate::interfaces::dog::IDog; +use crate::interfaces::food::{IFood, IFoodFactory}; use crate::interfaces::human::IHuman; +declare_default_factory!(dyn ICat, threadsafe = true); + pub async fn bootstrap() -> Result<Arc<AsyncDIContainer>> { let mut di_container = AsyncDIContainer::new(); @@ -24,8 +27,27 @@ pub async fn bootstrap() -> Result<Arc<AsyncDIContainer>> .in_singleton_scope() .await?; - di_container.bind::<dyn ICat>().to::<Cat>().await?; + di_container + .bind::<dyn ICat>() + .to_default_factory(&|_| { + let cat: TransientPtr<dyn ICat> = TransientPtr::new(Cat::new()); + + cat + }) + .await?; + di_container.bind::<dyn IHuman>().to::<Human>().await?; + di_container + .bind::<IFoodFactory>() + .to_factory(&|_| { + Box::new(|| { + let food: Box<dyn IFood> = Box::new(Food::new()); + + food + }) + }) + .await?; + Ok(di_container) } |