<commit_before>---
title: Dependency Injection
---

Arez components provide several different mechanisms for integrating into dependency injection
frameworks. The two dependency injection frameworks that have seen the most use are
[Dagger2](https://google.github.io/dagger) and [GIN](https://code.google.com/archive/p/google-gin/).
This is due to Arez being primarily used within a web browser context.

Arez components typically consist of the base component (i.e. `com.example.MyComponent`), the enhanced
component class generated by the Arez annotation processor (i.e. `com.example.Arez_MyComponent`)
and optionally a generated base repository (i.e. `com.example.MyComponentRepository`) and an enhanced
repository class (i.e. `com.example.Arez_MyComponentRepository`) both of which are generated by
the annotation processor.

Typically when using Arez component in an application that uses dependency injection, the consuming
code will use `@Inject` to get references to the base component (i.e. `com.example.MyComponent`) or
the base repository (i.e. `com.example.MyComponentRepository`). The consumer code ends up looking
something like one of the following examples depending on whether field or constructor based injection
is used.

{@file_content: file=arez/doc/examples/inject1/ExampleConsumer.java "start_line=public class"}<commit_msg>Use more precise language<commit_after>---
title: Dependency Injection
---

Arez components provide several different mechanisms for integrating into dependency injection
frameworks. The two dependency injection frameworks that have seen the most use are
[Dagger2](https://google.github.io/dagger) and [GIN](https://code.google.com/archive/p/google-gin/).
This is due to Arez being primarily used within a web browser context.

Arez components typically consist of the base component (i.e. `com.example.MyComponent`), the enhanced
component class generated by the Arez annotation processor (i.e. `com.example.Arez_MyComponent`)
and optionally a generated base repository (i.e. `com.example.MyComponentRepository`) and an enhanced
repository class (i.e. `com.example.Arez_MyComponentRepository`) both of which are generated by
the annotation processor.

Typically when using Arez component in an application that uses dependency injection, the consuming
code will use the `@Inject` annotation to get references to the base component (i.e. `com.example.MyComponent`) or
the base repository (i.e. `com.example.MyComponentRepository`). The consumer code ends up looking
something like one of the following examples depending on whether field or constructor based injection
is used.

{@file_content: file=arez/doc/examples/inject1/ExampleConsumer.java "start_line=public class"}
{'fraction_non_alphanumeric': 0.07796984185362266, 'fraction_numerical': 0.001471129091577786, 'mean_word_length': 5.415094339622642, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4412402', 'n_tokens_mistral': 691, 'n_tokens_neox': 673, 'n_words': 304}