From 32af31faf0854678dc5cfb298f26866d14b96ab7 Mon Sep 17 00:00:00 2001 From: Jierong Li Date: Sat, 27 Mar 2021 09:58:42 +0900 Subject: [PATCH 1/2] Update API.md for renaming --- API.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/API.md b/API.md index 2a04d8da..500a4833 100644 --- a/API.md +++ b/API.md @@ -125,11 +125,11 @@ Since the root of a DI tree does not have a parent component, we bootstrap the r ```swift let rootComponent = RootComponent() -class RootComponent: NeedleFoundation.RootComponent { +class RootComponent: BootstrapComponent { /// Root component code... } ``` -Notice the `RootComponent` does not need to specify any dependency protocol by inheriting from `NeedleFoundation.RootComponent`. Root of a DI graph has no parent to acquire dependencies from anyways. +Notice the `RootComponent` does not need to specify any dependency protocol by inheriting from `BootstrapComponent`. Root of a DI graph has no parent to acquire dependencies from anyways. Since we know `root` does not have any parents, in application code, we can simply invoke `RootComponent()` to instantiate the root scope. From 12f544b580d8dfbc9f0724076c41f8e69c37796c Mon Sep 17 00:00:00 2001 From: Jierong Li Date: Sat, 27 Mar 2021 15:15:32 +0900 Subject: [PATCH 2/2] Add an example for injecting dynamic dependency --- API.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/API.md b/API.md index 500a4833..7d1de6d3 100644 --- a/API.md +++ b/API.md @@ -115,6 +115,10 @@ class LoggedInComponent: Component { var gameComponent: GameComponent { return GameComponent(parent: self) } + + func scoreSheetComponent(filter: ScoreFilter? = nil) { + return ScoreSheetComponent(parent: self, filter: filter) + } } ``` Once this tree structure has been declared in code, the needle command-line tool uses it to decide where the dependencies for a particular Scope come from. The algorithm is simple, for each item that this scope requires, we walk up the chain of parents. The **nearest parent** that is able to provide an item (we match both the name and the type of the variable declared in the dependency protocol), is the one we fetch that property from.