Comparing version 1.1.0 to 2.0.0
@@ -1,2 +0,3 @@ | ||
export * from './container'; | ||
export * from './ContainerImpl'; | ||
export * from './dependencies'; | ||
export * from './types'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./container"), exports); | ||
tslib_1.__exportStar(require("./ContainerImpl"), exports); | ||
tslib_1.__exportStar(require("./dependencies"), exports); | ||
tslib_1.__exportStar(require("./types"), exports); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "cheap-di", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "JavaScript dependency injection like Autofac in .Net", | ||
@@ -10,7 +10,7 @@ "scripts": { | ||
"devDependencies": { | ||
"@types/jest": "^26.0.14", | ||
"jest": "^26.4.2", | ||
"ts-jest": "^26.4.1", | ||
"tslib": "2.0.1", | ||
"typescript": "3.9.7" | ||
"@types/jest": "26.0.23", | ||
"jest": "26.6.3", | ||
"ts-jest": "26.5.6", | ||
"tslib": "2.2.0", | ||
"typescript": "4.2.4" | ||
}, | ||
@@ -17,0 +17,0 @@ "files": [ |
173
README.md
@@ -6,3 +6,3 @@ # cheap-di | ||
You have an interface and its implementation | ||
You have an interface (base/abstract class) and its implementation (derived class) | ||
@@ -12,15 +12,13 @@ `user-repository.js` | ||
export class UserRepository { | ||
constructor() { | ||
if (new.target === UserRepository) { | ||
throw new TypeError('Cannot construct UserRepository instance directly'); | ||
} | ||
constructor() { | ||
if (new.target === UserRepository) { | ||
throw new TypeError('Cannot construct UserRepository instance directly'); | ||
} | ||
list() { | ||
throw new Error("Not implemented"); | ||
} | ||
getById(userId) { | ||
throw new Error("Not implemented"); | ||
} | ||
} | ||
list() { | ||
throw new Error("Not implemented"); | ||
} | ||
getById(userId) { | ||
throw new Error("Not implemented"); | ||
} | ||
} | ||
@@ -34,24 +32,21 @@ ``` | ||
export class FakeUserRepository extends UserRepository { | ||
constructor() { | ||
super(); | ||
this.users = [ | ||
{ | ||
id: 1, | ||
name: 'user-1' | ||
}, | ||
{ | ||
id: 2, | ||
name: 'user-2' | ||
}, | ||
]; | ||
} | ||
list() { | ||
return this.users; | ||
} | ||
getById(userId) { | ||
return this.users.find(user => user.id === userId); | ||
} | ||
constructor() { | ||
super(); | ||
this.users = [ | ||
{ | ||
id: 1, | ||
name: 'user-1' | ||
}, | ||
{ | ||
id: 2, | ||
name: 'user-2' | ||
}, | ||
]; | ||
} | ||
list() { | ||
return this.users; | ||
} | ||
getById(userId) { | ||
return this.users.find(user => user.id === userId); | ||
} | ||
} | ||
@@ -61,8 +56,8 @@ ``` | ||
There is simple logger. | ||
`loger.js` | ||
`logger.js` | ||
```js | ||
export class Logger { | ||
debug(message) { | ||
throw new Error("Not implemented"); | ||
} | ||
debug(message) { | ||
throw new Error("Not implemented"); | ||
} | ||
} | ||
@@ -76,9 +71,9 @@ ``` | ||
export class ConsoleLogger extends Logger { | ||
constructor(prefix) { | ||
this.prefix = prefix; | ||
} | ||
debug(message) { | ||
console.log(`${this.prefix}: ${message}`); | ||
} | ||
constructor(prefix) { | ||
super(); | ||
this.prefix = prefix; | ||
} | ||
debug(message) { | ||
console.log(`${this.prefix}: ${message}`); | ||
} | ||
} | ||
@@ -88,4 +83,4 @@ ``` | ||
You have the repository consumer. | ||
To allow DI container inject dependencies in your consumer class you should specify `__constructorParams` static property. | ||
That property should contain instance types array in the order of your constructor. | ||
To allow DI container inject dependencies in your consumer class you should specify `__dependencies` static property. | ||
That property should contain constructor array in the order of your constructor. | ||
@@ -98,17 +93,17 @@ `user-service.js` | ||
export class UserService { | ||
static __constructorParams = [ UserRepository, Logger ]; | ||
constructor(userRepository, logger) { | ||
this.userRepository = userRepository; | ||
this.logger = logger; | ||
} | ||
list() { | ||
this.logger.debug('Access to users list'); | ||
return this.userRepository.list(); | ||
} | ||
get(userId) { | ||
return this.userRepository.getById(userId); | ||
} | ||
static __dependencies = [ UserRepository, Logger ]; | ||
constructor(userRepository, logger) { | ||
this.userRepository = userRepository; | ||
this.logger = logger; | ||
} | ||
list() { | ||
this.logger.debug('Access to users list'); | ||
return this.userRepository.list(); | ||
} | ||
get(userId) { | ||
return this.userRepository.getById(userId); | ||
} | ||
} | ||
@@ -144,2 +139,54 @@ ``` | ||
## TypeScript | ||
`logger.ts` | ||
```ts | ||
export abstract class Logger { | ||
abstract debug: (message: string) => void; | ||
} | ||
``` | ||
`console-logger.ts` | ||
```ts | ||
import { Logger} from './logger'; | ||
export class ConsoleLogger extends Logger { | ||
constructor(prefix) { | ||
super(); | ||
this.prefix = prefix; | ||
} | ||
debug(message: string) { | ||
console.log(`${this.prefix}: ${message}`); | ||
} | ||
} | ||
``` | ||
`@dependencies` decorator can be used to simplify dependency syntax | ||
`user-service.ts` | ||
```ts | ||
import { dependencies } from 'cheap-di'; | ||
import { Logger } from './logger'; | ||
import { UserRepository } from './user-repository'; | ||
@dependencies(UserRepository, Logger) | ||
export class UserService { | ||
constructor( | ||
private userRepository: UserRepository, | ||
private logger: Logger | ||
) { | ||
} | ||
list() { | ||
this.logger.debug('Access to users list'); | ||
return this.userRepository.list(); | ||
} | ||
get(userId) { | ||
return this.userRepository.getById(userId); | ||
} | ||
} | ||
``` | ||
You can see more examples in `cheap-di/src/container.test.ts` |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
185
16680
15
161
1