Dependencies
Esta página aún no está disponible en tu idioma.
Manage objects which contain lots of state. If you were a previous user of Sapphire, dependency injection is the moral equivalent of container
.
Dependency injection promotes maintainability and helps organize imports.
For example, a minimal setup for any project might look like this:
Directorysrc/
- index.js (your main file and client)
- dependencies.d.ts (for intellisense)
Everything else is handled. However, you may want customize things.
Adding Dependencies to Root
When makeDependencies is called, sern implicility adds dependencies to the container. A few of these are shown:
@sern/logger
: Loggers implementLogging
@sern/modules
: A map of all command modules. ID -> Module@sern/errors
: Handling errors and lifetime →ErrorHandling
@sern/emitter
: Emit events that occur throughout the lifecycle of a project →Emitter
Lifecycle Hooks
Dependencies can call a function throughout you bot’s lifetime.
Init
Your object needs to initiate things. Developers are allowed to use
async
andawait
.
-
Do you need to perform intializing behavior for a dependency?
-
Modify your
Dependencies
interface: -
Make sure its been added:
-
Now, when your bot starts, the
init
method will be called. 🎉
Dispose
Your object needs to destroy things before shutdown, if a crash occurs
-
Do you need to perform intializing behavior for a dependency?
-
Make sure its been added:
-
Now, when your bot starts, the
dispose
method will be called. 🎉
Usage in Commands
This is for command modules, plugins only. event modules would have to use the
Service
api
Your dependencies are located in SDT.
Service
The Service api is used for places where sern cannot inject into parameters properly. View
When to use Dependency injection
Leveraging dependency injection in your projects is recommended because DI creates a loose coupling between the parts of your application. As a result, the codebase is more modular and easier to test and refactor