diff options
author | HampusM <hampus@hampusmat.com> | 2023-08-31 19:19:06 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-08-31 19:19:06 +0200 |
commit | 7bed48c852a741df5a14359916faf21d90d39814 (patch) | |
tree | 5cc94835225d356ed658cf78a99deeb1b4e730f8 /src/di_container/asynchronous | |
parent | 0b4232d343e2214ead8fa62583bff2e948173ddf (diff) |
refactor: pass around BindingOptions instead of name
Diffstat (limited to 'src/di_container/asynchronous')
4 files changed, 91 insertions, 49 deletions
diff --git a/src/di_container/asynchronous/binding/builder.rs b/src/di_container/asynchronous/binding/builder.rs index 5862d63..83a1efb 100644 --- a/src/di_container/asynchronous/binding/builder.rs +++ b/src/di_container/asynchronous/binding/builder.rs @@ -9,6 +9,7 @@ use crate::di_container::asynchronous::binding::scope_configurator::AsyncBinding #[cfg(feature = "factory")] use crate::di_container::asynchronous::binding::when_configurator::AsyncBindingWhenConfigurator; use crate::di_container::asynchronous::IAsyncDIContainer; +use crate::di_container::BindingOptions; use crate::errors::async_di_container::AsyncBindingBuilderError; use crate::interfaces::async_injectable::AsyncInjectable; use crate::util::use_double; @@ -104,7 +105,11 @@ where where Implementation: AsyncInjectable<DIContainerType>, { - if self.di_container.has_binding::<Interface>(None).await { + if self + .di_container + .has_binding::<Interface>(BindingOptions::new()) + .await + { return Err(AsyncBindingBuilderError::BindingAlreadyExists(type_name::< Interface, >( @@ -189,7 +194,11 @@ where use crate::private::castable_factory::threadsafe::ThreadsafeCastableFactory; use crate::provider::r#async::AsyncFactoryVariant; - if self.di_container.has_binding::<Interface>(None).await { + if self + .di_container + .has_binding::<Interface>(BindingOptions::new()) + .await + { return Err(AsyncBindingBuilderError::BindingAlreadyExists(type_name::< Interface, >( @@ -200,7 +209,7 @@ where self.di_container .set_binding::<Interface>( - None, + BindingOptions::new(), Box::new(crate::provider::r#async::AsyncFactoryProvider::new( crate::ptr::ThreadsafeFactoryPtr::new(factory_impl), AsyncFactoryVariant::Normal, @@ -292,7 +301,11 @@ where use crate::private::castable_factory::threadsafe::ThreadsafeCastableFactory; use crate::provider::r#async::AsyncFactoryVariant; - if self.di_container.has_binding::<Interface>(None).await { + if self + .di_container + .has_binding::<Interface>(BindingOptions::new()) + .await + { return Err(AsyncBindingBuilderError::BindingAlreadyExists(type_name::< Interface, >( @@ -303,7 +316,7 @@ where self.di_container .set_binding::<Interface>( - None, + BindingOptions::new(), Box::new(crate::provider::r#async::AsyncFactoryProvider::new( crate::ptr::ThreadsafeFactoryPtr::new(factory_impl), AsyncFactoryVariant::Normal, @@ -382,7 +395,11 @@ where use crate::private::castable_factory::threadsafe::ThreadsafeCastableFactory; use crate::provider::r#async::AsyncFactoryVariant; - if self.di_container.has_binding::<Interface>(None).await { + if self + .di_container + .has_binding::<Interface>(BindingOptions::new()) + .await + { return Err(AsyncBindingBuilderError::BindingAlreadyExists(type_name::< Interface, >( @@ -393,7 +410,7 @@ where self.di_container .set_binding::<Interface>( - None, + BindingOptions::new(), Box::new(crate::provider::r#async::AsyncFactoryProvider::new( crate::ptr::ThreadsafeFactoryPtr::new(factory_impl), AsyncFactoryVariant::Default, @@ -477,7 +494,11 @@ where use crate::private::castable_factory::threadsafe::ThreadsafeCastableFactory; use crate::provider::r#async::AsyncFactoryVariant; - if self.di_container.has_binding::<Interface>(None).await { + if self + .di_container + .has_binding::<Interface>(BindingOptions::new()) + .await + { return Err(AsyncBindingBuilderError::BindingAlreadyExists(type_name::< Interface, >( @@ -488,7 +509,7 @@ where self.di_container .set_binding::<Interface>( - None, + BindingOptions::new(), Box::new(crate::provider::r#async::AsyncFactoryProvider::new( crate::ptr::ThreadsafeFactoryPtr::new(factory_impl), AsyncFactoryVariant::AsyncDefault, @@ -519,13 +540,13 @@ mod tests di_container_mock .expect_has_binding::<dyn subjects_async::IUserManager>() - .with(eq(None)) + .with(eq(BindingOptions::new())) .return_once(|_name| false) .once(); di_container_mock .expect_set_binding::<dyn subjects_async::IUserManager>() - .withf(|name, _provider| name.is_none()) + .withf(|binding_options, _provider| binding_options.name.is_none()) .return_once(|_name, _provider| ()) .once(); @@ -562,13 +583,13 @@ mod tests di_container_mock .expect_has_binding::<IUserManagerFactory>() - .with(eq(None)) + .with(eq(BindingOptions::new())) .return_once(|_name| false) .once(); di_container_mock .expect_set_binding::<IUserManagerFactory>() - .withf(|name, _provider| name.is_none()) + .withf(|binding_options, _provider| binding_options.name.is_none()) .return_once(|_name, _provider| ()) .once(); @@ -613,13 +634,13 @@ mod tests di_container_mock .expect_has_binding::<IUserManagerFactory>() - .with(eq(None)) + .with(eq(BindingOptions::new())) .return_once(|_name| false) .once(); di_container_mock .expect_set_binding::<IUserManagerFactory>() - .withf(|name, _provider| name.is_none()) + .withf(|binding_options, _provider| binding_options.name.is_none()) .return_once(|_name, _provider| ()) .once(); @@ -659,13 +680,13 @@ mod tests di_container_mock .expect_has_binding::<dyn subjects_async::IUserManager>() - .with(eq(None)) + .with(eq(BindingOptions::new())) .return_once(|_name| false) .once(); di_container_mock .expect_set_binding::<dyn subjects_async::IUserManager>() - .withf(|name, _provider| name.is_none()) + .withf(|binding_options, _provider| binding_options.name.is_none()) .return_once(|_name, _provider| ()) .once(); @@ -706,13 +727,13 @@ mod tests di_container_mock .expect_has_binding::<dyn subjects_async::IUserManager>() - .with(eq(None)) + .with(eq(BindingOptions::new())) .return_once(|_name| false) .once(); di_container_mock .expect_set_binding::<dyn subjects_async::IUserManager>() - .withf(|name, _provider| name.is_none()) + .withf(|binding_options, _provider| binding_options.name.is_none()) .return_once(|_name, _provider| ()) .once(); diff --git a/src/di_container/asynchronous/binding/scope_configurator.rs b/src/di_container/asynchronous/binding/scope_configurator.rs index 0b5bad8..f10bb48 100644 --- a/src/di_container/asynchronous/binding/scope_configurator.rs +++ b/src/di_container/asynchronous/binding/scope_configurator.rs @@ -6,6 +6,7 @@ use std::sync::Arc; use crate::di_container::asynchronous::binding::when_configurator::AsyncBindingWhenConfigurator; use crate::di_container::asynchronous::IAsyncDIContainer; +use crate::di_container::BindingOptions; use crate::errors::async_di_container::AsyncBindingScopeConfiguratorError; use crate::interfaces::async_injectable::AsyncInjectable; use crate::provider::r#async::{AsyncSingletonProvider, AsyncTransientTypeProvider}; @@ -85,7 +86,7 @@ where self.di_container .set_binding::<Interface>( - None, + BindingOptions::new(), Box::new(AsyncSingletonProvider::new(singleton)), ) .await; @@ -97,7 +98,7 @@ where { self.di_container .set_binding::<Interface>( - None, + BindingOptions::new(), Box::new( AsyncTransientTypeProvider::<Implementation, DIContainerType>::new(), ), @@ -121,7 +122,7 @@ mod tests di_container_mock .expect_set_binding::<dyn subjects_async::IUserManager>() - .withf(|name, _provider| name.is_none()) + .withf(|binding_options, _provider| binding_options.name.is_none()) .return_once(|_name, _provider| ()) .once(); @@ -143,7 +144,7 @@ mod tests di_container_mock .expect_set_binding::<dyn subjects_async::IUserManager>() - .withf(|name, _provider| name.is_none()) + .withf(|binding_options, _provider| binding_options.name.is_none()) .return_once(|_name, _provider| ()) .once(); diff --git a/src/di_container/asynchronous/binding/when_configurator.rs b/src/di_container/asynchronous/binding/when_configurator.rs index d08239e..4521178 100644 --- a/src/di_container/asynchronous/binding/when_configurator.rs +++ b/src/di_container/asynchronous/binding/when_configurator.rs @@ -6,6 +6,7 @@ use std::marker::PhantomData; use std::sync::Arc; use crate::di_container::asynchronous::IAsyncDIContainer; +use crate::di_container::BindingOptions; use crate::errors::async_di_container::AsyncBindingWhenConfiguratorError; /// When configurator for a binding for type `Interface` inside a [`IAsyncDIContainer`]. @@ -45,7 +46,7 @@ where { let binding = self .di_container - .remove_binding::<Interface>(None) + .remove_binding::<Interface>(BindingOptions::new()) .await .map_or_else( || { @@ -57,7 +58,7 @@ where )?; self.di_container - .set_binding::<Interface>(Some(name), binding) + .set_binding::<Interface>(BindingOptions::new().name(name), binding) .await; Ok(()) @@ -81,13 +82,13 @@ mod tests di_container_mock .expect_remove_binding::<dyn subjects_async::INumber>() - .with(eq(None)) + .with(eq(BindingOptions::new())) .return_once(|_name| Some(Box::new(MockIAsyncProvider::new()))) .once(); di_container_mock .expect_set_binding::<dyn subjects_async::INumber>() - .withf(|name, _provider| name == &Some("awesome")) + .withf(|binding_options, _provider| binding_options.name == Some("awesome")) .return_once(|_name, _provider| ()) .once(); diff --git a/src/di_container/asynchronous/mod.rs b/src/di_container/asynchronous/mod.rs index e5f7f5d..8d67b99 100644 --- a/src/di_container/asynchronous/mod.rs +++ b/src/di_container/asynchronous/mod.rs @@ -235,10 +235,7 @@ impl IAsyncDIContainer for AsyncDIContainer { Box::pin(async move { let binding_providable = self - .get_binding_providable::<Interface>( - binding_options.name, - dependency_history, - ) + .get_binding_providable::<Interface>(binding_options, dependency_history) .await?; self.handle_binding_providable(binding_providable).await @@ -249,16 +246,22 @@ impl IAsyncDIContainer for AsyncDIContainer #[async_trait] impl details::DIContainerInternals for AsyncDIContainer { - async fn has_binding<Interface>(self: &Arc<Self>, name: Option<&'static str>) -> bool + async fn has_binding<Interface>( + self: &Arc<Self>, + binding_options: BindingOptions<'static>, + ) -> bool where Interface: ?Sized + 'static, { - self.binding_storage.lock().await.has::<Interface>(name) + self.binding_storage + .lock() + .await + .has::<Interface>(binding_options) } async fn set_binding<Interface>( self: &Arc<Self>, - name: Option<&'static str>, + binding_options: BindingOptions<'static>, provider: Box<dyn IAsyncProvider<Self>>, ) where Interface: 'static + ?Sized, @@ -266,17 +269,20 @@ impl details::DIContainerInternals for AsyncDIContainer self.binding_storage .lock() .await - .set::<Interface>(name, provider); + .set::<Interface>(binding_options, provider); } async fn remove_binding<Interface>( self: &Arc<Self>, - name: Option<&'static str>, + binding_options: BindingOptions<'static>, ) -> Option<Box<dyn IAsyncProvider<Self>>> where Interface: 'static + ?Sized, { - self.binding_storage.lock().await.remove::<Interface>(name) + self.binding_storage + .lock() + .await + .remove::<Interface>(binding_options) } } @@ -411,7 +417,7 @@ impl AsyncDIContainer async fn get_binding_providable<Interface>( self: &Arc<Self>, - name: Option<&'static str>, + binding_options: BindingOptions<'static>, dependency_history: DependencyHistory, ) -> Result<AsyncProvidable<Self>, AsyncDIContainerError> where @@ -423,12 +429,12 @@ impl AsyncDIContainer let bindings_lock = self.binding_storage.lock().await; provider = bindings_lock - .get::<Interface>(name) + .get::<Interface>(binding_options.clone()) .map_or_else( || { Err(AsyncDIContainerError::BindingNotFound { interface: type_name::<Interface>(), - name, + name: binding_options.name, }) }, Ok, @@ -452,6 +458,7 @@ pub(crate) mod details use async_trait::async_trait; + use crate::di_container::BindingOptions; use crate::provider::r#async::IAsyncProvider; #[async_trait] @@ -459,21 +466,21 @@ pub(crate) mod details { async fn has_binding<Interface>( self: &Arc<Self>, - name: Option<&'static str>, + binding_options: BindingOptions<'static>, ) -> bool where Interface: ?Sized + 'static; async fn set_binding<Interface>( self: &Arc<Self>, - name: Option<&'static str>, + binding_options: BindingOptions<'static>, provider: Box<dyn IAsyncProvider<Self>>, ) where Interface: 'static + ?Sized; async fn remove_binding<Interface>( self: &Arc<Self>, - name: Option<&'static str>, + binding_options: BindingOptions<'static>, ) -> Option<Box<dyn IAsyncProvider<Self>>> where Interface: 'static + ?Sized; @@ -514,7 +521,10 @@ mod tests .binding_storage .lock() .await - .set::<dyn subjects_async::IUserManager>(None, Box::new(mock_provider)); + .set::<dyn subjects_async::IUserManager>( + BindingOptions::new(), + Box::new(mock_provider), + ); } di_container @@ -550,7 +560,7 @@ mod tests .lock() .await .set::<dyn subjects_async::IUserManager>( - Some("special"), + BindingOptions::new().name("special"), Box::new(mock_provider), ); } @@ -591,7 +601,10 @@ mod tests .binding_storage .lock() .await - .set::<dyn subjects_async::INumber>(None, Box::new(mock_provider)); + .set::<dyn subjects_async::INumber>( + BindingOptions::new(), + Box::new(mock_provider), + ); } let first_number_rc = di_container @@ -640,7 +653,7 @@ mod tests .lock() .await .set::<dyn subjects_async::INumber>( - Some("cool"), + BindingOptions::new().name("cool"), Box::new(mock_provider), ); } @@ -747,7 +760,10 @@ mod tests .binding_storage .lock() .await - .set::<IUserManagerFactory>(None, Box::new(mock_provider)); + .set::<IUserManagerFactory>( + BindingOptions::new(), + Box::new(mock_provider), + ); } di_container @@ -843,7 +859,10 @@ mod tests .binding_storage .lock() .await - .set::<IUserManagerFactory>(Some("special"), Box::new(mock_provider)); + .set::<IUserManagerFactory>( + BindingOptions::new().name("special"), + Box::new(mock_provider), + ); } di_container |