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 --- .../asynchronous/binding/scope_configurator.rs | 75 +++++++++++++++------- 1 file changed, 52 insertions(+), 23 deletions(-) (limited to 'src/di_container/asynchronous/binding/scope_configurator.rs') 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, -- cgit v1.2.3-18-g5258