Make sure to review the roadmap and current limitations before using.
Table of Contents
This project started as a technical assignment for a job application, but, in building out the library, I thought it would be a good opportunity to get into the weeds a bit on what makes a good library. This will be an ever evolving project taking into account best practices as I come across them. As a bonus, this provides a minimal implenetation for a functioning dependency injection framework.
To get a local copy up and running follow these simple example steps.
Make sure you have node and npm installed.
To check if you have Node.js installed, run this in your terminal:
node -vIf your terminal returns a version number, you're good to go. npm is distributed with Node.js, so if you have node, you should have npm, but to confirm, run this in your terminal:
npm -vGo here if you do not have node installed. If you want to update npm, use the following command in terminal:
npm install npm@latest -gMy go to package manager is yarn. You can get yarn via the following command (this is optional):
npm install --global yarn
-
Clone the repo into your local directory
git clone https://github.com/miguelriverarios/MRR-Simple-Inject-Library.git
-
Install NPM packages
npm install
or with yarn
yarn
Tests are performed using the jasmine-karma framework. To run the test methods, use the following command:
npm test
Or with yarn:
yarn test
If successful, a browser window will open showing test results.
This library provides a simple framework for implementing dependency injectio via an IoC container. Let's say you have a class representing a Superhero like so:
class Superhero {
constructor(superpower) {
this.superpower = superower;
}
}
A Superhero isn't a superhero without a Superpower, so let's pretend Superpower is defined as such:
class Superpower {
constructor() {
this.ability = "heat vision";
}
}
Normally you'd have to define these sequentially before instantiating a Superhero, but with this library you can do it like so:
require("/src/injection.js");
const inject = new SimpleInject();
class Superhero {
constructor(superpower) {
this.superpower = superpower;
}
}
inject.register("superhero", ["superpower", Superhero]);
class Superpower {
constructor() {
this.ability = "heat vision";
}
}
inject.register("superpower", [Superpower]);
const superhero = inject.get("superhero");
console.log(superhero.superpower.ability); // heat vision
See the open issues for a list of proposed features (and known issues).
- No support for checking or resolving circular dependencies
- No check for dependency depth within stack (could result in long runtimes if dependencies are considerably layered)
- Services must be defined as classes (ie no functions, etc)
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
All additional features should include corresponding test methods.
Distributed under the MIT License. See LICENSE for more information.
Miguel Rivera Rios - miguel.e.rivera.rios@gmail.com
Project Link: https://github.com/miguelriverarios/MRR-Simple-Inject-Library