Comparing version 4.0.0-rc-32 to 4.0.0-rc-33
@@ -40,24 +40,7 @@ "use strict"; | ||
findOrCreateMetadata: metadata_js_1.findOrCreateMetadata, | ||
saveConstructorMetadata: metadata_js_1.saveConstructorMetadata, | ||
createConstructorMetadata: metadata_js_1.createConstructorMetadata, | ||
isSingleton: isSingleton_js_1.isSingleton, | ||
Trace: Trace_js_1.Trace, | ||
}; | ||
// // typescript polyfill | ||
// declare global { | ||
// interface SymbolConstructor { | ||
// readonly metadata: unique symbol; | ||
// } | ||
// | ||
// interface Function { | ||
// [Symbol.metadata]: DecoratorMetadata | null; | ||
// } | ||
// } | ||
// | ||
// type Writable<T> = { | ||
// -readonly [key in keyof T]: T[key]; | ||
// }; | ||
// | ||
// // runtime polyfill | ||
// if (Symbol.metadata == null) { | ||
// (Symbol as Writable<typeof Symbol>).metadata = Symbol('cheap-di Symbol.metadata polyfill') as typeof Symbol.metadata; | ||
// } | ||
//# sourceMappingURL=index.js.map |
@@ -13,3 +13,3 @@ export * from './cheapDiSymbol.js'; | ||
import { inject } from './decorators/inject.js'; | ||
import { findMetadata, findOrCreateMetadata } from './metadata.js'; | ||
import { findMetadata, findOrCreateMetadata, saveConstructorMetadata, createConstructorMetadata } from './metadata.js'; | ||
import { isSingleton } from './isSingleton.js'; | ||
@@ -25,24 +25,7 @@ import { Trace } from './Trace.js'; | ||
findOrCreateMetadata, | ||
saveConstructorMetadata, | ||
createConstructorMetadata, | ||
isSingleton, | ||
Trace, | ||
}; | ||
// // typescript polyfill | ||
// declare global { | ||
// interface SymbolConstructor { | ||
// readonly metadata: unique symbol; | ||
// } | ||
// | ||
// interface Function { | ||
// [Symbol.metadata]: DecoratorMetadata | null; | ||
// } | ||
// } | ||
// | ||
// type Writable<T> = { | ||
// -readonly [key in keyof T]: T[key]; | ||
// }; | ||
// | ||
// // runtime polyfill | ||
// if (Symbol.metadata == null) { | ||
// (Symbol as Writable<typeof Symbol>).metadata = Symbol('cheap-di Symbol.metadata polyfill') as typeof Symbol.metadata; | ||
// } | ||
//# sourceMappingURL=index.js.map |
@@ -11,3 +11,3 @@ export * from './cheapDiSymbol.js'; | ||
import { ContainerImpl } from './ContainerImpl.js'; | ||
import { findMetadata, findOrCreateMetadata } from './metadata.js'; | ||
import { findMetadata, findOrCreateMetadata, saveConstructorMetadata, createConstructorMetadata } from './metadata.js'; | ||
import { isSingleton } from './isSingleton.js'; | ||
@@ -23,2 +23,4 @@ import { Trace } from './Trace.js'; | ||
findOrCreateMetadata: typeof findOrCreateMetadata; | ||
saveConstructorMetadata: typeof saveConstructorMetadata; | ||
createConstructorMetadata: typeof createConstructorMetadata; | ||
isSingleton: typeof isSingleton; | ||
@@ -25,0 +27,0 @@ Trace: typeof Trace; |
{ | ||
"name": "cheap-di", | ||
"description": "TypeScript dependency injection like Autofac in .Net", | ||
"version": "4.0.0-rc-32", | ||
"version": "4.0.0-rc-33", | ||
"scripts": { | ||
@@ -6,0 +6,0 @@ "compile": "tsc --build tsconfig.cjs.json ./tsconfig.esm.json ./tsconfig.types.json", |
# cheap-di | ||
JavaScript's dependency injection like Autofac in .Net | ||
JavaScript's dependency injection is like Autofac in .Net | ||
* [Installation](#installation) | ||
* [How to use](#how-to-use) | ||
* [Using with abstractions](#using-abstractions) | ||
* [Registration variants](#registration-variants) | ||
@@ -11,3 +12,3 @@ * [registerImplementation](#register-implementation) | ||
## <a name="isntallation"></a> Installation | ||
## <a name="installation"></a> Installation | ||
@@ -20,6 +21,62 @@ ```shell | ||
The recommended way of using this package is using it with code transformers like `cheap-di-ts-transform`. Because in this way you will get the true dependency injection: | ||
The recommended way of using this package is to use it with code transformers like <a href="https://github.com/tomas-light/cheap-di/tree/master/packages/cheap-di-ts-transform">cheap-di-ts-transform</a>. Because in this way, you will get true dependency injection: | ||
```ts | ||
import { container } from 'cheap-di'; | ||
class MyLogger { | ||
log(...message: unknown[]) { | ||
console.log(...message); | ||
} | ||
} | ||
class MyService { | ||
constructor(private logger: MyLogger) {} | ||
someStaff() { | ||
this.logger.log('I do something'); | ||
} | ||
} | ||
/** | ||
* With cheap-di-ts-transform here will be added metadata about Service dependencies. | ||
* */ | ||
// in this case you have no abstractions, so you don't need to registrate something yourself, | ||
// just resolve your target: | ||
const myService = container.resolve(MyService); | ||
if (myService) { | ||
myService.someStaff(); | ||
} | ||
``` | ||
If you can't use <a href="https://github.com/tomas-light/cheap-di/tree/master/packages/cheap-di-ts-transform">cheap-di-ts-transform</a>, you have to use `@inject` decorator to define your dependencies (<i>it supports stage 2 and stage 3 TypeScript syntax</i>): | ||
```ts | ||
import { container, inject } from 'cheap-di'; | ||
class MyLogger { | ||
log(...message: unknown[]) { | ||
console.log(...message); | ||
} | ||
} | ||
@inject(MyLogger) | ||
class MyService { | ||
constructor(private logger: MyLogger) {} | ||
someStaff() { | ||
this.logger.log('I do something'); | ||
} | ||
} | ||
// in this case you have no abstractions, so you don't need to registrate something yourself, | ||
// just resolve your target: | ||
const myService = container.resolve(MyService); | ||
if (myService) { | ||
myService.someStaff(); | ||
} | ||
``` | ||
## <a name="using-abstractions"></a> Using with abstractions | ||
> Note: | ||
> You may wonder, why we use abstract classes? | ||
> You may wonder why we use abstract classes? | ||
> - It is syntax analog of interface. | ||
@@ -33,2 +90,3 @@ > | ||
```ts | ||
// logger.ts | ||
abstract class Logger { | ||
@@ -45,2 +103,6 @@ abstract debug: (message: string) => void; | ||
} | ||
``` | ||
```ts | ||
// service.ts | ||
import { Logger } from './logger'; | ||
@@ -54,15 +116,16 @@ class Service { | ||
} | ||
/** | ||
* With cheap-di-ts-transform here will be added metadata about Service dependencies. | ||
* */ | ||
``` | ||
```ts | ||
// somewhere in your application initialization | ||
import { container } from 'cheap-di'; | ||
import { Logger, ConsoleLogger } from './logger'; | ||
const myLogPrefix = 'INFO: '; | ||
container.registerImplementation(ConsoleLogger).as(Logger).inject(myLogPrefix); | ||
``` | ||
```ts | ||
// somewhere inside your code | ||
// or you may use some middleware to do this, to get rid of Service Locator antipattern | ||
import { container } from 'cheap-di'; | ||
import { Service } from './service'; | ||
@@ -73,3 +136,3 @@ const service = container.resolve(Service); | ||
But if you can't use transformers you still may use cheap-di with `@inject` decorator (it supports stage 2 and stage 3 TypeScript syntax): | ||
Without <a href="https://github.com/tomas-light/cheap-di/tree/master/packages/cheap-di-ts-transform">cheap-di-ts-transform</a>, you still may use cheap-di with `@inject` decorator (it supports stage 2 and stage 3 TypeScript syntax): | ||
@@ -108,5 +171,7 @@ ```ts | ||
} | ||
``` | ||
```ts | ||
// somewhere | ||
import { container } from 'cheap-di'; | ||
import { InfoLogger, ErrorLogger, ConsoleLogger } from './logger'; | ||
@@ -118,6 +183,8 @@ const infoPrefix = 'INFO: '; | ||
container.registerImplementation(ConsoleLogger).as(ErrorLogger).inject(errorPrefix); | ||
``` | ||
```ts | ||
// somewhere in inside your code | ||
// or you may use some middleware to do this, to get rid of Service Locator antipattern | ||
import { container } from 'cheap-di'; | ||
import { Service } from './service'; | ||
@@ -128,3 +195,3 @@ const service = container.resolve(Service); | ||
To use stage 2 decorators you need to adjust your tsconfig.json like: | ||
To use stage 2 decorators, you need to adjust your tsconfig.json like this: | ||
```json | ||
@@ -139,3 +206,3 @@ { | ||
To use stage 3 decorators you don't need extra setup. | ||
To use stage 3 decorators, you don't need extra setup. | ||
@@ -182,3 +249,3 @@ ## <a name="registration-variants"></a> Registration variants | ||
And singleton also may be used with interface specification: | ||
And singletons may also be used with interface specifications: | ||
```ts | ||
@@ -215,3 +282,3 @@ import { container } from 'cheap-di'; | ||
If you want to register some instance as interface. Result is similar to singleton registration except the fact you have to instantiate class by your self | ||
If you want to register some instance as an interface, the result is similar to singleton registration, except that you have to instantiate the class yourself. | ||
@@ -218,0 +285,0 @@ ```ts |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
109512
310
926