npm

Need private packages and team management tools?Check out npm Orgs. »

dependencyjs

2.0.6 • Public • Published

dependencyjs

DependencyJS

Dependency injection framework for Typescript and Javascript

What is dependencyjs?

Dependencyjs is an dependency framework for javascript and typescript applications. This framework will help building independent components in application. So it is going to be useful if you want to achieve a well maintainable and testable application

Installation

Node.js

dependencyjs is available on npm. To install it, type:

$ npm install dependencyjs

Browsers

You can also use it within the browser; install via npm and use the ComponentRegistry.js file found within the download. For example:

<script src="./node_modules/dependencyjs/src/ComponentRegistry.js"></script>

For JavaScript;

DIC.registry.register(BaseSample, sample);
 
DIC.registry.resolve(BaseSample);

Limitations: loading configurations dynamically from other files is not suppoerted on the browser. It must be bundled together (using browserify or webpack).

Usage

You can register base classes with their implementation. Lets take an example of nodejs application

below is the base class

export abstract class BaseSample{
    public abstract ShowSample(name: string): string;
}

now create a concrete implementation of BaseSample class

export class Sample extends BaseSample{
    ShowSample(name: string): string{
        return "Test Name is :"+ name;
    }
}

Now register class with its concrete implementation

import {ComponentRegistry, registry} from "dependencyjs";
import {BaseSample} from "./BaseSample";
import {Sample} from "./Sample";
 
//register class object
let sample: BaseSample = new Sample();
registry.register<BaseSample>(BaseSample, sample);

Resolve class object to use it anywhere in code

let sample: BaseSample= registry.resolve<BaseSample>(BaseSample);

Using resolver

Dependencies can be registered with extra parameter so that we can have multiple implementation of base class registered in registry.

lets extend same Base sample in another class and register both of them in registry

export class SampleTypeSecond extends BaseSample{
    ShowSample(name: string): string{
        return "Test Name is :"+ name;
    }
}

Now register both of them in registry

import {ComponentRegistry, registry} from "dependencyjs";
import {BaseSample} from "./BaseSample";
import {Sample} from "./Sample";
import {Sample} from "./SampleTypeSecond";
 
//register class object
let sample: BaseSample = new Sample();
let secondSample: BaseSample = new SampleTypeSecond();
 
registry.register<BaseSample>(BaseSample, sample, "sample");
registry.register<BaseSample>(BaseSample, secondSample, "secondSample");

Resolve class object with resolver in your code

let sample: BaseSample= registry.resolve<BaseSample>(BaseSample, "sample");
let sampleSecond: BaseSample= registry.resolve<BaseSample>(BaseSample, "secondSample");

Loading dependencies from configuration

dependencyjs provide a functionality where you can load classes dynamically from json configurations.

How to configure json

 
{
  "container": {
    "register": [
      {
        "base": {
          "typeName": "<BaseClassType>",
          "sourceType": "<fs or package>",
          "sourceInfo": "<location of file or package name>"
        },
        "resolver": "<any string to identify class>",
        "map": {
          "typeName": "<Concrete implementation class name>",
          "sourceType": "<fs or package>",
          "sourceInfo": "<location of file or package name>"
        }
      },
      {
        "base": {
          "typeName": "<BaseClassType>",
          "sourceType": "<fs or package>",
          "sourceInfo": "<location of file or package name>"
        },
        "resolver": "<any string to identify class>",
        "map": {
          "typeName": "<Concrete implementation class name>",
          "sourceType": "<fs or package>",
          "sourceInfo": "<location of file or package name>"
        }
      }
    ]
  }
}

Details of each field

1. Container: Container can have multiple class registration.
2. Register: In register, you can mention detail about base class and child class to register the dependency.
  a. base:
      typeName  : This will contain name of the exported base class. In our example about, it could be "BaseSample"
      sourceType: You can use a base class from a node package or it could exist in your local system. So there are two possible
                  values -> "fs" or "package"
      sourceInfo: If sourceType is package then this field will contain name of the package. If source type is "fs" then
                  this field will contain localtion of your file which contains base class. For example, location of Sample
                  class is "/BaseSample".
  b. map
      typeName  : This will contain name of the exported concrete implementation class. In our example, it could be "Sample".
      sourceType: You can use a concrete class from a node package or it could exist in your local system. So there are two possible
                  values -> "fs" or "package"
      sourceInfo: If sourceType is package then this field will contain name of the package. If source type is "fs" then
                  this field will contain localtion of your file which contains concrete class. For example, location of Sample
                  class is "/Sample".
  c. resolver   : If a base class have multiple implementation and you want to get any kind of implementation at run time, you can use
                  resolver. This will help you to identify classes uniquely.

Here is example of one json configuration

{
  "container": {
    "register": [
      {
        "base": {
          "typeName": "BaseSample",
          "sourceType": "fs",
          "sourceInfo": "/BaseSample"
        },
        "resolver": "Sample1",
        "map": {
          "typeName": "Sample",
          "sourceType": "fs",
          "sourceInfo": "/Sample"
        }
      },
      {
        "base": {
          "typeName": "BaseSample",
          "sourceType": "fs",
          "sourceInfo": "/BaseSample"
         },
         "resolver": "Sample2",
         "map": {
           "typeName": "SampleTypeSecond",
           "sourceType": "fs",
           "sourceInfo": "/SampleTypeSecond"
         }
       }
    ]
  }
}

How to load json configuration

In registry, a method has been introduced -

registry.loadConfiguration(config).

You can use above method to load json configurations. This method needs Config object as parameter. You can find it from dependencyjs. Below are the detail of Config object

- Config: This class basically contains two properties.
    - appDirectory: You need to initialize this property with main execution directory.
    - configFolder: This is the folder path where you keep all json configurations to register them in registry.

Below is the example of reading json configuration.

import {registry, Config} from "dependencyjs";
 
let config: Config = new Config();
config.appDirectory = "Main application directory"
config.configFolder = "where you contains all json"
 
registry.loadConfiguration(config);

All your classes will be register which has been mentioned in json files. If not, you will get appropriate error message.

Now you can use registry as the way explained in usage section

Core Contributors

Feel free to reach out to any of the core contributors with your questions or concerns. We will do our best to respond in a timely manner.

IntimeTec Manish Kumawat Akhil S

install

npm i dependencyjs

Downloadsweekly downloads

62

version

2.0.6

license

ISC

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
  • avatar
Report a vulnerability