> For the complete documentation index, see [llms.txt](https://docs.aurelia.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aurelia.io/getting-to-know-aurelia/services-and-runtime-hooks/dependency-injection.md).

# Dependency injection (DI)

Learn the fundamentals of Aurelia's dependency injection container and how to register, resolve, and organize services.

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](/getting-to-know-aurelia/startup-and-enhancement/app-configuration-and-startup.md); 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](/getting-to-know-aurelia/services-and-runtime-hooks/dependency-injection/overview.md)              |
| Registration helpers | `Registration.singleton`, `.transient`, `.instance`, and more control lifetime and creation. | [Creating services](/getting-to-know-aurelia/services-and-runtime-hooks/dependency-injection/creating-services.md)      |
| Interfaces & tokens  | Use symbols or `DI.createInterface()` to inject by contract instead of class.                | [What is DI?](/getting-to-know-aurelia/services-and-runtime-hooks/dependency-injection/what-is-dependency-injection.md) |
| Resolvers            | Fine tune how dependencies are delivered (lazy, optional, all, new instance, etc.).          | [Resolvers](/getting-to-know-aurelia/services-and-runtime-hooks/dependency-injection/resolvers.md)                      |

## 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](/getting-to-know-aurelia/services-and-runtime-hooks/app-tasks.md) 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](/getting-to-know-aurelia/services-and-runtime-hooks/dependency-injection/overview.md) for a conceptual refresher.
2. Review [creating services](/getting-to-know-aurelia/services-and-runtime-hooks/dependency-injection/creating-services.md) to choose the right registration helper.
3. Learn how [resolvers](/getting-to-know-aurelia/services-and-runtime-hooks/dependency-injection/resolvers.md) influence injection behavior for advanced scenarios.
4. Explore [What is dependency injection?](/getting-to-know-aurelia/services-and-runtime-hooks/dependency-injection/what-is-dependency-injection.md) if you’re new to DI patterns in general.

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
