Code Monkey home page Code Monkey logo

inversify-basic-example's Introduction

inversify-basic-example

Join the chat at https://gitter.im/inversify/inversify-basic-example Build Status Dependencies img img

A basic example that showcases how to setup InversifyJS

This is a very basic InversifyJS example.

This program declares:

  • Three interfaces Warrior, Weapon and Battle.
  • Two implementations of Weapon: Katana and Shuriken
  • Two implementations of Warrior: Ninja and Samurai
  • One implementation of Battle: EpicBattle.

The warriors own a weapon and are tagged with some metadata.

We use some constraints whenTargetNamed and whenParentNamed to indicate which Weapon should be injected into and implementation of Warrior and which implementation of Warrior should injected into EpicBattle:

container.bind<Warrior>(SERVICE_IDENTIFIER.WARRIOR).to(Ninja).whenTargetNamed(TAG.CHINESE);
container.bind<Warrior>(SERVICE_IDENTIFIER.WARRIOR).to(Samurai).whenTargetNamed(TAG.JAPANESE);
container.bind<Weapon>(SERVICE_IDENTIFIER.WEAPON).to(Shuriken).whenParentNamed(TAG.CHINESE);
container.bind<Weapon>(SERVICE_IDENTIFIER.WEAPON).to(Katana).whenParentNamed(TAG.JAPANESE);
container.bind<Battle>(SERVICE_IDENTIFIER.BATTLE).to(EpicBattle);

How can I run it?

You can clone it using:

$ git clone https://github.com/inversify/inversify-basic-example.git

To run this example you need to install some dependencies:

$ cd inversify-basic-example
$ npm install

And compile the TypeScript code into JavaScript code:

$ gulp

The generated code is available at the dist directory.

At this point you are ready to run the example:

$ node dist/main.js

You should see the following in console:

FIGHT!
                Ninja (Shuriken)
                vs
                Samurai (Katana)

inversify-basic-example's People

Contributors

bwheel181 avatar dcavanagh avatar dlarner3194 avatar greenkeeper[bot] avatar greenkeeperio-bot avatar johnjacobkenny avatar lholznagel avatar pitamar avatar remojansen avatar

Watchers

 avatar  avatar  avatar

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.