diff --git a/docs/guide/walkthrough/services.md b/docs/guide/walkthrough/services.md index 2756c2eea..c72800da3 100644 --- a/docs/guide/walkthrough/services.md +++ b/docs/guide/walkthrough/services.md @@ -2,11 +2,12 @@ sidebar_position: 6 --- +# Services + :::tip This is version 3 api only!! ::: -# Services :::tip TLDR: The direct upgrade to useContainer. if you set up a bot with create-bot, check dependencies.d.ts. @@ -48,14 +49,73 @@ export default commandModule({ }) ``` -## Safety -- Services cannot be called in other services while makeDependencies is forming. -- You will need to wire dependencies together. import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; +## Safety +- Services cannot be called in other services while makeDependencies is forming. + + + + + +Lets pass a logger into our database. +```ts title="index.ts" showLineNumbers +await makeDependencies({ + build: root => root + //Overriding the default logger provided. + .upsert({ '@sern/logger': single(() => new Logger()) }) + + // Wiring our logger into the database. + .add(ctx => { + return { database: single(() => new Database(ctx['sern/logger']))) } + }) +}) +``` + + + + +```ts title="index.ts" showLineNumbers +await makeDependencies({ + build: root => root + //Overriding the default logger provided. + .upsert({ '@sern/logger': single(() => new Logger()) }) + + // Wiring our logger into the database. + // We wire our database incorrectly. Logger should be passed INTO the constructor + .add({ database: single(() => new Database()) }) +}) +``` + +```ts title="index.ts" showLineNumbers +import { Service, makeDependencies } from '@sern/handler'; + +//Calling Service prematurely! +const logger = Service('@sern/logger'); + +class Database { + + constructor() { + this.logger = logger + } +} +``` +This is a code smell anyway. It breaks encapsulation and defeats the purpose of wiring dependencies + + + +- Services can only be used after sern has made dependencies. + - Calling a service before will crash your application. +- Services can be safely used outside of commandModules. + - Be careful to not cause too many side effects. + + + +- You will need to wire dependencies together. + @@ -67,6 +127,7 @@ await makeDependencies(...pass your options here) import { Service } from '@sern/handler'; const client = Service('@sern/client'); ``` +