ContainerBuilder

Struct ContainerBuilder 

Source
pub struct ContainerBuilder { /* private fields */ }
Expand description

Container builder - provides a fluent API for service registration

Implementations§

Source§

impl ContainerBuilder

Source

pub fn new() -> Self

Create a new container builder

Source

pub fn add_transient<TService, TImplementation>( self, factory: impl Fn(&dyn ServiceProvider) -> DiResult<TImplementation> + Send + Sync + 'static, ) -> Self
where TService: 'static, TImplementation: Send + Sync + 'static,

Register a transient service

Source

pub fn add_transient_self<T>( self, factory: impl Fn(&dyn ServiceProvider) -> DiResult<T> + Send + Sync + 'static, ) -> Self
where T: Send + Sync + 'static,

Register a transient service (self-registration)

Source

pub fn add_transient_simple<TService, TImplementation>( self, factory: impl Fn() -> TImplementation + Send + Sync + 'static, ) -> Self
where TService: 'static, TImplementation: Send + Sync + 'static,

Register a transient service (no dependencies)

Source

pub fn add_scoped<TService, TImplementation>( self, factory: impl Fn(&dyn ServiceProvider) -> DiResult<TImplementation> + Send + Sync + 'static, ) -> Self
where TService: 'static, TImplementation: Send + Sync + 'static,

Register a scoped service

Source

pub fn add_scoped_self<T>( self, factory: impl Fn(&dyn ServiceProvider) -> DiResult<T> + Send + Sync + 'static, ) -> Self
where T: Send + Sync + 'static,

Register a scoped service (self-registration)

Source

pub fn add_scoped_simple<TService, TImplementation>( self, factory: impl Fn() -> TImplementation + Send + Sync + 'static, ) -> Self
where TService: 'static, TImplementation: Send + Sync + 'static,

Register a scoped service (no dependencies)

Source

pub fn add_singleton<TService, TImplementation>( self, factory: impl Fn(&dyn ServiceProvider) -> DiResult<TImplementation> + Send + Sync + 'static, ) -> Self
where TService: 'static, TImplementation: Send + Sync + 'static,

Register a singleton service

Source

pub fn add_singleton_self<T>( self, factory: impl Fn(&dyn ServiceProvider) -> DiResult<T> + Send + Sync + 'static, ) -> Self
where T: Send + Sync + 'static,

Register a singleton service (self-registration)

Source

pub fn add_singleton_simple<TService, TImplementation>( self, factory: impl Fn() -> TImplementation + Send + Sync + 'static, ) -> Self
where TService: 'static, TImplementation: Send + Sync + 'static,

Register a singleton service (no dependencies)

Source

pub fn add_instance<T>(self, instance: T) -> Self
where T: Send + Sync + 'static,

Register a singleton instance

Source

pub fn add_named_transient<TService, TImplementation>( self, name: impl Into<String>, factory: impl Fn(&dyn ServiceProvider) -> DiResult<TImplementation> + Send + Sync + 'static, ) -> Self
where TService: 'static, TImplementation: Send + Sync + 'static,

Register a named transient service

Source

pub fn add_named_transient_self<T>( self, name: impl Into<String>, factory: impl Fn(&dyn ServiceProvider) -> DiResult<T> + Send + Sync + 'static, ) -> Self
where T: Send + Sync + 'static,

Register a named transient service (self-registration)

Source

pub fn add_named_transient_simple<TService, TImplementation>( self, name: impl Into<String>, factory: impl Fn() -> TImplementation + Send + Sync + 'static, ) -> Self
where TService: 'static, TImplementation: Send + Sync + 'static,

Register a named transient service (no dependencies)

Source

pub fn add_named_scoped<TService, TImplementation>( self, name: impl Into<String>, factory: impl Fn(&dyn ServiceProvider) -> DiResult<TImplementation> + Send + Sync + 'static, ) -> Self
where TService: 'static, TImplementation: Send + Sync + 'static,

Register a named scoped service

Source

pub fn add_named_scoped_self<T>( self, name: impl Into<String>, factory: impl Fn(&dyn ServiceProvider) -> DiResult<T> + Send + Sync + 'static, ) -> Self
where T: Send + Sync + 'static,

Register a named scoped service (self-registration)

Source

pub fn add_named_scoped_simple<TService, TImplementation>( self, name: impl Into<String>, factory: impl Fn() -> TImplementation + Send + Sync + 'static, ) -> Self
where TService: 'static, TImplementation: Send + Sync + 'static,

Register a named scoped service (no dependencies)

Source

pub fn add_named_singleton<TService, TImplementation>( self, name: impl Into<String>, factory: impl Fn(&dyn ServiceProvider) -> DiResult<TImplementation> + Send + Sync + 'static, ) -> Self
where TService: 'static, TImplementation: Send + Sync + 'static,

Register a named singleton service

Source

pub fn add_named_singleton_self<T>( self, name: impl Into<String>, factory: impl Fn(&dyn ServiceProvider) -> DiResult<T> + Send + Sync + 'static, ) -> Self
where T: Send + Sync + 'static,

Register a named singleton service (self-registration)

Source

pub fn add_named_singleton_simple<TService, TImplementation>( self, name: impl Into<String>, factory: impl Fn() -> TImplementation + Send + Sync + 'static, ) -> Self
where TService: 'static, TImplementation: Send + Sync + 'static,

Register a named singleton service (no dependencies)

Source

pub fn add_named_instance<T>(self, name: impl Into<String>, instance: T) -> Self
where T: Send + Sync + 'static,

Register a named singleton instance

Source

pub fn add_transient_with_deps<TService, TImplementation, TDep1>( self, factory: impl Fn(Arc<TDep1>) -> TImplementation + Send + Sync + 'static, ) -> Self
where TService: 'static, TImplementation: Send + Sync + 'static, TDep1: 'static + Send + Sync,

Register a transient service with dependency injection

Source

pub fn add_transient_with_deps2<TService, TImplementation, TDep1, TDep2>( self, factory: impl Fn(Arc<TDep1>, Arc<TDep2>) -> TImplementation + Send + Sync + 'static, ) -> Self
where TService: 'static, TImplementation: Send + Sync + 'static, TDep1: 'static + Send + Sync, TDep2: 'static + Send + Sync,

Register a transient service with two dependencies

Source

pub fn add_scoped_with_deps<TService, TImplementation, TDep1>( self, factory: impl Fn(Arc<TDep1>) -> TImplementation + Send + Sync + 'static, ) -> Self
where TService: 'static, TImplementation: Send + Sync + 'static, TDep1: 'static + Send + Sync,

Register a scoped service with dependency injection

Source

pub fn add_scoped_with_deps2<TService, TImplementation, TDep1, TDep2>( self, factory: impl Fn(Arc<TDep1>, Arc<TDep2>) -> TImplementation + Send + Sync + 'static, ) -> Self
where TService: 'static, TImplementation: Send + Sync + 'static, TDep1: 'static + Send + Sync, TDep2: 'static + Send + Sync,

Register a scoped service with two dependencies

Source

pub fn add_singleton_with_deps<TService, TImplementation, TDep1>( self, factory: impl Fn(Arc<TDep1>) -> TImplementation + Send + Sync + 'static, ) -> Self
where TService: 'static, TImplementation: Send + Sync + 'static, TDep1: 'static + Send + Sync,

Register a singleton service with dependency injection

Source

pub fn add_singleton_with_deps2<TService, TImplementation, TDep1, TDep2>( self, factory: impl Fn(Arc<TDep1>, Arc<TDep2>) -> TImplementation + Send + Sync + 'static, ) -> Self
where TService: 'static, TImplementation: Send + Sync + 'static, TDep1: 'static + Send + Sync, TDep2: 'static + Send + Sync,

Register a singleton service with two dependencies

Source

pub fn decorate<TService>( self, _decorator: impl Fn(&dyn ServiceProvider, Arc<TService>) -> DiResult<TService> + Send + Sync + 'static, ) -> Self
where TService: Send + Sync + 'static,

Decorator pattern - decorate existing services

Source

pub fn add_conditional<TService, TImplementation>( self, condition: bool, lifetime: Lifetime, factory: impl Fn(&dyn ServiceProvider) -> DiResult<TImplementation> + Send + Sync + 'static, ) -> Self
where TService: 'static, TImplementation: Send + Sync + 'static,

Conditional registration - only register service when condition is met

Source

pub fn add_services(self, services: Vec<ServiceDescriptor>) -> Self

Register multiple services at once

Source

pub fn build(self) -> ServiceProvider

Build the container

Source

pub fn container(&self) -> &Container

Get reference to the internal container (for advanced operations)

Trait Implementations§

Source§

impl Default for ContainerBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.