Dependency injection
When working on a Pavex application, you don't have to worry about wiring.
All the components in your application (request handlers, middlewares, error handlers, etc.) use their input parameters to declare the data they need to do their job.
We refer to those input parameters as their dependencies.
Pavex takes care of building and injecting those dependencies when and where they're needed.
We refer to this system as Pavex's dependency injection framework.
What is the purpose of dependency injection?
Let's look at an example: rejecting unauthenticated requests in a middleware.
The desired behavior:
- If the user is logged in, the middleware lets the request through.
- If the user isn't logged in, a
401 Unauthorizedresponse is returned.
use pavex::Response;
use pavex::middleware::Processing;
use pavex::pre_process;
use crate::User;
#[pre_process]
pub fn reject_anonymous(user: &User) -> Processing {
if let User::Anonymous = user {
let r = Response::unauthorized();
Processing::EarlyReturn(r)
} else {
Processing::Continue
}
}
The middleware logic doesn't care about how authentication is performed. It only cares about the result: is the user authenticated or not?
The contract is data-driven: as long as the outcome of the authentication process doesn't change
(i.e. the User type) the middleware will work as expected and doesn't need to be modified.
You won't have to touch middleware code if, in the future,
you decide to migrate to a different authentication system
(e.g. from username/password authentication to an OAuth2 flow).
This is the entire purpose of Pavex's dependency injection framework: decouple the way data is computed
from the way it's used.
The middleware doesn't care about how the User is computed, it only cares about what it is.
This is a simple example, but the same principle applies to a vast collection of use cases: body parsing, logging, authorization, etc.
Guide structure
There are four different sources of injectable dependencies in Pavex:
Check out the respective sections for guidance on how and when to use each.
First-party components
Pavex provides a variety of constructors to extract commonly used data from the incoming request. Check out the "Request data" guide for an overview.