@11z/core v1.4.0
@11z/core
11z(core) - lighter, easier and maintainable.
Feature ✨
- Typescript dependency injection ✔
Table of contents
Example
Dependency injection
./di.ts
import { Injectable, Injector, Inject } from '@11z/core'
// Injector service.
@Injectable()
class ExampleService {
public helloWorld(): string {
return 'hello world!'
}
}
// Injector.
class Example {
constructor(@Inject(ExampleService) private readonly exampleService: ExampleService) {}
public helloWorld(): string {
return this.exampleService.helloWorld()
}
}
// NOTE: Testing purpose.
const example = Injector.get(Example)
console.log(example.helloWorld()) // hello word!
Installation
You need nodeJs install.
# with npm
npm i @11z/core
# installing typescript
1. npm i -D typescript - in this case I'm using npm.
2. npx tsc --init - to create tsconfig.json file.
As we all know, the library uses @decorator
without enabling some additional features. Typescript will complain. You need to enable these additional features of Typescript. In the file
'tsconfig.json' Launch these:
{
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
That's it. let's get into coding! see example.
Apis
We provide all the Apis that you will need to create a flexible and maintainable application.
@Injectable
The @Injectable()
decorator is used to define metadata object.
Example:
./injectable.ts
import { Injectable } from '@11z/core'
@Injectable()
export class ExampleService {}
@Inject
The @Inject()
decorator is used to mark parameter as dependency.
Example:
./inject.ts
import { Inject } from '@11z/core'
import { ExampleService } from './injectable'
export class Example {
constructor(@Inject(ExampleService) private readonly exampleService: ExampleService) {}
}
@Injector
A top-level class used to resolve injector value.
Example:
./injector.ts
import { Injector } from '@11z/core'
import { Example } from './inject'
// Resolve Example injector value.
Injector.get(Example)
Define injector
The defineInjector
function is used to define metadata object.
Example:
import { defineInjector } from '@11z/core'
export class Example {}
// Define injector.
defineInjector(Example)
The end
11z - lighter, easier and maintainable.
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago