-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
hullis
committed
Sep 6, 2021
1 parent
35cdc76
commit e26ad50
Showing
3 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2021 Wendell Hu | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,96 @@ | ||
# redi | ||
|
||
A dependency library for TypeScript and JavaScript, along with a binding for React. | ||
|
||
## What's This? | ||
|
||
**redi** (pronounced 'ready') is a dependency injection library for TypeScript (& JavaScript with some babel config). It also provides a set of bindings to let you adopt the pattern in your React applications. | ||
|
||
- redi is completely opt-in. Unlike Angular, redi let you decide when and where to use dependency injection. | ||
- redi provides a multi-layered dependency injection system. | ||
- redi supports multi kinds of injection items, including classes, instances, and factories. | ||
- redi supports advanced features such as async injection item, lazy instantiation to boot your application's performance. | ||
|
||
## Getting Started | ||
|
||
### Installation | ||
|
||
```sh | ||
npm install @wendellhu/redi | ||
``` | ||
|
||
After installation you need to enable `experimentalDecorators` in your tsconfig.json file. | ||
|
||
```diff | ||
{ | ||
"compilerOptions": { | ||
+ "experimentalDecorators": true | ||
} | ||
} | ||
``` | ||
|
||
### Basics | ||
|
||
Let's get started with a real-word example: | ||
|
||
```typescript | ||
class AuthService { | ||
public getCurrentUserInfo(): UserInfo { | ||
// your implementation here... | ||
} | ||
} | ||
|
||
class FileListService { | ||
constructor() {} | ||
|
||
public getUserFiles(): Promise<Files> { | ||
const currentUser = // ...AuthService.getCurrentUserInfo() | ||
// ... | ||
} | ||
} | ||
``` | ||
|
||
It is clearly that `FileListServices` dependents on `AuthService`, so you just need to declare it on the constructor of `FileListService`. | ||
|
||
Step 1. Declare dependency relationship. | ||
|
||
```diff | ||
class AuthService { | ||
public getCurrentUserInfo(): UserInfo { | ||
// your implementation here... | ||
} | ||
} | ||
|
||
import { Inject } from '@wendellhu/redi' | ||
|
||
class FileListService { | ||
- constructor() {} | ||
+ constructor(@Inject(AuthService) private readonly authService: AuthService) {} | ||
|
||
public getUserFiles(): Promise<Files> { | ||
- const currentUser = // ...AuthService.getCurrentUserInfo() | ||
+ const currentUser = this.authService.getCurrentUserInfo() | ||
// ... | ||
} | ||
} | ||
``` | ||
|
||
Then you need to include all things into an `Injector`. | ||
|
||
Step 2. Provide dependencies. | ||
|
||
```typescript | ||
import { Injector } from '@wendellhu/redi' | ||
|
||
const injector = new Injector([[FileListService], [AuthService]]) | ||
``` | ||
|
||
Then you don't instantiate a `FileListService` by yourself. You get a `FileListService` from the injector you just created. | ||
|
||
Step 3. Get a dependency. | ||
|
||
```typescript | ||
const fileListService = injector.get(FileListService) | ||
``` | ||
|
||
That's it! |
This file was deleted.
Oops, something went wrong.