pub trait Provide<T> {
// Required method
fn provide(&self) -> T;
}Expand description
Trait to allow getting an instance of T from the implementing type. AppContext
implements this for various types it contains. This allows a method to specify the type it
requires, then the caller of the method can determine how to provide the type. This is a
similar concept to dependency injection (DI) in frameworks like Java Spring, though this
is far from a full DI system.
This is useful, for example, to allow mocking the DB connection in tests. Your DB operation
method would declare a parameter of type Provide<DataBaseConnection>, then your application
code would provide the AppContext to the method, and your tests could provide a mocked
Provide instance that returns a mock DB connection. Note that mocking the DB comes with
its own set of trade-offs, for example, it may not exactly match the behavior of an actual DB
that’s used in production. Consider testing against an actual DB instead of mocking, e.g.,
by using test containers.
A mocked implementation of the trait is provided if the testing-mocks feature is enabled.
See also:
Required Methods§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementors§
impl Provide<AppConfig> for AppContext
impl Provide<AppMetadata> for AppContext
impl Provide<Option<RedisFetch>> for AppContext
worker-sidekiq only.impl Provide<PGMQueue> for AppContext
worker-pg only.impl Provide<Pool<ConnectionManager<MysqlConnection>>> for AppContext
db-diesel-mysql-pool only.impl Provide<Pool<ConnectionManager<PgConnection>>> for AppContext
db-diesel-postgres-pool only.impl Provide<Pool<ConnectionManager<SqliteConnection>>> for AppContext
db-diesel-sqlite-pool only.impl Provide<RedisEnqueue> for AppContext
worker-sidekiq only.impl Provide<Sender> for AppContext
email-sendgrid only.impl Provide<SmtpTransport> for AppContext
email-smtp only.impl Provide<Vec<Arc<dyn HealthCheck>>> for AppContext
impl<T> Provide<T> for MockProvide<T>
Trait to allow getting an instance of T from the implementing type. AppContext
implements this for various types it contains. This allows a method to specify the type it
requires, then the caller of the method can determine how to provide the type. This is a
similar concept to dependency injection (DI) in frameworks like Java Spring, though this
is far from a full DI system.
This is useful, for example, to allow mocking the DB connection in tests. Your DB operation
method would declare a parameter of type Provide<DataBaseConnection>, then your application
code would provide the AppContext to the method, and your tests could provide a mocked
Provide instance that returns a mock DB connection. Note that mocking the DB comes with
its own set of trade-offs, for example, it may not exactly match the behavior of an actual DB
that’s used in production. Consider testing against an actual DB instead of mocking, e.g.,
by using test containers.
A mocked implementation of the trait is provided if the testing-mocks feature is enabled.
See also: