From 1c46b68581213ca8ae6200daa32f626b5389b4b0 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 25 Aug 2022 20:21:49 +0200 Subject: 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 --- examples/factory/bootstrap.rs | 18 +++++++++--------- examples/factory/main.rs | 10 +++++++--- 2 files changed, 16 insertions(+), 12 deletions(-) (limited to 'examples/factory') 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> { let mut di_container: DIContainer = DIContainer::new(); di_container .bind::() - .to::() - .unwrap(); + .to::()?; - di_container - .bind::() - .to_factory(&|name, date_of_birth, password| { + di_container.bind::().to_factory( + &|name, date_of_birth, password| { let user: TransientPtr = 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> { println!("Hello, world!"); - let di_container = bootstrap(); + let di_container = bootstrap()?; - let mut user_manager = di_container.get::().unwrap(); + let mut user_manager = di_container.get::()?.transient()?; user_manager.fill_with_users(); println!("Printing user information"); user_manager.print_users(); + + Ok(()) } -- cgit v1.2.3-18-g5258