Multi-Provider Strategy for App Configuration in Python

Multi-Provider Strategy for App Configuration in Python
The Problem
When dealing with config values from multiple sources like Azure KeyVault, JSON files, and environment variables, it's important to separate configuration logic from business logic to avoid creating spaghetti code.
The Solution
Using the provider pattern, we create provider classes for each config data source (KeyVault, JSON file, environment variables) that implement a common interface. An InMemoryProvider class is also added to allow setting config values in memory. A Configuration class is then implemented to fetch and set config values.
Implementation
- AbstractConfigurationProvider: Abstract base class for providers.
- KeyVaultProvider: Concrete class for accessing data from KeyVault.
- JsonFileProvider: Concrete class for accessing data from JSON files.
- InMemoryProvider: Concrete class for setting config values in memory.
- EnvProvider: Concrete class for accessing data from environment variables.
- Configuration: Class for fetching and setting config values.
Final Thoughts
This approach allows for easy extensibility and adherence to DRY principles. Adding config data sources like XML files can be done with minimal code modification. The pattern provides a clean way to manage config values in a multi-source scenario.