di.js icon indicating copy to clipboard operation
di.js copied to clipboard

How to inject literal values (like strings)?

Open bahmutov opened this issue 11 years ago • 8 comments

Hi,

I am trying working through DI.js ES5 examples under node, but got stuck trying to inject simple string values. For example, let us create Foo with single string argument

var di = require('di');
function Foo(name) {
  this.name = name;
}
di.annotate(Foo, new di.Inject('name'));
// ?
var foo = injector.get(Foo);

I have problem writing step // ? where I would provide values like 'John' or 'Mary'. Any advice would be appreciated.

bahmutov avatar Feb 12 '15 06:02 bahmutov

You would use a factory like so:

var di = require('di');

di.annotate(Foo, new di.Inject(NameFactory));
function Foo(name) {
  this.name = name;
}

function NameFactory() {
  return 'John';
}

var injector = new di.Injector();
var foo = injector.get(Foo);

L8D avatar May 18 '15 20:05 L8D

Did not work for me… new NameFactory() returns an instance of NameFactory {} instead of the expected string…

micha149 avatar Jun 30 '15 07:06 micha149

@micha149 yeah I just realized that primitives wouldn't be supported. You can still return an object from NameFactory however

L8D avatar Jun 30 '15 07:06 L8D

OK, but this is not suitable. If you use classes from a third party module, you want to pass a string if the class expects it. Maybe something like a new StaticInject(someStaticValue) should be implemented which always resolves in the passed value…

micha149 avatar Jun 30 '15 07:06 micha149

If you have a class from a third party module you should be able to wrap it with a factory:

import {ThirdPartyClass} from 'third-party';

function ThirdPartyWrapper() {
  return new ThirdPartyClass(someStaticValue);
}

This is the recommended approach for interfacing with third parties since it's usually not a good policy to add annotations to existing constructors.

L8D avatar Jun 30 '15 07:06 L8D

OK, but this would end in a mess, when I'm using modules with a lot of classes. Maybe angular/di.js is still not the injector of choice for non-angular projects… thanks

micha149 avatar Jun 30 '15 07:06 micha149

@micha149 how would this be any more messy than adding annotations to existing classes?

L8D avatar Jun 30 '15 20:06 L8D

You're right. Dependency injection is always some kind of messy because we need to configure how our dependencies have to be plugged together. But wrapping each third party class/constructor into a factory function will make this more complicated. At first, we need to write all these factories. At second any developer have to keep in mind to use the Wrapper instead of the actual constructor to add this third party class as a dependency.

This could be a di setup for a third party kitchen module

// services.js
import { Kitchen, CoffeeMaker, Grinder, Pump, Heater, Skillet, Stove, Fridge, Dishwasher } from 'third-party-kitchen';
import { Electricity } from './src/electricity';
import { annotate, Inject } from 'di';

annotate(Grinder, new Inject(Electricity));
annotate(Heater, new Inject(Electricity));
annotate(Pump, new Inject(Heater), new Inject(Electricity));
annotate(CoffeeMaker, new Inject(Grinder), new Inject(Pump), new Inject(Heater));

annotate(Stove, new Inject(Electricity));
annotate(Fridge, new Inject(Electricity));
annotate(Dishwasher, new Inject(Electricity));

annotate(Kitchen, new Inject(CoffeeMaker), new Inject(Skillet), new Inject(Stove), new Inject(Fridge), new Inject(Dishwasher));

Now, imagine each of this Classes is wrapped with a factory. And additionally, we have to export these wrappers to use it with our own classes.

// services.js

function StoveWrapper(electricity) {
    return new Stove(electricity);
}
annotate(StoveWrapper, new Inject(Electricity));

function FridgetWrapper(electricity) {
    return new Stove(electricity);
}
annotate(FridgetWrapper, new Inject(Electricity));

// etc.

function KitchenWrapper(coffeMaker, skillet, stove, fridge, dishwasher) {
    return new Kitchen(coffeMaker, skillet, stove, fridge, dishwasher)
}
annotate(KitchenWrapper, new Inject(StoveWrapper), new Inject(FridgetWrapper) /* ... etc */);

This would become a hard job. Maybe I am a little bit spoilt from the symfony dependency injection component, but a generic dependency injection component should give the developer all necessary tools to be comfortable with defining the service structure of his app.

And this – to get back to the topic of this issue – is also to be able to inject primitives into a constructor.

micha149 avatar Jul 01 '15 11:07 micha149