pub struct Provider;Expand description
Provider Helper Struct
A helper struct for creating different types of providers.
Use the static methods Provider::value(), Provider::factory(),
Provider::request_factory(), and Provider::transient_factory()
when defining module providers or registering them manually.
§Example
impl ModuleDefinition for AppModule {
fn register(container: &Container) -> Result<()> {
register_provider(container, Provider::value(AppConfig::default()))?;
register_provider(container, Provider::factory(|c| Ok(MyService::new(c))))?;
Ok(())
}
}Implementations§
Source§impl Provider
impl Provider
Sourcepub fn value<T>(value: T) -> ValueProvider<T>
pub fn value<T>(value: T) -> ValueProvider<T>
Sourcepub fn factory<T, F>(factory: F) -> FactoryProvider<T, F>
pub fn factory<T, F>(factory: F) -> FactoryProvider<T, F>
Creates a factory provider.
The factory receives the Container and returns Result
§Type Parameters
T: The type to create (must be Send + Sync + ’static)F: The factory function type
Sourcepub fn request_factory<T, F>(factory: F) -> RequestFactoryProvider<T, F>
pub fn request_factory<T, F>(factory: F) -> RequestFactoryProvider<T, F>
Creates a request-scoped provider.
The factory is executed once per request (per scoped container). The created instance is cached for the duration of that request.
§Type Parameters
T: The type to create (must be Send + Sync + ’static)F: The factory function type
Sourcepub fn transient_factory<T, F>(factory: F) -> TransientFactoryProvider<T, F>
pub fn transient_factory<T, F>(factory: F) -> TransientFactoryProvider<T, F>
Creates a transient provider.
The factory is executed every time the type is resolved via container.resolve(). A new instance is created each time.
§Type Parameters
T: The type to create (must be Send + Sync + ’static)F: The factory function type