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
pnpm add jimple
yarn add jimple
bun add jimple
CDN (Browser)
<script src="https://cdn.jsdelivr.net/npm/jimple@latest/src/Jimple.js"></script>
โ ๏ธ Production Warning: Replace latest
with a specific version for production use.
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:
container.set('utility', container.protect(() => {
return Math.random() * 100;
}));
const utilityFn = container.get('utility'); // Returns the function itself
const result = utilityFn(); // Call the function
Extending Services
Add behavior to existing services:
container.set('logger', (c) => new Logger());
// Extend the logger to add file output
container.extend('logger', (logger, c) => {
logger.addFileHandler('/var/log/app.log');
return logger;
});
Optional Dependencies & Defaults
Handle optional services with fallbacks:
container.set('cache', (c) => {
if (c.has('redisConfig')) {
return new RedisCache(c.get('redisConfig'));
}
return new MemoryCache(); // Fallback
});
Raw Service Access
Get the service definition function instead of the service itself:
container.set('database', (c) => new Database());
const dbFactory = container.raw('database');
const db1 = dbFactory(container);
const db2 = dbFactory(container); // Create another instance manually
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:
API Reference
Container Methods
Method | Description | Returns |
---|---|---|
set(id, value) | Define 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