Skip to main content
Kit provides a powerful dependency injection (DI) container inspired by Laravel’s service container. The container manages class dependencies, allows automatic resolution, and enables easy testing through dependency swapping.

The App Container

The App struct is the central facade for dependency injection in Kit. It provides static methods for registering and resolving services throughout your application.

Registering Services

Singletons

Singletons are shared instances that persist for the application’s lifetime. The same instance is returned every time you resolve the type:

Factories

Factories create a new instance every time the service is resolved:

Trait Bindings

Bind a trait to a concrete implementation, enabling interface-based programming:

Factory Trait Bindings

Create a new implementation instance each time the trait is resolved:

Resolving Services

Basic Resolution

Use App::get() for optional resolution or App::resolve() for required dependencies:

Trait Resolution

Use App::make() or App::resolve_make() for trait objects:

Resolution in Controllers

The ? operator makes dependency resolution clean and ergonomic in controllers:

The #[injectable] Macro

The #[injectable] macro provides automatic dependency injection with zero boilerplate. It:
  1. Automatically derives Clone (and Default for simple structs)
  2. Registers the type as a singleton at application startup
  3. Resolves #[inject] field dependencies automatically

Simple Injectable

For structs without dependencies:

Injectable with Dependencies

Use #[inject] to mark fields that should be resolved from the container:
Dependencies are resolved when the service is registered. Ensure dependencies are registered before dependents.

Unit Structs

Unit structs are also supported:

Registration Methods

Resolution Methods

Checking Registration

Check if a service is registered before resolving:

Convenience Macros

Kit provides macros for cleaner registration syntax:

Testing with the Container

Kit provides TestContainer for isolated testing with fake implementations:

TestContainer Methods

The TestContainerGuard ensures test isolation by automatically cleaning up when dropped.

Manual Registration in Bootstrap

While #[injectable] provides automatic registration, you may need to manually register services that require runtime configuration (like database connections, external API clients, or services configured from environment variables). The bootstrap.rs file is the central location for manual service registration:
The bootstrap::register() function is called from main.rs before the server starts:
For more details on the bootstrap file and when to use manual vs automatic registration, see the Bootstrap documentation.

Auto-Registration

Kit uses the inventory crate for compile-time service registration. Services marked with #[injectable] are automatically registered when App::boot_services() is called (this happens automatically in Server::from_config()).

Practical Examples

Service with Database Access

Service with Injected Dependencies

Using in Controller

Summary