InversifyJS is participating 2021 OSC China Open Source Project Selection , please vote for it!
InversifyJS 2021 OSC China Open Source Project Selection {{projectVoteCount} has been obtained in, please vote for it!
2021 OSC China Open Source Project Selection It is in hot progress. Come and vote for your favorite open source project!
2021 OSC China Open Source Project Selection>>> Midfield Review
InversifyJS won the 2021 OSC China Open Source Project Selection "The Best Popularity Project" !
Authorization Agreement MIT
development language TypeScript View source code »
operating system Cross platform
Software type Open source software
Open source organizations nothing
region Unknown
deliverer terenceyhj
intended for unknown
Recording time 2021-05-20

Software Introduction

InversifyJS A powerful and lightweight inversion control (IoC) container, suitable for JavaScript and Node.js applications written by TypeScript. It uses class constructors to define and inject its dependencies. InversifyJS API is friendly and easy to understand, and encourages the application of OOP and IoC best practices.

InversifyJS has four main goals:

  1. Allows JavaScript developers to write code that follows the SOLID principle.

  2. Promote and encourage adherence to best object-oriented programming and dependency injection practices.

  3. Minimize runtime overhead.

  4. Provide artistic programming experience and ecology.

use

Step 1: Define the interface

 // file interfaces.ts export interface Warrior { fight(): string; sneak(): string; } export interface Weapon { hit(): string; } export interface ThrowableWeapon { throw(): string; }

Step 2: Define dependency

 // file entities.ts import { injectable, inject } from "inversify"; import "reflect-metadata"; import { Weapon, ThrowableWeapon, Warrior } from "./ interfaces"; import { TYPES } from "./types"; @injectable() class Katana implements Weapon { public hit() { return "cut! "; } } @injectable() class Shuriken implements ThrowableWeapon { public throw() { return "hit! "; } } @injectable() class Ninja implements Warrior { private _katana: Weapon; private _shuriken: ThrowableWeapon; public constructor( @inject(TYPES.Weapon) katana: Weapon, @inject(TYPES.ThrowableWeapon) shuriken: ThrowableWeapon ) { this._katana = katana; this._shuriken = shuriken; } public fight() { return this._katana.hit(); } public sneak() { return this._shuriken.throw(); } } export { Ninja, Katana, Shuriken };

Step 3: Create and configure the IOC container

 // file inversify.config.ts import { Container } from "inversify"; import { TYPES } from "./types"; import { Warrior, Weapon, ThrowableWeapon } from "./ interfaces"; import { Ninja, Katana, Shuriken } from "./ entities"; const myContainer = new Container(); myContainer.bind<Warrior>(TYPES. Warrior).to(Ninja); myContainer.bind<Weapon>(TYPES. Weapon).to(Katana); myContainer.bind<ThrowableWeapon>(TYPES. ThrowableWeapon).to(Shuriken); export { myContainer };

Step 4: Dependency resolution

 import { myContainer } from "./inversify.config"; import { TYPES } from "./types"; import { Warrior } from "./interfaces"; const ninja = myContainer.get<Warrior>(TYPES. Warrior); expect(ninja.fight()).eql("cut!"); //  true expect(ninja.sneak()).eql("hit!"); //  true
Expand to read the full text

code

Gitee index of is
exceed Items for

comment

Click to join the discussion 🔥 (2) Post and join the discussion 🔥
No content temporarily
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
No more
No content temporarily
Issued a question and answer
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
No more
No content temporarily
No content temporarily
two comment
fourteen Collection
 OSCHINA
Log in to view more high-quality content
 Back to top
Top