From aa548ded39c7ba1927019c748c359523b21d59e8 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 29 Oct 2022 14:38:51 +0200 Subject: refactor!: add dependency history type BREAKING CHANGE: Binding builders & configurators now take dependency history type arguments, the DetectedCircular variant of InjectableError now contains a dependency history field & the injectable traits take dependency history instead of a Vec --- src/di_container/asynchronous/binding/builder.rs | 102 +++++++++++++++------ .../asynchronous/binding/scope_configurator.rs | 75 ++++++++++----- .../asynchronous/binding/when_configurator.rs | 18 +++- 3 files changed, 137 insertions(+), 58 deletions(-) (limited to 'src/di_container/asynchronous/binding') diff --git a/src/di_container/asynchronous/binding/builder.rs b/src/di_container/asynchronous/binding/builder.rs index f334cd7..3d03562 100644 --- a/src/di_container/asynchronous/binding/builder.rs +++ b/src/di_container/asynchronous/binding/builder.rs @@ -5,6 +5,7 @@ use std::any::type_name; use std::marker::PhantomData; use std::sync::Arc; +use crate::dependency_history::IDependencyHistory; use crate::di_container::asynchronous::binding::scope_configurator::AsyncBindingScopeConfigurator; #[cfg(feature = "factory")] use crate::di_container::asynchronous::binding::when_configurator::AsyncBindingWhenConfigurator; @@ -20,24 +21,33 @@ pub type BoxFn = Box<(dyn Fn + Send + Sync) /// Binding builder for type `Interface` inside a [`IAsyncDIContainer`]. /// /// [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer -pub struct AsyncBindingBuilder +pub struct AsyncBindingBuilder where Interface: 'static + ?Sized + Send + Sync, - DIContainerType: IAsyncDIContainer, + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync, { di_container: Arc, + dependency_history_factory: fn() -> DependencyHistoryType, + interface_phantom: PhantomData, } -impl AsyncBindingBuilder +impl + AsyncBindingBuilder where Interface: 'static + ?Sized + Send + Sync, - DIContainerType: IAsyncDIContainer, + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync + 'static, { - pub(crate) fn new(di_container: Arc) -> Self + pub(crate) fn new( + di_container: Arc, + dependency_history_factory: fn() -> DependencyHistoryType, + ) -> Self { Self { di_container, + dependency_history_factory, interface_phantom: PhantomData, } } @@ -88,11 +98,16 @@ where pub async fn to( &self, ) -> Result< - AsyncBindingScopeConfigurator, + AsyncBindingScopeConfigurator< + Interface, + Implementation, + DIContainerType, + DependencyHistoryType, + >, AsyncBindingBuilderError, > where - Implementation: AsyncInjectable, + Implementation: AsyncInjectable, { if self.di_container.has_binding::(None).await { return Err(AsyncBindingBuilderError::BindingAlreadyExists(type_name::< @@ -101,8 +116,10 @@ where ))); } - let binding_scope_configurator = - AsyncBindingScopeConfigurator::new(self.di_container.clone()); + let binding_scope_configurator = AsyncBindingScopeConfigurator::new( + self.di_container.clone(), + self.dependency_history_factory, + ); binding_scope_configurator.in_transient_scope().await; @@ -164,7 +181,7 @@ where &self, factory_func: &'static FactoryFunc, ) -> Result< - AsyncBindingWhenConfigurator, + AsyncBindingWhenConfigurator, AsyncBindingBuilderError, > where @@ -257,7 +274,7 @@ where &self, factory_func: &'static FactoryFunc, ) -> Result< - AsyncBindingWhenConfigurator, + AsyncBindingWhenConfigurator, AsyncBindingBuilderError, > where @@ -350,7 +367,7 @@ where &self, factory_func: &'static FactoryFunc, ) -> Result< - AsyncBindingWhenConfigurator, + AsyncBindingWhenConfigurator, AsyncBindingBuilderError, > where @@ -444,7 +461,7 @@ where &self, factory_func: &'static FactoryFunc, ) -> Result< - AsyncBindingWhenConfigurator, + AsyncBindingWhenConfigurator, AsyncBindingBuilderError, > where @@ -489,13 +506,14 @@ mod tests use mockall::predicate::eq; use super::*; - use crate::test_utils::mocks::async_di_container::MockAsyncDIContainer; - use crate::test_utils::subjects_async; + use crate::test_utils::{mocks, subjects_async}; #[tokio::test] async fn can_bind_to() -> Result<(), Box> { - let mut di_container_mock = MockAsyncDIContainer::new(); + let mut di_container_mock = mocks::async_di_container::MockAsyncDIContainer::< + mocks::MockDependencyHistory, + >::new(); di_container_mock .expect_has_binding::() @@ -511,8 +529,12 @@ mod tests let binding_builder = AsyncBindingBuilder::< dyn subjects_async::IUserManager, - MockAsyncDIContainer, - >::new(Arc::new(di_container_mock)); + mocks::async_di_container::MockAsyncDIContainer, + mocks::MockDependencyHistory, + >::new( + Arc::new(di_container_mock), + mocks::MockDependencyHistory::new, + ); binding_builder.to::().await?; @@ -534,7 +556,8 @@ mod tests subjects_async::Number, ) -> dyn subjects_async::IUserManager; - let mut di_container_mock = MockAsyncDIContainer::new(); + let mut di_container_mock = + mocks::async_di_container::MockAsyncDIContainer::new(); di_container_mock .expect_has_binding::() @@ -550,8 +573,12 @@ mod tests let binding_builder = AsyncBindingBuilder::< IUserManagerFactory, - MockAsyncDIContainer, - >::new(Arc::new(di_container_mock)); + mocks::async_di_container::MockAsyncDIContainer, + mocks::MockDependencyHistory, + >::new( + Arc::new(di_container_mock), + mocks::MockDependencyHistory::new, + ); binding_builder .to_factory(&|_| { @@ -577,7 +604,8 @@ mod tests #[factory(async = true)] type IUserManagerFactory = dyn Fn(String) -> dyn subjects_async::IUserManager; - let mut di_container_mock = MockAsyncDIContainer::new(); + let mut di_container_mock = + mocks::async_di_container::MockAsyncDIContainer::new(); di_container_mock .expect_has_binding::() @@ -593,8 +621,12 @@ mod tests let binding_builder = AsyncBindingBuilder::< IUserManagerFactory, - MockAsyncDIContainer, - >::new(Arc::new(di_container_mock)); + mocks::async_di_container::MockAsyncDIContainer, + mocks::MockDependencyHistory, + >::new( + Arc::new(di_container_mock), + mocks::MockDependencyHistory::new, + ); binding_builder .to_async_factory(&|_| { @@ -621,7 +653,8 @@ mod tests declare_default_factory!(dyn subjects_async::IUserManager); - let mut di_container_mock = MockAsyncDIContainer::new(); + let mut di_container_mock = + mocks::async_di_container::MockAsyncDIContainer::new(); di_container_mock .expect_has_binding::() @@ -637,8 +670,12 @@ mod tests let binding_builder = AsyncBindingBuilder::< dyn subjects_async::IUserManager, - MockAsyncDIContainer, - >::new(Arc::new(di_container_mock)); + mocks::async_di_container::MockAsyncDIContainer, + mocks::MockDependencyHistory, + >::new( + Arc::new(di_container_mock), + mocks::MockDependencyHistory::new, + ); binding_builder .to_default_factory(&|_| { @@ -665,7 +702,8 @@ mod tests declare_default_factory!(dyn subjects_async::IUserManager, async = true); - let mut di_container_mock = MockAsyncDIContainer::new(); + let mut di_container_mock = + mocks::async_di_container::MockAsyncDIContainer::new(); di_container_mock .expect_has_binding::() @@ -681,8 +719,12 @@ mod tests let binding_builder = AsyncBindingBuilder::< dyn subjects_async::IUserManager, - MockAsyncDIContainer, - >::new(Arc::new(di_container_mock)); + mocks::async_di_container::MockAsyncDIContainer, + mocks::MockDependencyHistory, + >::new( + Arc::new(di_container_mock), + mocks::MockDependencyHistory::new, + ); binding_builder .to_async_default_factory(&|_| { diff --git a/src/di_container/asynchronous/binding/scope_configurator.rs b/src/di_container/asynchronous/binding/scope_configurator.rs index e2e916a..b5923ec 100644 --- a/src/di_container/asynchronous/binding/scope_configurator.rs +++ b/src/di_container/asynchronous/binding/scope_configurator.rs @@ -4,6 +4,7 @@ use std::marker::PhantomData; use std::sync::Arc; +use crate::dependency_history::IDependencyHistory; use crate::di_container::asynchronous::binding::when_configurator::AsyncBindingWhenConfigurator; use crate::di_container::asynchronous::IAsyncDIContainer; use crate::errors::async_di_container::AsyncBindingScopeConfiguratorError; @@ -14,28 +15,45 @@ use crate::ptr::ThreadsafeSingletonPtr; /// Scope configurator for a binding for type 'Interface' inside a [`IAsyncDIContainer`]. /// /// [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer -pub struct AsyncBindingScopeConfigurator -where +pub struct AsyncBindingScopeConfigurator< + Interface, + Implementation, + DIContainerType, + DependencyHistoryType, +> where Interface: 'static + ?Sized + Send + Sync, - Implementation: AsyncInjectable, - DIContainerType: IAsyncDIContainer, + Implementation: AsyncInjectable, + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync, { di_container: Arc, + dependency_history_factory: fn() -> DependencyHistoryType, + interface_phantom: PhantomData, implementation_phantom: PhantomData, } -impl - AsyncBindingScopeConfigurator +impl + AsyncBindingScopeConfigurator< + Interface, + Implementation, + DIContainerType, + DependencyHistoryType, + > where Interface: 'static + ?Sized + Send + Sync, - Implementation: AsyncInjectable, - DIContainerType: IAsyncDIContainer, + Implementation: AsyncInjectable, + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync + 'static, { - pub(crate) fn new(di_container: Arc) -> Self + pub(crate) fn new( + di_container: Arc, + dependency_history_factory: fn() -> DependencyHistoryType, + ) -> Self { Self { di_container, + dependency_history_factory, interface_phantom: PhantomData, implementation_phantom: PhantomData, } @@ -46,14 +64,16 @@ where /// This is the default. pub async fn in_transient_scope( &self, - ) -> AsyncBindingWhenConfigurator + ) -> AsyncBindingWhenConfigurator { self.di_container .set_binding::( None, - Box::new( - AsyncTransientTypeProvider::::new(), - ), + Box::new(AsyncTransientTypeProvider::< + Implementation, + DIContainerType, + DependencyHistoryType, + >::new()), ) .await; @@ -67,17 +87,18 @@ where pub async fn in_singleton_scope( &self, ) -> Result< - AsyncBindingWhenConfigurator, + AsyncBindingWhenConfigurator, AsyncBindingScopeConfiguratorError, > { let singleton: ThreadsafeSingletonPtr = ThreadsafeSingletonPtr::from( - Implementation::resolve(&self.di_container, Vec::new()) - .await - .map_err( - AsyncBindingScopeConfiguratorError::SingletonResolveFailed, - )?, + Implementation::resolve( + &self.di_container, + (self.dependency_history_factory)(), + ) + .await + .map_err(AsyncBindingScopeConfiguratorError::SingletonResolveFailed)?, ); self.di_container @@ -112,8 +133,12 @@ mod tests let binding_scope_configurator = AsyncBindingScopeConfigurator::< dyn subjects_async::IUserManager, subjects_async::UserManager, - mocks::async_di_container::MockAsyncDIContainer, - >::new(Arc::new(di_container_mock)); + mocks::async_di_container::MockAsyncDIContainer, + mocks::MockDependencyHistory, + >::new( + Arc::new(di_container_mock), + mocks::MockDependencyHistory::new, + ); binding_scope_configurator.in_transient_scope().await; } @@ -133,8 +158,12 @@ mod tests let binding_scope_configurator = AsyncBindingScopeConfigurator::< dyn subjects_async::IUserManager, subjects_async::UserManager, - mocks::async_di_container::MockAsyncDIContainer, - >::new(Arc::new(di_container_mock)); + mocks::async_di_container::MockAsyncDIContainer, + mocks::MockDependencyHistory, + >::new( + Arc::new(di_container_mock), + mocks::MockDependencyHistory::new, + ); assert!(matches!( binding_scope_configurator.in_singleton_scope().await, diff --git a/src/di_container/asynchronous/binding/when_configurator.rs b/src/di_container/asynchronous/binding/when_configurator.rs index 9a1505b..4d56347 100644 --- a/src/di_container/asynchronous/binding/when_configurator.rs +++ b/src/di_container/asynchronous/binding/when_configurator.rs @@ -5,31 +5,38 @@ use std::any::type_name; use std::marker::PhantomData; use std::sync::Arc; +use crate::dependency_history::IDependencyHistory; use crate::di_container::asynchronous::IAsyncDIContainer; use crate::errors::async_di_container::AsyncBindingWhenConfiguratorError; /// When configurator for a binding for type 'Interface' inside a [`IAsyncDIContainer`]. /// /// [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer -pub struct AsyncBindingWhenConfigurator +pub struct AsyncBindingWhenConfigurator where Interface: 'static + ?Sized + Send + Sync, - DIContainerType: IAsyncDIContainer, + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync, { di_container: Arc, + interface_phantom: PhantomData, + dependency_history_phantom: PhantomData, } -impl AsyncBindingWhenConfigurator +impl + AsyncBindingWhenConfigurator where Interface: 'static + ?Sized + Send + Sync, - DIContainerType: IAsyncDIContainer, + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync, { pub(crate) fn new(di_container: Arc) -> Self { Self { di_container, interface_phantom: PhantomData, + dependency_history_phantom: PhantomData, } } @@ -92,7 +99,8 @@ mod tests let binding_when_configurator = AsyncBindingWhenConfigurator::< dyn subjects_async::INumber, - mocks::async_di_container::MockAsyncDIContainer, + mocks::async_di_container::MockAsyncDIContainer, + mocks::MockDependencyHistory, >::new(Arc::new(di_container_mock)); assert!(matches!( -- cgit v1.2.3-18-g5258