inversifyjs

Binding classes with property injection to kernel in inversify

拥有回忆 提交于 2021-01-04 05:33:49
问题 I am trying to achieve dependency injection on my node project via inversify. I have a kernel config like this: inversify.config.ts import "reflect-metadata"; import {Kernel, interfaces} from "inversify"; import {Config} from "./config"; import {Collection} from "./collection"; let kernel = new Kernel(); kernel.bind<Config>("Config").to(Config).inSingletonScope(); // kernel.bind<interfaces.Newable<Collection>>("Collection").toConstructor<Collection>(Collection); // it works without the line

Factory with parameters in InversifyJS

浪尽此生 提交于 2020-06-27 14:36:10
问题 I'm using InversifyJS with TypeScript. Let's say I have a class which has a mixture of injected and non-injected constructor parameters: @injectable() export class MyClass { constructor( foo: Foo, // This thing isn't injectable @inject(Bar) bar: Bar // This thing is ){ ... } } I would like to inject a factory for this class into some other class and then invoke it with a value for the first parameter. @injectable() export class SomeOtherClass { constructor( @inject("Factory<MyClass>")

InfersifyJS - how to conditionally load constantValue

一世执手 提交于 2019-12-25 01:12:36
问题 I am using inversifyJS in my typescript + NODE.js application. I have for different environments different configurations: Configuration production const CONFIG_PRODUCTION: Object = { PATH_TO_STATIC_FILES: '../web/build', SECURE_COOKIE: false }; export default CONFIG_PRODUCTION; Configuration development const CONFIG_DEVELOPMENT: Object = { PATH_TO_STATIC_FILES: 'build/web/build', SECURE_COOKIE: true }; export default CONFIG_DEVELOPMENT; Now on my infersify container configuration my setup

What does it mean when a class implements itself in Typescript

萝らか妹 提交于 2019-12-24 07:58:38
问题 I'm trying to add dependency injection to a plain Typescript project, found an npm package called inversify. So looking at the examples I came across this code: import { Container, injectable, inject } from "inversify"; @injectable() class Katana { public hit() { return "cut!"; } } @injectable() class Shuriken { public throw() { return "hit!"; } } @injectable() class Ninja implements Ninja { private _katana: Katana; private _shuriken: Shuriken; public constructor(katana: Katana, shuriken:

InversifyJS Injecting Literal Constructor Parameters

不羁的心 提交于 2019-12-21 06:24:15
问题 Is it possible to get the below behaviour with InversifyJS: constructor(IDependency resolvedDependency, string myLiteral) ^ ^ Automatically resolve Defined Literal If so, what's the best way to go about it? 回答1: There are a few things that you can do. Here I will post a few of them... Injecting the literal as a constant value let TYPES = { IWarrior: Symbol("IWarrior"), IWeapon: Symbol("IWeapon"), rank: Symbol("rank") } interface IWeapon {} @injectable() class Katana implements IWeapon {}

How to dynamically bind a dynamically imported type

萝らか妹 提交于 2019-12-13 02:19:15
问题 I am trying to have templates automagically wired to an inversifyjs container but whatever I try it's not working. Please help? private templates = [ {file: './component.html.tpl', obj: 'HtmlTemplate'}, {file: './component.tpl.ts', obj: 'ComponentTemplate'} ]; private container = new Container(); bind(){ // original and working code // this.container.bind<ITemplate>('HtmlTemplate').to(HtmlTemplate); // this.container.bind<ITemplate>('ComponentTemplate').to(ComponentTemplate); this.templates

How to inject an asynchronous dependency in inversify?

心不动则不痛 提交于 2019-12-07 01:37:12
问题 I have TypeScript application and I'm using Inversify for IoC. I have a connection class: 'use strict'; import { injectable } from 'inversify'; import { createConnection, Connection } from "typeorm"; import { Photo, PhotoMetadata, Author, Album } from '../index'; @injectable() class DBConnectionManager { public createPGConnection(): Promise<Connection> { return createConnection({ driver: { type: "postgres", host: "host", port: 5432, username: "username", password: "password", database:

In Inversify, why to prefer Constructor/Factory injection over toDynamicValue?

戏子无情 提交于 2019-12-06 05:09:07
问题 In InversifyJS, are there any specific advantages of following the approaches outlined in factory injection guide and constructor injection guide for injecting factories and constructors respectively over just using toDynamicValue. 回答1: toConstructor If you use a toConstructor you will be able to pass arguments to the constructor but you won't be able to resolve those arguments (unless you inject them as well). container.bind<interfaces.Newable<Katana>>("Newable<Katana>") .toConstructor

In Inversify, why to prefer Constructor/Factory injection over toDynamicValue?

守給你的承諾、 提交于 2019-12-04 09:36:39
In InversifyJS , are there any specific advantages of following the approaches outlined in factory injection guide and constructor injection guide for injecting factories and constructors respectively over just using toDynamicValue . toConstructor If you use a toConstructor you will be able to pass arguments to the constructor but you won't be able to resolve those arguments (unless you inject them as well). container.bind<interfaces.Newable<Katana>>("Newable<Katana>") .toConstructor<Katana>(Katana); toDynamicValue If you use a toDynamicValue you will be able to pass constructor arguments to

InversifyJS Injecting Literal Constructor Parameters

≡放荡痞女 提交于 2019-12-03 20:40:47
Is it possible to get the below behaviour with InversifyJS: constructor(IDependency resolvedDependency, string myLiteral) ^ ^ Automatically resolve Defined Literal If so, what's the best way to go about it? There are a few things that you can do. Here I will post a few of them... Injecting the literal as a constant value let TYPES = { IWarrior: Symbol("IWarrior"), IWeapon: Symbol("IWeapon"), rank: Symbol("rank") } interface IWeapon {} @injectable() class Katana implements IWeapon {} interface IWarrior { weapon: IWeapon; rank: string; } @injectable() class Warrior implements IWarrior { public