dizy
v0.5.0
Published
A lightweight Vanilla JavaScript Dependency Injection implementation with lifecycles.
Downloads
20
Maintainers
Readme
Getting Started
Installation
npm install dizy
Simple usage
import { ContextContainerFactory, SingletoneConfig } from "dizy";
// Create array of di object configurations
const diObjectsConfigs = [
// Singletone object config
new SingletoneConfig(
'carService', // name of DI Object
CarService // DI Object class or function
),
new SingletoneConfig('engineService', EngineService),
];
// Class of your app where you want to use DI
// It's just an example, you can use it anywhere
class App {
constructor() {
// Next to lines of code is needed for configuring your DI Context
this.context = ContextContainerFactory.createContainer(diObjectsConfigs, 'appContext');
this.context.init(); // Runs the context
}
start() {
const carService = this.context.getInstance('carService'); // Getting instance of your DI Object
carService.check();
}
}
class CarService {
constructor(engineService) { // name of constructor parameter must be equal to name of object in DI Context
this.engineService = engineService;
}
check() {
this.engineService.pressure();
}
}
class EngineService {
constructor() { }
pressure() {
console.log('Pressure is high!');
}
}
Introduction
Welcome to the documentation of Dizy! This library provides a powerful and flexible solution for managing dependencies in your applications, making it easier to write maintainable and testable code.
Dependency injection is a design pattern that allows you to decouple components from their dependencies, making them more reusable and easier to test. With our DI library, you can easily inject dependencies into your components, allowing them to focus on their core functionality.
The library offers a simple and intuitive API, making it easy to get started with dependency injection.
In this documentation, we'll cover everything you need to know about using Dizy. We'll start with an overview of the library's features and how they can benefit your projects. Then, we'll dive into specific examples of how to use the library in different scenarios. Finally, we'll provide some tips and best practices for working with the library effectively.
Let's get started and explore the power of dependency injection with Dizy!
Goals
- Help the developer write more reliable and easy to read code.
- Simplify and speed up the development process.
- Allow you to commit the results of architectural analysis in the software objects form.
- Provide the ability to quickly replace or disable software objects.
- Ensure compliance with the lifecycle of objects and provide an opportunity to create special lifecycles.
- Provide tools for configuring objects
Review of approaches
Service Locator
Dependency Injection
Dizy multilifecycle
Other implementations and performance overview
Authors
Supporters
Become a sponsor on opencollective to get your company in front of future thousands of engaged developers and help us out!
Love Dizy? Give our repo a star :star: :arrow_up:.