Skip to main content

DbContextServiceCollectionExt

Trait DbContextServiceCollectionExt 

Source
pub trait DbContextServiceCollectionExt {
    // Required methods
    fn add_dbcontext(
        self,
        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;
    fn add_dbcontext_anykey(
        self,
        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;
    fn add_dbcontext_async(
        self,
        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;
}
Expand description

Adds the DbContext registration helpers to rust_dix::ServiceCollection.

Required Methods§

Source

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

Registers a DbContext as Scoped with the default key.

The provider (connection pool) is created eagerly at registration time and a failure panics, fail-fast at startup. A later registration for the same key replaces this one (MEDI last-registration-wins).

Source

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

Registers a typed/keyed DbContext as Scoped.

key can be an enum deriving DbContextKey (which implements IDbContextKey and Into<ServiceKey>) or a &str/String (source-compatible; registered as a String key). The key’s name() is used for the entity model.

Source

fn add_dbcontext_anykey( self, configure: impl Fn(&ServiceKey, &mut DbContextOptionsBuilder) + Send + Sync + 'static, ) -> Self

Registers one fallback DbContext for every requested key without an exact registration (rust-dix AnyKey).

The configure closure receives the requested ServiceKey and builds per-key options. Built options/providers are cached per requested key (bounded LRU, DEFAULT_ANYKEY_CAPACITY entries), so connection pools are reused across scopes. The context always uses use_default_entity_model(); isolate tenants with query filters.

Security: map the requested key through an allow-list/tenant table; never interpolate it directly into SQL or connection strings.

Source

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.

capacity bounds how many requested keys keep a cached options/provider (and connection pool). Evicting an entry does not invalidate live DbContexts; a later resolution for that key builds a fresh pool.

Source

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

Registers the default DbContext with async-initialized provider options.

Provider creation runs during build_async().await; failures surface as RdiError instead of a registration-time panic. An DbContextOptionsBuilder::async_preflight hook, when configured, runs after provider creation. The DbContext remains a synchronous Scoped service, so owned resolution keeps working. A sync build() returns RdiError::AsyncServiceRequiresBuildAsync.

Source

fn add_dbcontext_keyed_async( self, key: impl IDbContextKey + Into<ServiceKey>, configure: impl FnOnce(&mut DbContextOptionsBuilder) + Send + Sync + 'static, ) -> Self

Keyed variant of Self::add_dbcontext_async.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§