Comments (8)
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);
from di.js.
Did not work for me… new NameFactory()
returns an instance of NameFactory {}
instead of the expected string…
from di.js.
@micha149 yeah I just realized that primitives wouldn't be supported. You can still return an object from NameFactory
however
from di.js.
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…
from di.js.
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.
from di.js.
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
from di.js.
@micha149 how would this be any more messy than adding annotations to existing classes?
from di.js.
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.
from di.js.
Related Issues (20)
- Mock providers not cached HOT 3
- Document TransientScope HOT 2
- annotate helper and SuperConstructor doesn't work HOT 1
- Using di2 in ES6 project with Traceur HOT 1
- Chrome Extension
- Non-module constructor parameter HOT 1
- InjectPromise HOT 8
- Invalid assumption of a class to be a factory
- minification issues HOT 13
- Exception: Constructors of derived class must contain a super call when the memberVariables option is enabled HOT 1
- example how to use it with typescript HOT 3
- DI.js dead? HOT 3
- Using angular/di.js in a babel runtime gives error HOT 23
- What about reusable classes? HOT 2
- import real module when writing a mock sounds ugly HOT 2
- Question: interfaces
- Alive? HOT 10
- Meet - refactoring di.js to TypeScript version - `ts-di` =) HOT 1
- node main.js Error
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from di.js.