pub struct ServiceCollection { /* private fields */ }Expand description
Service collection — register services, then call .build().
Analogous to IServiceCollection in Microsoft.Extensions.DependencyInjection.
Implementations§
Source§impl ServiceCollection
impl ServiceCollection
pub fn new() -> ServiceCollection
pub fn singleton<T>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
pub fn transient<T>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
pub fn scoped<T>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
Sourcepub fn keyed_singleton<T>(
self,
k: impl Into<ServiceKey>,
f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static,
) -> ServiceCollection
pub fn keyed_singleton<T>( self, k: impl Into<ServiceKey>, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
Register a keyed singleton service.
String keys (keyed_singleton("name", …)) and typed keys
(keyed_singleton(ServiceKey::new(MyKey::A), …)) are both accepted.
Sourcepub fn keyed_transient<T>(
self,
k: impl Into<ServiceKey>,
f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static,
) -> ServiceCollection
pub fn keyed_transient<T>( self, k: impl Into<ServiceKey>, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
Register a keyed transient service (new instance each resolution).
Sourcepub fn keyed_scoped<T>(
self,
k: impl Into<ServiceKey>,
f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static,
) -> ServiceCollection
pub fn keyed_scoped<T>( self, k: impl Into<ServiceKey>, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
Register a keyed scoped service (shared within a scope).
Sourcepub fn keyed_any_singleton<T>(
self,
f: impl Fn(&dyn IServiceResolver, &ServiceKey) -> Arc<T> + Send + Sync + 'static,
) -> ServiceCollection
pub fn keyed_any_singleton<T>( self, f: impl Fn(&dyn IServiceResolver, &ServiceKey) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
Register an AnyKey fallback singleton (MEDI KeyedService.AnyKey).
The factory receives the requested key and one instance is created and cached per requested key. Exact-key registrations take precedence.
Sourcepub fn keyed_any_scoped<T>(
self,
f: impl Fn(&dyn IServiceResolver, &ServiceKey) -> Arc<T> + Send + Sync + 'static,
) -> ServiceCollection
pub fn keyed_any_scoped<T>( self, f: impl Fn(&dyn IServiceResolver, &ServiceKey) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
Register an AnyKey fallback scoped service (one instance per requested
key per scope).
Sourcepub fn keyed_any_transient<T>(
self,
f: impl Fn(&dyn IServiceResolver, &ServiceKey) -> Arc<T> + Send + Sync + 'static,
) -> ServiceCollection
pub fn keyed_any_transient<T>( self, f: impl Fn(&dyn IServiceResolver, &ServiceKey) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
Register an AnyKey fallback transient service (fresh instance per
resolution, receives the requested key).
Sourcepub fn async_singleton<T>(
self,
f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static,
) -> ServiceCollection
pub fn async_singleton<T>( self, f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static, ) -> ServiceCollection
Register an async singleton service (initialized once during build_async).
The factory receives Arc<ServiceProvider>. To resolve services from
within the factory, use get_async; to propagate the resolution context
to spawned tasks (enabling cross-task cycle detection), call
ServiceProvider::async_context.
Sourcepub fn async_transient<T>(
self,
f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static,
) -> ServiceCollection
pub fn async_transient<T>( self, f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static, ) -> ServiceCollection
Register an async transient service (new async instance each resolution).
Sourcepub fn async_scoped<T>(
self,
f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static,
) -> ServiceCollection
pub fn async_scoped<T>( self, f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static, ) -> ServiceCollection
Register an async scoped service (shared within a scope).
Sourcepub fn async_keyed_singleton<T>(
self,
k: impl Into<ServiceKey>,
f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static,
) -> ServiceCollection
pub fn async_keyed_singleton<T>( self, k: impl Into<ServiceKey>, f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static, ) -> ServiceCollection
Register an async keyed singleton service.
Sourcepub fn async_keyed_transient<T>(
self,
k: impl Into<ServiceKey>,
f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static,
) -> ServiceCollection
pub fn async_keyed_transient<T>( self, k: impl Into<ServiceKey>, f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static, ) -> ServiceCollection
Register an async keyed transient service.
Sourcepub fn async_keyed_scoped<T>(
self,
k: impl Into<ServiceKey>,
f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static,
) -> ServiceCollection
pub fn async_keyed_scoped<T>( self, k: impl Into<ServiceKey>, f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static, ) -> ServiceCollection
Register an async keyed scoped service.
pub fn try_add<T>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
Sourcepub fn try_add_singleton<T>(
self,
f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static,
) -> ServiceCollection
pub fn try_add_singleton<T>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
Register a singleton only if no unkeyed registration for T exists yet.
Sourcepub fn try_add_scoped<T>(
self,
f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static,
) -> ServiceCollection
pub fn try_add_scoped<T>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
Register a scoped service only if no unkeyed registration for T exists yet.
Sourcepub fn try_add_transient<T>(
self,
f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static,
) -> ServiceCollection
pub fn try_add_transient<T>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
Register a transient service only if no unkeyed registration for T exists yet.
pub fn add<T>( self, lt: ServiceLifetime, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> ServiceCollection
pub fn instance<T>(self, v: Arc<T>) -> ServiceCollection
pub fn singleton_value<T>(self, v: T) -> ServiceCollection
pub fn build(self) -> Result<Arc<ServiceProvider>, RdiError>
Sourcepub fn build_with(
self,
options: ServiceProviderOptions,
) -> Result<Arc<ServiceProvider>, RdiError>
pub fn build_with( self, options: ServiceProviderOptions, ) -> Result<Arc<ServiceProvider>, RdiError>
Build with explicit ServiceProviderOptions.
build() equals build_with(ServiceProviderOptions::default()), i.e.
strict scope validation is enabled by default (MEDI ValidateScopes).
Use crate::ServiceProviderOptions::lenient to restore the ≤ 0.7
root-scope behavior where the root provider caches scoped services.
Sourcepub async fn build_async(self) -> Result<Arc<ServiceProvider>, RdiError>
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.
Use this instead of build when any service requires
async initialization (e.g., database connections, remote config).
Sourcepub async fn build_async_with(
self,
options: ServiceProviderOptions,
) -> Result<Arc<ServiceProvider>, RdiError>
pub async fn build_async_with( self, options: ServiceProviderOptions, ) -> Result<Arc<ServiceProvider>, RdiError>
Async variant of build_with.
Sourcepub fn from_injected() -> ServiceCollection
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
impl DbContextServiceCollectionExt for ServiceCollection
Source§fn add_dbcontext(
self,
configure: impl FnOnce(&mut DbContextOptionsBuilder) + Send + Sync + 'static,
) -> Self
fn add_dbcontext( self, configure: impl FnOnce(&mut DbContextOptionsBuilder) + Send + Sync + 'static, ) -> Self
DbContext as Scoped with the default key. Read moreSource§fn add_dbcontext_keyed(
self,
key: impl IDbContextKey + Into<ServiceKey>,
configure: impl FnOnce(&mut DbContextOptionsBuilder) + Send + Sync + 'static,
) -> Self
fn add_dbcontext_keyed( self, key: impl IDbContextKey + Into<ServiceKey>, configure: impl FnOnce(&mut DbContextOptionsBuilder) + Send + Sync + 'static, ) -> Self
DbContext as Scoped. Read moreSource§fn add_dbcontext_anykey(
self,
configure: impl Fn(&ServiceKey, &mut DbContextOptionsBuilder) + Send + Sync + 'static,
) -> Self
fn add_dbcontext_anykey( self, configure: impl Fn(&ServiceKey, &mut DbContextOptionsBuilder) + Send + Sync + 'static, ) -> Self
DbContext for every requested key without an
exact registration (rust-dix AnyKey). Read moreSource§fn add_dbcontext_anykey_with_capacity(
self,
capacity: usize,
configure: impl Fn(&ServiceKey, &mut DbContextOptionsBuilder) + Send + Sync + 'static,
) -> Self
fn add_dbcontext_anykey_with_capacity( self, capacity: usize, configure: impl Fn(&ServiceKey, &mut DbContextOptionsBuilder) + Send + Sync + 'static, ) -> Self
Self::add_dbcontext_anykey with an explicit LRU capacity. Read moreSource§fn add_dbcontext_async(
self,
configure: impl FnOnce(&mut DbContextOptionsBuilder) + Send + Sync + 'static,
) -> Self
fn add_dbcontext_async( self, configure: impl FnOnce(&mut DbContextOptionsBuilder) + Send + Sync + 'static, ) -> Self
DbContext with async-initialized provider
options. Read moreSource§fn add_dbcontext_keyed_async(
self,
key: impl IDbContextKey + Into<ServiceKey>,
configure: impl FnOnce(&mut DbContextOptionsBuilder) + Send + Sync + 'static,
) -> Self
fn add_dbcontext_keyed_async( self, key: impl IDbContextKey + Into<ServiceKey>, configure: impl FnOnce(&mut DbContextOptionsBuilder) + Send + Sync + 'static, ) -> Self
Self::add_dbcontext_async.