Skip to main content

ServiceCollection

Struct ServiceCollection 

Source
pub struct ServiceCollection { /* private fields */ }

Implementations§

Source§

impl ServiceCollection

Source

pub fn new() -> ServiceCollection

Source

pub fn singleton<T>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
where T: Send + Sync + 'static + ?Sized,

Source

pub fn transient<T>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
where T: Send + Sync + 'static + ?Sized,

Source

pub fn scoped<T>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
where T: Send + Sync + 'static + ?Sized,

Source

pub fn keyed_singleton<T>( self, k: impl Into<String>, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
where T: Send + Sync + 'static + ?Sized,

Register a keyed singleton service.

The key distinguishes multiple implementations of the same trait. Use get_keyed::<T>(key) or try_get_keyed::<T>(key) to resolve.

Source

pub fn keyed_transient<T>( self, k: impl Into<String>, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
where T: Send + Sync + 'static + ?Sized,

Register a keyed transient service (new instance each resolution).

Source

pub fn keyed_scoped<T>( self, k: impl Into<String>, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
where T: Send + Sync + 'static + ?Sized,

Register a keyed scoped service (shared within a scope).

Source

pub fn async_singleton<T>( self, f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static, ) -> ServiceCollection
where T: Send + Sync + 'static + ?Sized,

Register an async singleton service (initialized once during build_async).

Source

pub fn async_transient<T>( self, f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static, ) -> ServiceCollection
where T: Send + Sync + 'static + ?Sized,

Register an async transient service (new async instance each resolution).

Source

pub fn async_scoped<T>( self, f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static, ) -> ServiceCollection
where T: Send + Sync + 'static + ?Sized,

Register an async scoped service (shared within a scope).

Source

pub fn async_keyed_singleton<T>( self, k: impl Into<String>, f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static, ) -> ServiceCollection
where T: Send + Sync + 'static + ?Sized,

Register an async keyed singleton service.

Source

pub fn async_keyed_transient<T>( self, k: impl Into<String>, f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static, ) -> ServiceCollection
where T: Send + Sync + 'static + ?Sized,

Register an async keyed transient service.

Source

pub fn async_keyed_scoped<T>( self, k: impl Into<String>, f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static, ) -> ServiceCollection
where T: Send + Sync + 'static + ?Sized,

Register an async keyed scoped service.

Source

pub fn try_add<T>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
where T: Send + Sync + 'static + ?Sized,

Source

pub fn add<T>( self, lt: ServiceLifetime, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
where T: Send + Sync + 'static + ?Sized,

Source

pub fn instance<T>(self, v: Arc<T>) -> ServiceCollection
where T: Send + Sync + 'static,

Source

pub fn singleton_value<T>(self, v: T) -> ServiceCollection
where T: Send + Sync + 'static,

Source

pub fn build(self) -> Result<Arc<ServiceProvider>, RdiError>

Source

pub async fn build_async(self) -> Result<Arc<ServiceProvider>, RdiError>

Build the provider, executing async factories for singleton services.

Async singleton factories are run during build and their results are cached as singleton instances. Async transient/scoped factories are stored for later resolution.

Returns Arc<ServiceProvider> so that provider_arc() works for async resolution via get_async / get_keyed_async.

Use this instead of build when any service requires async initialization (e.g., database connections, remote config).

Source

pub fn from_injected() -> ServiceCollection

Build a ServiceCollection from all #[rust_dix::inject] annotations in the current binary (across all crates).

Trait Implementations§

Source§

impl DbContextServiceCollectionExt for ServiceCollection

Source§

fn add_dbcontext( self, configure: impl FnOnce(&mut DbContextOptionsBuilder) + Send + Sync + 'static, ) -> Self

Registers a DbContext as scoped with default key. Read more
Source§

fn add_dbcontext_keyed( self, key: &str, configure: impl FnOnce(&mut DbContextOptionsBuilder) + Send + Sync + 'static, ) -> Self

Registers a keyed DbContext as scoped. Read more
Source§

impl Default for ServiceCollection

Source§

fn default() -> ServiceCollection

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

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.