You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: service-container.md
+22-4Lines changed: 22 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,9 @@
2
2
3
3
4
4
## Introduction
5
-
Supercharge uses a service container to manage dependencies. The container allows you to register dependencies and retrieve them when needed. This setup supports dependency injection in your application. Well, dependency injection is a funky term that basically describes “injecting” dependencies into a class.
5
+
Supercharge uses a service container to manage dependencies. The container allows you to register dependencies and retrieve them when needed. This setup supports dependency injection in your application. Well, dependency injection is a funky term that basically describes “injecting” dependencies into a class (or function).
6
6
7
-
Injecting dependencies instead of letting classes resolve them on their own has the benefit of controlling the dependencies instead of relying on existing setups. Controlling dependencies is especially helpful during testing.
7
+
Injecting dependencies instead of letting classes resolve them on their own has the benefit of controlling the dependencies. Controlling dependencies is especially helpful during testing because you can inject mock objects or fake data and run assertions on them.
8
8
9
9
Here’s an example where the dependency injection and the service container is helpful:
10
10
@@ -26,9 +26,27 @@ class RedisCache implements CacheContract {
26
26
* Determine whether the cache stores an item for the given `key`.
27
27
*/
28
28
has(key:string):boolean {
29
-
return!!awaitthis.redis.get(key)
29
+
returnawaitthis.redis.exists(key)
30
30
}
31
31
}
32
32
```
33
33
34
-
The `RedisCache` should be responsible to for the caching of items. A Redis client should store and retrieve cache items. The Redis cache itself shouldn’t worry about setting up a Redis client instance and connecting it to the Redis database. The cache should get its dependencies injected. That’s where the container comes handy.
34
+
The `RedisCache` should be responsible for the caching of items. A Redis client should store and retrieve individual cache items. The Redis cache itself shouldn’t worry about setting up a Redis client instance and connecting it to the Redis database. Here’s where the container comes handy: injecting a Redis client into the cache class .
35
+
36
+
37
+
## Resolving Dependendies
38
+
The Supercharge service container **does not** resolve dependencies automatically for you. You need to actively retrieve services from the container when needed.
39
+
40
+
For example, you may need to retrieve a `Redis` service in your [service provider](/docs/service-providers) when building a custom Redis cache class.
41
+
42
+
43
+
## When to Use The Container
44
+
You should use the container when writing a Supercharge package that you're sharing with other Supercharge developers. Typically, you’re providing a service provider that binds your services into the container.
0 commit comments