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

# Dependency injection (DI)

Dependency Injection (DI) is one of Aurelia's core features. It manages the creation and delivery of object dependencies, promoting loose coupling and making your application more modular and testable.

Instead of components creating their own dependencies, Aurelia's DI container provides them automatically. This means your classes declare what they need, and the framework handles the rest.

## How It Works

At its simplest, you use the `@autoinject` decorator (or the static `inject` property) to tell Aurelia what dependencies a class needs:

```javascript
import { autoinject } from 'aurelia-framework';
import { HttpClient } from 'aurelia-fetch-client';
import { EventAggregator } from 'aurelia-event-aggregator';

@autoinject
export class UserService {
  constructor(private http: HttpClient, private ea: EventAggregator) {
  }
}
```

Aurelia automatically creates and injects instances of `HttpClient` and `EventAggregator` when `UserService` is requested.

## Key Concepts

* **Container** — The DI container manages object creation and lifetime. Aurelia creates a root container at startup, and child containers are created for each component.
* **Registration** — How the container knows what to create. By default, classes are registered as transients (new instance each time). You can also register singletons and custom factories.
* **Resolution** — How dependencies are located and provided. The container walks up the container hierarchy until it finds a registration.
* **Resolvers** — Special helpers like `Lazy`, `All`, `Optional`, and `Factory` that give you fine-grained control over how dependencies are resolved.

## In This Section

* [Overview](/v1-docs/getting-to-know-aurelia/dependency-injection-di/overview.md) — Deep dive into how the DI container works, object lifetimes, and container hierarchies
* [Injection](/v1-docs/getting-to-know-aurelia/dependency-injection-di/injection.md) — All the ways to declare and receive dependencies
* [Resolvers](/v1-docs/getting-to-know-aurelia/dependency-injection-di/resolvers.md) — Advanced resolution strategies for special scenarios

{% hint style="info" %}
If you're using TypeScript, `@autoinject` reads type metadata to determine dependencies automatically. For JavaScript, use the static `inject` property or the `@inject()` decorator instead.
{% endhint %}


---

# 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://aurelia-1.gitbook.io/v1-docs/getting-to-know-aurelia/dependency-injection-di.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.
