aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-08-22 19:13:19 +0200
committerHampusM <hampus@hampusmat.com>2022-08-22 19:13:19 +0200
commit8e862c7998d0b59c71d20cbcbbc57031f734b6fa (patch)
treefbcddcfa697d0b67c9d20022004aa2ae16c5a339 /examples
parentda3f56d48f9c349b29e1d462b2a535b1a89c5e45 (diff)
refactor!: move specifying binding scope to a binding scope configurator
BREAKING CHANGE: Specifying the scope of a DI container binding is now done with a binding scope configurator
Diffstat (limited to 'examples')
-rw-r--r--examples/basic/bootstrap.rs15
-rw-r--r--examples/basic/main.rs12
-rw-r--r--examples/unbound/bootstrap.rs15
-rw-r--r--examples/unbound/main.rs4
4 files changed, 27 insertions, 19 deletions
diff --git a/examples/basic/bootstrap.rs b/examples/basic/bootstrap.rs
index 4af2d82..4e02dc3 100644
--- a/examples/basic/bootstrap.rs
+++ b/examples/basic/bootstrap.rs
@@ -1,3 +1,5 @@
+use std::error::Error;
+
use syrette::DIContainer;
// Concrete implementations
@@ -10,16 +12,17 @@ use crate::interfaces::cat::ICat;
use crate::interfaces::dog::IDog;
use crate::interfaces::human::IHuman;
-pub fn bootstrap() -> DIContainer
+pub fn bootstrap() -> Result<DIContainer, Box<dyn Error>>
{
let mut di_container: DIContainer = DIContainer::new();
di_container
.bind::<dyn IDog>()
- .to_singleton::<Dog>()
- .unwrap();
- di_container.bind::<dyn ICat>().to::<Cat>().unwrap();
- di_container.bind::<dyn IHuman>().to::<Human>().unwrap();
+ .to::<Dog>()?
+ .in_singleton_scope()?;
- di_container
+ di_container.bind::<dyn ICat>().to::<Cat>()?;
+ di_container.bind::<dyn IHuman>().to::<Human>()?;
+
+ Ok(di_container)
}
diff --git a/examples/basic/main.rs b/examples/basic/main.rs
index 3a937c3..72f07c2 100644
--- a/examples/basic/main.rs
+++ b/examples/basic/main.rs
@@ -2,6 +2,8 @@
#![deny(clippy::pedantic)]
#![allow(clippy::module_name_repetitions)]
+use std::error::Error;
+
mod animals;
mod bootstrap;
mod interfaces;
@@ -10,17 +12,19 @@ use bootstrap::bootstrap;
use interfaces::dog::IDog;
use interfaces::human::IHuman;
-fn main()
+fn main() -> Result<(), Box<dyn Error>>
{
println!("Hello, world!");
- let di_container = bootstrap();
+ let di_container = bootstrap()?;
- let dog = di_container.get_singleton::<dyn IDog>().unwrap();
+ let dog = di_container.get_singleton::<dyn IDog>()?;
dog.woof();
- let human = di_container.get::<dyn IHuman>().unwrap();
+ let human = di_container.get::<dyn IHuman>()?;
human.make_pets_make_sounds();
+
+ Ok(())
}
diff --git a/examples/unbound/bootstrap.rs b/examples/unbound/bootstrap.rs
index dc8468c..f59bc83 100644
--- a/examples/unbound/bootstrap.rs
+++ b/examples/unbound/bootstrap.rs
@@ -1,3 +1,5 @@
+use std::error::Error;
+
use syrette::DIContainer;
// Concrete implementations
@@ -10,21 +12,20 @@ use crate::interfaces::animal_store::IAnimalStore;
use crate::interfaces::dog::IDog;
use crate::interfaces::human::IHuman;
-pub fn bootstrap() -> DIContainer
+pub fn bootstrap() -> Result<DIContainer, Box<dyn Error>>
{
let mut di_container: DIContainer = DIContainer::new();
di_container
.bind::<dyn IDog>()
- .to_singleton::<Dog>()
- .unwrap();
+ .to::<Dog>()?
+ .in_singleton_scope()?;
- di_container.bind::<dyn IHuman>().to::<Human>().unwrap();
+ di_container.bind::<dyn IHuman>().to::<Human>()?;
di_container
.bind::<dyn IAnimalStore>()
- .to::<AnimalStore>()
- .unwrap();
+ .to::<AnimalStore>()?;
- di_container
+ Ok(di_container)
}
diff --git a/examples/unbound/main.rs b/examples/unbound/main.rs
index 47629e4..031a691 100644
--- a/examples/unbound/main.rs
+++ b/examples/unbound/main.rs
@@ -17,9 +17,9 @@ fn main() -> Result<(), Box<dyn Error>>
{
println!("Hello, world!");
- let di_container = bootstrap();
+ let di_container = bootstrap()?;
- let dog = di_container.get_singleton::<dyn IDog>().unwrap();
+ let dog = di_container.get_singleton::<dyn IDog>()?;
dog.woof();