Dependency Injection
Overview
Dependency Injection (DI) is a design pattern used in Equipment to manage object creation and lifecycle, promoting loose coupling and improved modularity.
Implementation Details
Equipment uses a library to implement dependency injection, specifically utilizing ThreadSafeSingleton for creating thread-safe, single-instance objects.
Key Components
Singleton Providers
Equipment defines singleton providers for key components:
-
Inspiring Singleton
- Accessor:
app.inspiring() - Creates a single instance of the
Inspireclass - Configured with quotes from the equipment configuration
- Used as an example on how to implement application services
- Accessor:
-
Scheduler Singleton
- Accessor:
app.scheduler() - Creates a single instance of the
Schedulerclass
- Accessor:
Other components are also defined as singletons at the equipment library level:
-
Log Singleton
- Accessor:
equipment.log() - Creates a single instance of the
Logclass - Configured with log settings from the equipment configuration
- Accessor:
-
Queue Singleton
- Accessor:
equipment.queue() - Creates a single instance of the
Queueclass - Configured with queue settings from the equipment configuration
- Accessor:
-
Database Singleton
- Accessor:
equipment.database() - Creates a single instance of the
Databaseclass - Configured with database settings from the equipment configuration
- Accessor:
-
Storage Singleton
- Accessor:
equipment.storage() - Creates a single instance of the
Storageclass - Configured with storage settings from the equipment configuration
- Accessor:
App Factory Method
The app() function serves as a factory method to create and configure the main App instance:
#!/usr/bin/env python
from app import app
app = app()
# Accessing singleton providers
log = app.log()
queue = app.queue()
database = app.database()
storage = app.storage()
# etc...
Benefits
- Modularity: Easy to manage and swap dependencies
- Thread Safety: Uses thread-safe singleton providers
- Configuration Flexibility: Dependencies can be easily configured
- Reduced Coupling: Components are loosely connected
Best Practices
- Use singleton providers for shared, stateful components
- Inject dependencies through constructors
- Keep dependencies minimal and focused