pub struct ScopedContainer { /* private fields */ }Expand description
A container with an associated scope identifier.
Useful for request-scoped or session-scoped services where you need to track the scope identity.
§Examples
use dependency_injector::{Container, ScopedContainer};
#[derive(Clone)]
struct RequestContext {
request_id: String,
}
let root = Container::new();
// Create a request-scoped container
let request = ScopedContainer::from_parent(&root);
request.singleton(RequestContext {
request_id: "req-123".to_string(),
});
let ctx = request.get::<RequestContext>().unwrap();
assert_eq!(ctx.request_id, "req-123");Implementations§
Source§impl ScopedContainer
impl ScopedContainer
Sourcepub fn from_parent(parent: &Container) -> Self
pub fn from_parent(parent: &Container) -> Self
Create a scoped container from a parent container.
Sourcepub fn from_scope(parent: &ScopedContainer) -> Self
pub fn from_scope(parent: &ScopedContainer) -> Self
Create a scoped container from another scoped container.
Sourcepub fn singleton<T: Injectable>(&self, instance: T)
pub fn singleton<T: Injectable>(&self, instance: T)
Register a singleton in this scope.
Sourcepub fn lazy<T: Injectable, F>(&self, factory: F)
pub fn lazy<T: Injectable, F>(&self, factory: F)
Register a lazy singleton in this scope.
Sourcepub fn transient<T: Injectable, F>(&self, factory: F)
pub fn transient<T: Injectable, F>(&self, factory: F)
Register a transient service in this scope.
Sourcepub fn register<T: Injectable>(&self, instance: T)
pub fn register<T: Injectable>(&self, instance: T)
Alias for singleton - register an instance.
Sourcepub fn register_factory<T: Injectable, F>(&self, factory: F)
pub fn register_factory<T: Injectable, F>(&self, factory: F)
Register using a factory.
Sourcepub fn get<T: Injectable>(&self) -> Result<Arc<T>>
pub fn get<T: Injectable>(&self) -> Result<Arc<T>>
Resolve a service from this scope or parent scopes.
Sourcepub fn resolve<T: Injectable>(&self) -> Result<Arc<T>>
pub fn resolve<T: Injectable>(&self) -> Result<Arc<T>>
Alias for get.
Sourcepub fn try_get<T: Injectable>(&self) -> Option<Arc<T>>
pub fn try_get<T: Injectable>(&self) -> Option<Arc<T>>
Try to resolve a service, returning None if not found.
Sourcepub fn try_resolve<T: Injectable>(&self) -> Option<Arc<T>>
pub fn try_resolve<T: Injectable>(&self) -> Option<Arc<T>>
Alias for try_get.
Sourcepub fn contains<T: Injectable>(&self) -> bool
pub fn contains<T: Injectable>(&self) -> bool
Check if a service exists in this scope or parent scopes.
Sourcepub fn has<T: Injectable>(&self) -> bool
pub fn has<T: Injectable>(&self) -> bool
Alias for contains.
Sourcepub fn container_mut(&mut self) -> &mut Container
pub fn container_mut(&mut self) -> &mut Container
Get the underlying container mutably.
Trait Implementations§
Source§impl Debug for ScopedContainer
impl Debug for ScopedContainer
Auto Trait Implementations§
impl Freeze for ScopedContainer
impl !RefUnwindSafe for ScopedContainer
impl Send for ScopedContainer
impl Sync for ScopedContainer
impl Unpin for ScopedContainer
impl UnsafeUnpin for ScopedContainer
impl !UnwindSafe for ScopedContainer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Injectable for T
impl<T> Injectable for T
Source§fn type_id_of() -> TypeIdwhere
Self: Sized,
fn type_id_of() -> TypeIdwhere
Self: Sized,
Returns the TypeId of this type (for internal use)
Source§fn type_name_of() -> &'static strwhere
Self: Sized,
fn type_name_of() -> &'static strwhere
Self: Sized,
Returns the type name for debugging