aboutsummaryrefslogtreecommitdiff
path: root/examples/async/bootstrap.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-09-17 18:33:43 +0200
committerHampusM <hampus@hampusmat.com>2022-09-17 18:33:43 +0200
commit7de7f73963a266cceff85d6ab71c3256e5d382ec (patch)
tree67575870945b7ed0a5eeb99ccba79327598b3e02 /examples/async/bootstrap.rs
parent8651f84f205da7a89f2fc7333d1dd8de0d80a22b (diff)
feat!: allow factories to access async DI container
BREAKING CHANGE: The to_factory & to_default_factory methods of AsyncBindingBuilder now expects a function returning a factory function
Diffstat (limited to 'examples/async/bootstrap.rs')
-rw-r--r--examples/async/bootstrap.rs30
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)
}