aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-08-25 20:21:49 +0200
committerHampusM <hampus@hampusmat.com>2022-08-27 14:28:23 +0200
commit1c46b68581213ca8ae6200daa32f626b5389b4b0 (patch)
tree8da649471db7893a2347a2df383324b84ac226f0 /examples
parent8e862c7998d0b59c71d20cbcbbc57031f734b6fa (diff)
refactor!: make DI container have single get function
BREAKING CHANGE: The DI container get_singleton & get_factory functions have been replaced by the get function now returning a enum
Diffstat (limited to 'examples')
-rw-r--r--examples/basic/main.rs4
-rw-r--r--examples/factory/bootstrap.rs18
-rw-r--r--examples/factory/main.rs10
-rw-r--r--examples/generics/main.rs10
-rw-r--r--examples/unbound/main.rs4
-rw-r--r--examples/with-3rd-party/main.rs2
6 files changed, 28 insertions, 20 deletions
diff --git a/examples/basic/main.rs b/examples/basic/main.rs
index 72f07c2..dbc9215 100644
--- a/examples/basic/main.rs
+++ b/examples/basic/main.rs
@@ -18,11 +18,11 @@ fn main() -> Result<(), Box<dyn Error>>
let di_container = bootstrap()?;
- let dog = di_container.get_singleton::<dyn IDog>()?;
+ let dog = di_container.get::<dyn IDog>()?.singleton()?;
dog.woof();
- let human = di_container.get::<dyn IHuman>()?;
+ let human = di_container.get::<dyn IHuman>()?.transient()?;
human.make_pets_make_sounds();
diff --git a/examples/factory/bootstrap.rs b/examples/factory/bootstrap.rs
index b752764..ad8c4d3 100644
--- a/examples/factory/bootstrap.rs
+++ b/examples/factory/bootstrap.rs
@@ -1,3 +1,5 @@
+use std::error::Error;
+
use syrette::ptr::TransientPtr;
use syrette::DIContainer;
@@ -9,24 +11,22 @@ use crate::interfaces::user_manager::IUserManager;
use crate::user::User;
use crate::user_manager::UserManager;
-pub fn bootstrap() -> DIContainer
+pub fn bootstrap() -> Result<DIContainer, Box<dyn Error>>
{
let mut di_container: DIContainer = DIContainer::new();
di_container
.bind::<dyn IUserManager>()
- .to::<UserManager>()
- .unwrap();
+ .to::<UserManager>()?;
- di_container
- .bind::<IUserFactory>()
- .to_factory(&|name, date_of_birth, password| {
+ di_container.bind::<IUserFactory>().to_factory(
+ &|name, date_of_birth, password| {
let user: TransientPtr<dyn IUser> =
TransientPtr::new(User::new(name, date_of_birth, password));
user
- })
- .unwrap();
+ },
+ )?;
- di_container
+ Ok(di_container)
}
diff --git a/examples/factory/main.rs b/examples/factory/main.rs
index bf3d43b..0f1a97b 100644
--- a/examples/factory/main.rs
+++ b/examples/factory/main.rs
@@ -7,21 +7,25 @@ mod interfaces;
mod user;
mod user_manager;
+use std::error::Error;
+
use bootstrap::bootstrap;
use crate::interfaces::user_manager::IUserManager;
-fn main()
+fn main() -> Result<(), Box<dyn Error>>
{
println!("Hello, world!");
- let di_container = bootstrap();
+ let di_container = bootstrap()?;
- let mut user_manager = di_container.get::<dyn IUserManager>().unwrap();
+ let mut user_manager = di_container.get::<dyn IUserManager>()?.transient()?;
user_manager.fill_with_users();
println!("Printing user information");
user_manager.print_users();
+
+ Ok(())
}
diff --git a/examples/generics/main.rs b/examples/generics/main.rs
index 9442641..f491aa0 100644
--- a/examples/generics/main.rs
+++ b/examples/generics/main.rs
@@ -2,18 +2,22 @@ mod bootstrap;
mod interfaces;
mod printer;
+use std::error::Error;
+
use bootstrap::bootstrap;
use interfaces::printer::IPrinter;
-fn main()
+fn main() -> Result<(), Box<dyn Error>>
{
let di_container = bootstrap();
- let string_printer = di_container.get::<dyn IPrinter<String>>().unwrap();
+ let string_printer = di_container.get::<dyn IPrinter<String>>()?.transient()?;
string_printer.print("Hello there".to_string());
- let int_printer = di_container.get::<dyn IPrinter<i32>>().unwrap();
+ let int_printer = di_container.get::<dyn IPrinter<i32>>()?.transient()?;
int_printer.print(2782028);
+
+ Ok(())
}
diff --git a/examples/unbound/main.rs b/examples/unbound/main.rs
index 031a691..e9a8feb 100644
--- a/examples/unbound/main.rs
+++ b/examples/unbound/main.rs
@@ -19,11 +19,11 @@ fn main() -> Result<(), Box<dyn Error>>
let di_container = bootstrap()?;
- let dog = di_container.get_singleton::<dyn IDog>()?;
+ let dog = di_container.get::<dyn IDog>()?.singleton()?;
dog.woof();
- let human = di_container.get::<dyn IHuman>()?;
+ let human = di_container.get::<dyn IHuman>()?.transient()?;
human.make_pets_make_sounds();
diff --git a/examples/with-3rd-party/main.rs b/examples/with-3rd-party/main.rs
index dd4c21f..e48c78f 100644
--- a/examples/with-3rd-party/main.rs
+++ b/examples/with-3rd-party/main.rs
@@ -17,7 +17,7 @@ fn main() -> Result<(), Box<dyn Error>>
let di_container = bootstrap()?;
- let ninja = di_container.get::<dyn INinja>()?;
+ let ninja = di_container.get::<dyn INinja>()?.transient()?;
ninja.throw_shuriken();