# Inject Classes

There are different ways to inject classes using dependency injection. Zenject provides constructor, field, property, and method injection.

{% hint style="warning" %}
&#x20;**It is recommended to only use Constructor and Method Injection.** [**Read more here**](https://github.com/modesttree/Zenject#:~:text=Recommendations,field/property%20injection.)
{% endhint %}

## Constructor Injection

Most of your scripts will probably inherit form **`MonoBehaviour`** so you will not be able to use constructor injection. If your class is a service or helper class that does not inherit **`MonoBehaviour`** then you can use constructor injection.&#x20;

```csharp
internal class DependencyInjectionExample
{
    private EasyEvents _events;

    public DependencyInjectionExample(EasyEvents events)
    {
        _events = events;
    }
}
```

Zenject dependency Injection will automatically provide an instance of **EasyEvents** into the **`DependencyInjectionExample`** class

{% hint style="info" %}
Zenject will inject dependencies during Unity'&#x73;**`Awake()`** phase. Recommended to access the injected dependencies in the **`Start()`**&#x6D;ethod instead of **`Awake()`**
{% endhint %}

## Method Injection

Most of your scripts will probably inherit form **`MonoBehaviour`** so you will not be able to use constructor injection. In that case you will need to use Method injection. There 2 ways that you can use **Method** **Injection.**

### 1. Have a private variable that is assigned via method injection and used throughout your class. Injected only once.&#x20;

{% hint style="info" %}
I have named the method **Initialize()** but you can use whatever method name you want. I have seen other people use the naming convention **Constructor()** since it acts like a constructor for classes that inherit from **MonoBehaviour**&#x20;
{% endhint %}

```csharp
internal class DependencyInjectionExample : MonoBehaviour
{
    private EasyEvents _events;

    [Inject]
    private void Initialize(EasyEvents events)
    {
        _events = events;
    }
    
    public void AddEvent()
    {
        _events.LoggedIn += OnLoggedIn;
    }

    public void RemoveEvent()
    {
        _events.LoggedIn -= OnLoggedIn;
    }

    private void OnLoggedIn(ILoginSession loginSession)
    {
        Debug.Log($"User {loginSession.LoginSessionId.DisplayName} has logged in");
    }
}
```

### 2. Inject the class into every method that needs. Injected into every class with <mark style="color:blue;">\[Inject]</mark> attribute

```csharp
internal class DependencyInjectionExample : MonoBehaviour
{
    [Inject]
    public void AddEvent(EasyEvents events)
    {
        events.LoggedIn += OnLoggedIn;
    }

    [Inject]
    public void RemoveEvent(EasyEvents events)
    {
        events.LoggedIn -= OnLoggedIn;
    }

    private void OnLoggedIn(ILoginSession loginSession)
    {
        Debug.Log($"User {loginSession.LoginSessionId.DisplayName} has logged in");
    }
    
    [Inject]
    private void AudioSettings(EasyAudio audio)
    {
        audio.AdjustLocalPlayerAudioVolume(25, EasySession.Client);
    }
}
```


---

# 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://fullstackindie.gitbook.io/easy-code-for-vivox/dependency-injection/inject-classes.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.
