Skip to content

Commit

Permalink
Adjust wording
Browse files Browse the repository at this point in the history
  • Loading branch information
hypergonial committed Dec 30, 2023
1 parent d9ec882 commit 03d4cb5
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions docs/guides/dependency_injection.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: A guide on dependency injection & arc

# Dependency Injection

**Dependency Injection** is a programming technique aimed at managing state or objects that a particular function may need to perform an operation. For example, if you have a function that needs access to a database, we can *inject* the database to said function when it is called. In the case of Discord bots, it can be a good way to manage state such as access to a database, an http client & so on.
**Dependency Injection** is a programming pattern aimed at seperating the initialization of state from functions that need to perform operations on said state. For example, if you have a function that needs access to a database, we can *inject* the database to said function when it is called. In the case of Discord bots, it can be a good way to share state, such as access to a database, an http client and so on.

`arc` uses [`alluka`](https://alluka.cursed.solutions/usage/) to facilitate dependency injection, and command callbacks are automatically injected with declared dependencies.

Expand All @@ -28,7 +28,8 @@ description: A guide on dependency injection & arc
client = arc.GatewayClient(bot)


database = MyDatabase(0)
database = MyDatabase(value=0)

# We declare a new dependency of type 'MyDatabase' and the value of 'database'
client.set_type_dependency(MyDatabase, database)
```
Expand All @@ -50,7 +51,8 @@ description: A guide on dependency injection & arc
client = arc.RESTClient(bot)


database = MyDatabase(0)
database = MyDatabase(value=0)

# We declare a new dependency of type 'MyDatabase' and the value of 'database'
client.set_type_dependency(MyDatabase, database)
```
Expand Down Expand Up @@ -109,7 +111,7 @@ def compare_counter(value: int, db: MyDatabase = arc.inject()) -> None:

## The benefits of dependency injection

Dependency injection **seperates the concern** of constructing an object from using them, therefore it is possible to **loosely couple** the logic & state of your program. One benefit of this approach is that we can seperate the actual implementations from the abstract types that functions may consume.
Dependency injection **separates the concern** of constructing an object from using them, therefore it is possible to **loosely couple** the logic and state of your program. One benefit of this approach is that we can separate the actual implementations from the abstract types that functions may consume.

!!! tip
If you do not know what [ABC](https://docs.python.org/3/glossary.html#term-abstract-base-class "Abstract Base Class")s in Python are, it is recommended that you [familiarize yourself](https://docs.python.org/3/library/abc.html) with them first before following this guide further.
Expand Down Expand Up @@ -139,7 +141,7 @@ class MockDatabase(Database):
return 0
```

Let's say our app has two configurations, a "testing mode" where we want our "database" to simply return fake values, and a "production mode" where it actually connects to a real database & fetches values from it. If your code relies on the concrete implementation of `ProductionDatabase` or `MockDatabase`, it is hard to switch it out on the fly, however if your code only depends on `Database`, you can effectively swap out which underlying implementation of `Database` it is using, and your code continues to work!
Let's say our app has two configurations, a "testing mode" where we want our "database" to simply return fake values, and a "production mode" where it actually connects to a real database and fetches values from it. If your code relies on the concrete implementation of `ProductionDatabase` or `MockDatabase`, it is hard to switch it out on the fly, however if your code only depends on `Database`, you can effectively swap out which underlying implementation of `Database` it is using, and your code continues to work!

=== "Gateway"

Expand Down

0 comments on commit 03d4cb5

Please sign in to comment.