Skip to main content

ScopeFactory

Trait ScopeFactory 

Source
pub trait ScopeFactory: Send + Sync {
    // Required method
    fn create_scope(&self) -> Scope;
}
Expand description

工厂 trait:创建独立 scope。可注入 singleton 服务按需创建 scope。

典型场景:后台 worker singleton 需要按请求创建独立 scope, 在 scope 内解析 Scoped 服务(如 request-scoped DbContext)。

ServiceProvider 实现此 trait,通过 ServiceProvider::scope 创建 scope。 注册方式:provider.singleton::<dyn ScopeFactory>(move |_| provider.clone() as Arc<dyn ScopeFactory>)

Required Methods§

Source

fn create_scope(&self) -> Scope

创建一个独立 scope,共享 root 的 store/singleton_cache/named, 独立 scoped_cache 和 disposables。

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§