How to use the inversify.Kernel function in inversify

To help you get started, we’ve selected a few inversify examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / inversify / inversify-tests.ts View on Github external
kernel.unbind("INinja");
    kernel.unbindAll();

    // Kernel modules
    let module: IKernelModule = (k: IKernel) => {
        k.bind("INinja").to(Ninja);
        k.bind("IKatana").to(Katana).inTransientScope();
        k.bind("IShuriken").to(Shuriken).inSingletonScope();
    };

    let options: IKernelOptions = {
        middleware: [],
        modules: [module]
    };

    kernel = new Kernel(options);
    let ninja2 = kernel.get("INinja");
    console.log(ninja2);

    // binding types
    kernel.bind("IKatana").to(Katana);
    kernel.bind("IKatana").toValue(new Katana());

    kernel.bind>("IKatana").toConstructor(Katana);

    kernel.bind>("IKatana").toFactory((context) => {
        return () => {
            return kernel.get("IKatana");
        };
    });

    kernel.bind>("IKatana").toAutoFactory();
github DefinitelyTyped / DefinitelyTyped / inversify-inject-decorators / inversify-inject-decorators-tests.ts View on Github external
public secondaryWeapon: Weapon;

  }

  kernel.bind(TYPES.Weapon).to(Sword).whenTargetTagged("throwwable", false);
  kernel.bind(TYPES.Weapon).to(Shuriken).whenTargetTagged("throwwable", true);

  let warrior = new Warrior();
  console.log(warrior.primaryWeapon instanceof Sword); // true
  console.log(warrior.primaryWeapon instanceof Shuriken); // true

}

module lazyMultiInject {

  let kernel = new Kernel();
  let { lazyMultiInject } = getDecorators(kernel);
  let TYPES = { Weapon: "Weapon" };

  interface Weapon {
      name: string;
      durability: number;
      use(): void;
  }

  @injectable()
  class Sword implements Weapon {
      public name: string;
      public durability: number;
      public constructor() {
          this.durability = 100;
          this.name = "Sword";
github inversify / inversify-binding-decorators / type_definitions / inversify-binding-decorators / inversify-binding-decorators-tests.ts View on Github external
/// 

import { inject, Kernel } from "inversify";
import { autoProvide, makeProvideDecorator, makeFluentProvideDecorator } from "inversify-binding-decorators";

module decorator {
    let kernel = new Kernel();
    let provide = makeProvideDecorator(kernel);

    interface INinja {
        fight(): string;
        sneak(): string;
    }

    interface IKatana {
        hit(): string;
    }

    interface IShuriken {
        throw(): string;
    }

    let TYPE = {
github DefinitelyTyped / DefinitelyTyped / inversify-express-utils / inversify-express-utils-tests.ts View on Github external
/// 

import { InversifyExpressServer, Controller, Get, All, Delete, Head, Put, Patch, Post, Method, TYPE } from "inversify-express-utils";
import * as express from "express";
import { Kernel } from "inversify";

let kernel = new Kernel();

module server {

    let server = new InversifyExpressServer(kernel);

    server
        .setConfig((app: express.Application) => {
            app.use((req: express.Request, res: express.Response, next: express.NextFunction) => {
                console.log("hello world");
                next();
            });
        })
        .setErrorConfig((app: express.Application) => {
            app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
                console.error(err.stack);
                res.status(500).send("Something broke!");
github inversify / inversify-binding-decorators / type_definitions / inversify-binding-decorators / inversify-binding-decorators-tests.ts View on Github external
this._katana = katana;
            this._shuriken = shuriken;
        }

        public fight() { return this._katana.hit(); };
        public sneak() { return this._shuriken.throw(); };

    }

    let ninja = kernel.get(TYPE.INinja);
    console.log(ninja);

}

module fluent_decorator {
    let kernel = new Kernel();
    let provide = makeFluentProvideDecorator(kernel);

    let provideSingleton = function(identifier: string) {
        return provide(identifier).inSingletonScope().done();
    };

    let provideTransient = function(identifier: string) {
        return provide(identifier).done();
    };

    interface INinja {
        fight(): string;
        sneak(): string;
    }

    interface IKatana {
github DefinitelyTyped / DefinitelyTyped / inversify-restify-utils / inversify-restify-utils-tests.ts View on Github external
import {
    InversifyRestifyServer, Controller, Get, Options,
    Delete, Head, Put, Patch, Post, Method, TYPE
} from "inversify-restify-utils";

import * as restify from "restify";
import { Kernel } from "inversify";

let kernel = new Kernel();

module server {

    let server = new InversifyRestifyServer(kernel);

    server
        .setConfig((app: restify.Server) => {
            app.use((req: restify.Request, res: restify.Response, next: restify.Next) => {
                console.log("hello world");
                next();
            });
        })
        .build()
        .listen(3000, "localhost");
}
github tiagomestre / inversify-tracer / examples / proto.ts View on Github external
class Ninja implements Warrior {

    public speed: number = 10;

    private weapon: Weapon;

    constructor(@inject('Weapon') weapon: Weapon) {
        this.weapon = weapon;
    }

    public attack(value: number, otherValue: string) {
        return this.weapon.use(value);
    }
}

let kernel = new Kernel();

kernel.bind('Weapon').to(Katana);
kernel.bind('Warrior').to(Ninja);

const tracer = new InversifyTracer({
    filters: ['Katana:*']
});

tracer.on('call', (callInfo: CallInfo) => {
    let comb = callInfo.parameters.map((param: any, i: number) => { return `${param}: ${callInfo.arguments[i]}`; });
    console.log(`${new Date().toISOString()} ${callInfo.className} ${callInfo.methodName} called ${comb}`);
});

tracer.on('return', (returnInfo: ReturnInfo) => {
    console.log(`${new Date().toISOString()} ${returnInfo.className} ${returnInfo.methodName} returned ${returnInfo.result}`);
});
github Talento90 / typescript-node / src / libs / ioc / index.ts View on Github external
import "reflect-metadata";
import { Kernel } from "inversify";

const env = process.env.NODE_ENV || "dev";
var kernel = new Kernel();

//Register ioc
require(`./inversify.${env}.config`).default(kernel);

export default kernel;
github smartive / giuseppe / core / IoC.ts View on Github external
import { InitialConfiguration } from './InitialConfiguration';
import { DefaultParamHandler } from './DefaultParamHandler';
import { DefaultRegistrar } from './DefaultRegistrar';
import { DefaultRouteHandler } from './DefaultRouteHandler';
import { IoCSymbols } from './IoCSymbols';
import { Kernel } from 'inversify';

const kernel = new Kernel();

kernel.bind(IoCSymbols.registrar).to(DefaultRegistrar).inSingletonScope();
kernel.bind(IoCSymbols.paramHandler).to(DefaultParamHandler).inSingletonScope();
kernel.bind(IoCSymbols.routeHandler).to(DefaultRouteHandler).inSingletonScope();
kernel.bind(IoCSymbols.configuration).to(InitialConfiguration).inSingletonScope();

export const IocContainer = kernel;