# Dependency injection (DI)

Aurelia ships with a lightweight but powerful dependency injection (DI) container. It wires classes together so you can focus on behavior instead of manual service lookups.

> **Before you start:** Make sure your project is set up using [App configuration and startup](https://docs.aurelia.io/getting-to-know-aurelia/startup-and-enhancement/app-configuration-and-startup); DI registrations usually happen during boot or feature composition.

## Why DI matters

* **Loose coupling** – swap implementations without rewriting consumers.
* **Testability** – inject doubles or fakes and keep constructors clean.
* **Lifetime control** – opt into singleton, transient, and scoped registrations.
* **Discoverability** – centralise service configuration instead of scattering `new` statements across the app.

## Quick start

Declare the dependencies your class needs using the `resolve()` function:

```typescript
import { resolve, ILogger } from '@aurelia/kernel';
import { ApiClient } from './services/api-client';

export class OrdersPage {
  private api = resolve(ApiClient);
  private logger = resolve(ILogger);

  async binding() {
    const orders = await this.api.load();
    this.logger.debug('Loaded orders', orders);
  }
}
```

Most Aurelia apps use the default root container created by the runtime, but you can always create your own for testing or specialised scenarios:

```typescript
import { DI, Registration } from '@aurelia/kernel';

const container = DI.createContainer();
container.register(
  Registration.singleton(ApiClient, ApiClient),
  Registration.instance('config', runtimeConfig)
);
```

Resolve services directly when you need to:

```typescript
const api = container.get(ApiClient);
const loggers = container.getAll(ILogger);
```

## Key concepts at a glance

| Concept              | Summary                                                                                      | Dive deeper                                                                                                                                 |
| -------------------- | -------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| Containers & scopes  | Create child containers to override or extend registrations per feature or component.        | [Container overview](https://docs.aurelia.io/getting-to-know-aurelia/services-and-runtime-hooks/dependency-injection/overview)              |
| Registration helpers | `Registration.singleton`, `.transient`, `.instance`, and more control lifetime and creation. | [Creating services](https://docs.aurelia.io/getting-to-know-aurelia/services-and-runtime-hooks/dependency-injection/creating-services)      |
| Interfaces & tokens  | Use symbols or `DI.createInterface()` to inject by contract instead of class.                | [What is DI?](https://docs.aurelia.io/getting-to-know-aurelia/services-and-runtime-hooks/dependency-injection/what-is-dependency-injection) |
| Resolvers            | Fine tune how dependencies are delivered (lazy, optional, all, new instance, etc.).          | [Resolvers](https://docs.aurelia.io/getting-to-know-aurelia/services-and-runtime-hooks/dependency-injection/resolvers)                      |

## Usage tips

* Keep constructors small; if a class needs more than a few services, consider extracting collaborators.
* Register feature-specific services at the component or route boundary rather than globally when possible.
* Combine DI with [App tasks](https://docs.aurelia.io/getting-to-know-aurelia/services-and-runtime-hooks/app-tasks) to hook container configuration into the application lifecycle.
* When integrating with other frameworks, create a bridge service that the container can hand out instead of reaching through globals.

## Next steps

1. Walk through the [DI overview](https://docs.aurelia.io/getting-to-know-aurelia/services-and-runtime-hooks/dependency-injection/overview) for a conceptual refresher.
2. Review [creating services](https://docs.aurelia.io/getting-to-know-aurelia/services-and-runtime-hooks/dependency-injection/creating-services) to choose the right registration helper.
3. Learn how [resolvers](https://docs.aurelia.io/getting-to-know-aurelia/services-and-runtime-hooks/dependency-injection/resolvers) influence injection behavior for advanced scenarios.
4. Explore [What is dependency injection?](https://docs.aurelia.io/getting-to-know-aurelia/services-and-runtime-hooks/dependency-injection/what-is-dependency-injection) if you’re new to DI patterns in general.

Once these fundamentals feel natural, pair them with Aurelia features such as [App tasks](https://docs.aurelia.io/getting-to-know-aurelia/services-and-runtime-hooks/app-tasks) and the [task queue](https://docs.aurelia.io/getting-to-know-aurelia/services-and-runtime-hooks/task-queue) to orchestrate complex startup flows.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aurelia.io/getting-to-know-aurelia/services-and-runtime-hooks/dependency-injection.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
