Why Dependency Injection?
Dependency injection helps you write more maintainable, testable code by:
- Decoupling components - Services don't need to know how their dependencies are created
- Improving testability - Easy to swap dependencies with mocks during testing
- Managing complexity - Centralized configuration of how objects are wired together
- Lazy loading - Services are only created when needed
- Singleton by default - Same instance returned on subsequent calls
- Dependency management - Services can depend on other services
Quick Start
Get up and running with Jimple in minutes:
Installation
npm install jimple@2.0.0-beta.5
pnpm add jimple@2.0.0-beta.5
yarn add jimple@2.0.0-beta.5
bun add jimple@2.0.0-beta.5
CDN (Browser)
<script src="https://cdn.jsdelivr.net/npm/jimple@2.0.0-beta.5/dist/Jimple.umd.js"></script>
Import Methods
// ES6 Modules
import Jimple from "jimple";
// CommonJS
const Jimple = require("jimple");
// AMD
define(["jimple"], function(Jimple) {
// Your code here
});
Core Concepts
Services
Services are objects that perform tasks in your application. They're defined as functions that return the service instance:
Parameters
Parameters store configuration values, strings, numbers, or any non-function data:
Factory Services
When you need a new instance every time instead of a singleton:
Advanced Features
Protecting Functions
To store an actual function (not a service factory) as a parameter:
Extending Services
Add behavior to existing services:
Removing Services or Parameters
Remove services or parameters from the container with unset()
:
Important Notes:
- Removes the service/parameter completely from the container
- Clears any cached instances and metadata for services
- Cannot be undone - you'll need to re-register the service
- Safe to call on non-existent services (no error thrown)
Optional Dependencies & Defaults
Handle optional services with fallbacks:
Raw Service Access
Get the service definition function instead of the service itself:
ES6 Proxy Mode
Use modern JavaScript syntax for a more natural API:
Limitations:
- Can't overwrite built-in methods (
set
,get
, etc.) - Accessing non-existent properties throws an error
- TypeScript requires special handling (see below)
TypeScript Support
Jimple provides full TypeScript support with interface definitions:
Modular Configuration with Providers
Organize your container configuration into reusable modules. Providers help you structure your application by grouping related services and configuration into logical units.
Basic Provider
A provider is an object with a register
method that defines services and parameters:
Multiple Providers
You can register multiple providers to organize different aspects of your application:
Provider Dependencies
Providers can depend on services defined by other providers, allowing for modular composition:
Benefits of Using Providers:
- Modularity - Group related services together
- Reusability - Share providers across different applications
- Organization - Separate concerns into logical modules
- Testing - Easier to mock entire provider modules
API Reference
Container Methods
Method | Description | Returns |
---|---|---|
set(id, value) | Define a service or parameter | void |
unset(id, value) | Remove a service or parameter | void |
get(id) | Retrieve a service or parameter | any |
has(id) | Check if service/parameter exists | boolean |
factory(fn) | Create a factory service | Function |
protect(fn) | Protect a function from being treated as service | Function |
extend(id, fn) | Extend an existing service | void |
raw(id) | Get the raw service definition | Function |
register(provider) | Register a service provider | void |
Need More Details?
For complete API documentation with detailed examples and type definitions:
View Full API Documentation โMore Examples
Express.js Web Server
Testing with Mocks
Documentation
Interactive Guide
This page! Learn Jimple with live examples, tutorials, and hands-on code playgrounds.
Start LearningComplete API Reference
Detailed JSDoc documentation with full method signatures, parameters, and comprehensive examples.
View API Docs