---
description: >-
  Dependency Injection (DI), is an important design pattern. Mandarine has its
  own DI framework built-in, which is used to increase efficiency, readability,
  and modularity.
---

# Dependency Injection

This page describes all the information related to Mandarine's built-in dependency injection (DI) framework.

## Concepts

Dependencies are objects that a specific class may need to perform certain operations. Instead of programmatically initialize the needed objects, DI allows classes to ask for dependencies from external sources. These dependencies are also known as _Injectables._

In Mandarine, the DI framework can only inject Mandarine recognizable components ([See here for more information](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-core/components)). **Note** that not all components are injectable as described in the mentioned article.

| Component Type                                                                                                    | Injectable | Allows Injection |
| ----------------------------------------------------------------------------------------------------------------- | ---------- | ---------------- |
| [Controller](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-core/components/controller)             | No         | Yes              |
| [Middleware](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-core/components/middleware)             | No         | Yes              |
| [Repository](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-core/components/repository)             | Yes        | No               |
| [Component](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-core/components/component)               | Yes        | Yes              |
| [Service](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-core/components/service)                   | Yes        | Yes              |
| [Configuration](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-core/components/configuration)       | Yes        | Yes              |
| [Manual Component](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-core/components/manual-component) | Yes        | No               |

* DI container
  * The DI Container is a global registry that handles the registration & keep all the injectable objects in your application together. When a dependency is needed in a component, Mandarine's internal systems will request the DI container in order to get the needed dependency.
* DI Factory
  * The DI Factory handles the injection of injectable objects (components) as well as their extraction.

### Consider

The injection process will take place at _mandarine compile time_ without human interaction. At the end of mandarine compilation, your application will be ready to use as it will have all the injections requested already injected and ready to be used.

## Injection forms

As mentioned before, the _DI Factory_ handles the injection of components. This injection can take place in two different forms

* By class construction
  * The parameters of the constructor of a component are considered injectable objects & mandarine will try to resolve them
* Manual Injection (Field injection)
  * The use of the decorator @Inject decorating a field will inject the requested object.
    * Mandarine will know what object is requested by on the declared type
  * Although manual injection is available, it is not recommended as it may make your code larger and slower at typescript compile time.

### The @Inject Decorator

**Syntax:**

```typescript
@Inject()
```

The @Inject decorator has two possible targets:

* Field
* Method parameter
  * Injection by method parameter is not recommended as it may contradict Single Responsibility Principle (SRP)&#x20;

### The @Injectable Decorator

The injectable decorator handles the creation of a manual component. When @Injectable is called, it will create a manual component inside the DI container and it will be readable for mandarine to resolve at _mandarine compile time._

For more information & examples about this, please [click here](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-core/components/manual-component).

**Syntax:** &#x20;

```typescript
@Injectable()
```

The @Injectable decorator has one possible target:

* Method
  * This method should return an initialized class that will be the reference to the expected object in dependency injection requests

Example:

```typescript
import { Service, Inject } from "https://deno.land/x/mandarinets/mod.ts";

@Service()
class Service1 {
    public fooNumber: number = 123;
}

@Service()
class Service2 {
    constructor(private readonly service1: Service1) {
    }

    public getFooNumber(): string {
        return `Your foo number is ${this.service1.fooNumber}`;
    }
}

@Service()
class Service3 {
    
    @Inject()
    private readonly service2: Service2;

    public helloWorld(): string {
        return this.service2.getFooNumber();
    }
}
```

In the example above, we can see how we are using manual injection on a field (_line 20_) and we are also using _injection by class construction_ (Line 8). After mandarine compile time, when we call our method **helloWorld()** which is a member of **Service3,** we will get **"Your foo number is 123"**
