@brushy/di is a powerful and flexible dependency injection system for JavaScript/TypeScript applications, with special support for React.
On this page
Introduction
@brushy/di is designed to facilitate dependency management in modern applications, enabling:
- Strongly typed dependency injection
- Instance lifecycle management (singleton, transient, scoped, immutable)
- React component support
- Asynchronous dependency resolution
- Monitoring and observability
- Smart promise caching
Installation
Basic Concepts
The DI (Dependency Injection) system is based on some fundamental concepts:- Container: Stores and manages dependencies
- Token: Unique identifier for each dependency
- Provider: Configuration that defines how a dependency should be created
- Lifecycle: Defines the lifecycle of an instance:
- singleton: A single instance is created and reused across the application
- transient: A new instance is created each time the dependency is resolved
- scoped: An instance is created per scope, typically per HTTP request
- immutable: A single instance is created and never invalidated or garbage collected
API Reference
Container
The Container is the central component of the dependency injection system. It is responsible for registering, resolving, and managing the lifecycle of dependencies.React Hooks
React hooks to facilitate dependency injection in functional components, includinguseInject and useInjectLazy.
Component Injection
Specific functionalities for React component injection, enabling the creation of modular and extensible UI systems.Utility Functions
Utility functions to facilitate the use of the dependency injection system in different contexts.Server Utilities
Specific utilities for managing dependency injection on the server side, especially useful in Node.js applications and frameworks like Next.js, Express, NestJS, etc.Advanced Guides
Best Practices
Best practices guide for using@brushy/di efficiently and in an organized way.
Examples
Check out the complete examples in each section of the documentation to see how to use@brushy/di in different scenarios.
Consult the specific guides for more details about each concept.