-
Notifications
You must be signed in to change notification settings - Fork 12
Open
Labels
documentationImprovements or additions to documentationImprovements or additions to documentationenhancementNew feature or requestNew feature or request
Description
Hey, @vovaspace
This line violates Dependcy Inversion Principle:
injected(ApiService, TOKENS.apiKey); |
The ES-module with definition of ApiService
class depends on infrastructure layer: your library brandi
and on the module structue: TOKENS
.
Just move this line to the composition root and everything becomes well again:
import { DependencyModule, injected } from 'brandi';
import { TOKENS } from '../tokens';
import { ApiService } from './ApiService';
export const apiModule = new DependencyModule();
apiModule.bind(TOKENS.apiKey).toConstant('#key9428');
injected(ApiService, TOKENS.apiKey); // << this code should be here
apiModule.bind(TOKENS.apiService).toInstance(ApiService).inTransientScope();
All dependency resolution rules should be placed in single place: composition root (see: Dependency Injection Principles, Practices, and Patterns by Steven van Deursen and Mark Seemann)
And if you are going to move it to the composition root, just add the inject
parameter to the container methods: toFactory
and toInstance
:
import { DependencyModule } from 'brandi';
import { TOKENS } from '../tokens';
import { ApiService } from './ApiService';
export const apiModule = new DependencyModule();
apiModule.bind(TOKENS.apiKey).toConstant('#key9428');
apiModule
.bind(TOKENS.apiService)
.toInstance(ApiService, TOKENS.apiKey)
.inTransientScope();
Metadata
Metadata
Assignees
Labels
documentationImprovements or additions to documentationImprovements or additions to documentationenhancementNew feature or requestNew feature or request