Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DIP Violation #28

Open
DScheglov opened this issue Oct 11, 2022 · 2 comments
Open

DIP Violation #28

DScheglov opened this issue Oct 11, 2022 · 2 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@DScheglov
Copy link

DScheglov commented Oct 11, 2022

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();
@vovaspace
Copy link
Owner

Hi!

That is great suggestion! I'm working on the next Brandi version and will try to implement that.

@vovaspace vovaspace added documentation Improvements or additions to documentation enhancement New feature or request labels Oct 14, 2022
@polerin
Copy link

polerin commented Nov 29, 2022

This is actually what I'm doing in my library usage of brandi. I'm exporting a set of tokens, a dependency module, and a default set of injected() registrations that can be used collectively or rebound by the consuming application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants