Skip to main content

Provide

Trait Provide 

Source
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§

Source

fn provide(&self) -> T

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§

Source§

impl Provide<AppConfig> for AppContext

Source§

impl Provide<AppMetadata> for AppContext

Source§

impl Provide<Option<RedisFetch>> for AppContext

Available on crate feature worker-sidekiq only.
Source§

impl Provide<PGMQueue> for AppContext

Available on crate feature worker-pg only.
Source§

impl Provide<Pool<ConnectionManager<MysqlConnection>>> for AppContext

Available on crate feature db-diesel-mysql-pool only.
Source§

impl Provide<Pool<ConnectionManager<PgConnection>>> for AppContext

Available on crate feature db-diesel-postgres-pool only.
Source§

impl Provide<Pool<ConnectionManager<SqliteConnection>>> for AppContext

Available on crate feature db-diesel-sqlite-pool only.
Source§

impl Provide<RedisEnqueue> for AppContext

Available on crate feature worker-sidekiq only.
Source§

impl Provide<Sender> for AppContext

Available on crate feature email-sendgrid only.
Source§

impl Provide<SmtpTransport> for AppContext

Available on crate feature email-smtp only.
Source§

impl Provide<Vec<Arc<dyn HealthCheck>>> for AppContext

Source§

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: