Skip to content

Commit 275d919

Browse files
committed
wip
1 parent 2d66777 commit 275d919

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

service-container.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33

44
## 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).
66

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.
88

99
Here’s an example where the dependency injection and the service container is helpful:
1010

@@ -26,9 +26,27 @@ class RedisCache implements CacheContract {
2626
* Determine whether the cache stores an item for the given `key`.
2727
*/
2828
has(key: string): boolean {
29-
return !!await this.redis.get(key)
29+
return await this.redis.exists(key)
3030
}
3131
}
3232
```
3333

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.
45+
46+
47+
## Binding
48+
tba.
49+
50+
51+
## Resolving
52+
tba.

0 commit comments

Comments
 (0)