0

I'm trying to design a system that inject configuration of each component into each class.

project structure:

 |features --|component-a component-a.service.js component-a.config.json --|component-b component-b.service.js component-b.config.json --|component-c component-c.service.js component-c.config.json 

I think about 2 approaches to inject the config:

  1. Create a class config which receives the name of the component and locate it, then initialize the attributes from the component-x.config.json file, and each component-xclass inherits from the class config.

  2. Use an injector that locate the configuration file for the dependent class when it will be needed for the client.

  3. Load the config file for each component and initialize the attributes inside the component constructor explicitly.

Which approach will be better to use with dependency injection pattern?

1 Answer 1

2

Approach (1) would couple all your service classes to your dependency injection container which, in my opinion, defeats the purpose. You would like to have services that know nothing about the outside world, they just receive configuration through their constructor/setters.

I think (2) is a better approach. Your services would be self-contained and decoupled from the DI container itself.

You could take a look at how others implement DI here. For example, Spring allows using configuration files as you do. Others use annotations to configure their classes.

2
  • 1
    More typical for JavaScript, this "injector" would be called a factory object: ComponentFactory. Decoupling the dependency injection container from component creation and configuration is definitely desirable in this case. Commented Sep 20, 2021 at 13:06
  • @GregBurghardt so should I add the config attributes to each component via the constructor? Commented Sep 20, 2021 at 14:11

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.