GithubHelp home page GithubHelp logo

Comments (8)

L8D avatar L8D commented on August 23, 2024

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.

micha149 avatar micha149 commented on August 23, 2024

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

from di.js.

L8D avatar L8D commented on August 23, 2024

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

from di.js.

micha149 avatar micha149 commented on August 23, 2024

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.

L8D avatar L8D commented on August 23, 2024

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.

micha149 avatar micha149 commented on August 23, 2024

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.

L8D avatar L8D commented on August 23, 2024

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

from di.js.

micha149 avatar micha149 commented on August 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.