|
| 1 | +# Python Simple IoC - Dependency Injection Container |
| 2 | + |
| 3 | +[](https://github.com/rande/python-simple-ioc/actions) |
| 4 | +[](https://www.python.org/downloads/) |
| 5 | +[](LICENSE) |
| 6 | + |
| 7 | +A lightweight, **Pythonic** dependency injection container inspired by Symfony's DependencyInjection component. This library embraces Python's philosophy of simplicity while providing powerful tools for organizing complex applications. |
| 8 | + |
| 9 | +## 🎯 Why Use Dependency Injection? |
| 10 | + |
| 11 | +Dependency injection isn't just enterprise complexity - it's a **Pythonic pattern** that promotes: |
| 12 | + |
| 13 | +- **Clear separation of concerns** - Each class has a single responsibility |
| 14 | +- **Testability** - Easy to mock dependencies for unit tests |
| 15 | +- **Flexibility** - Change implementations without touching existing code |
| 16 | +- **Maintainability** - Explicit dependencies make code easier to understand |
| 17 | + |
| 18 | +This approach aligns perfectly with Python's zen: *"Explicit is better than implicit"* and *"Readability counts"*. |
| 19 | + |
| 20 | +## 🚀 Quick Start |
| 21 | + |
| 22 | +### Installation |
| 23 | + |
| 24 | +```bash |
| 25 | +pip install ioc |
| 26 | +``` |
| 27 | + |
| 28 | +### Basic Usage |
| 29 | + |
| 30 | +1. **Define your services** in a `services.yml` file: |
| 31 | + |
| 32 | +```yaml |
| 33 | +parameters: |
| 34 | + database_url: "sqlite:///app.db" |
| 35 | + debug_mode: true |
| 36 | + |
| 37 | +services: |
| 38 | + # Database connection |
| 39 | + database: |
| 40 | + class: myapp.database.Database |
| 41 | + arguments: ["%database_url%"] |
| 42 | + |
| 43 | + # User repository with injected database |
| 44 | + user_repository: |
| 45 | + class: myapp.repositories.UserRepository |
| 46 | + arguments: ["@database"] |
| 47 | + |
| 48 | + # User service with injected repository |
| 49 | + user_service: |
| 50 | + class: myapp.services.UserService |
| 51 | + arguments: ["@user_repository"] |
| 52 | + calls: |
| 53 | + - [set_debug, ["%debug_mode%"]] |
| 54 | +``` |
| 55 | +
|
| 56 | +2. **Use the container** in your application: |
| 57 | +
|
| 58 | +```python |
| 59 | +import ioc |
| 60 | + |
| 61 | +# Build container from configuration |
| 62 | +container = ioc.build(['services.yml']) |
| 63 | + |
| 64 | +# Get your services - dependencies are automatically resolved! |
| 65 | +user_service = container.get('user_service') |
| 66 | + |
| 67 | +# Your service is ready to use with all dependencies injected |
| 68 | +users = user_service.get_all_users() |
| 69 | +``` |
| 70 | + |
| 71 | +## 🏗️ Perfect for Python Projects |
| 72 | + |
| 73 | +This library follows Python best practices: |
| 74 | + |
| 75 | +- **Configuration over code** - Define dependencies in YAML, not scattered across your codebase |
| 76 | +- **Explicit dependencies** - See exactly what each service needs at a glance |
| 77 | +- **No magic** - Simple, predictable behavior that follows Python conventions |
| 78 | +- **Framework agnostic** - Works with Flask, Django, FastAPI, or pure Python |
| 79 | + |
| 80 | +## 📚 Advanced Features |
| 81 | + |
| 82 | +### Service Definitions |
| 83 | + |
| 84 | +```yaml |
| 85 | +services: |
| 86 | + # Constructor injection |
| 87 | + email_service: |
| 88 | + class: myapp.EmailService |
| 89 | + arguments: ["@mailer", "%sender_email%"] |
| 90 | + |
| 91 | + # Method calls after construction |
| 92 | + logger: |
| 93 | + class: logging.Logger |
| 94 | + arguments: ["myapp"] |
| 95 | + calls: |
| 96 | + - [setLevel, ["INFO"]] |
| 97 | + - [addHandler, ["@file_handler"]] |
| 98 | + |
| 99 | + # Weak references (lazy loading) |
| 100 | + cache_service: |
| 101 | + class: myapp.CacheService |
| 102 | + arguments: ["#@redis_client"] # Only loaded when needed |
| 103 | +``` |
| 104 | +
|
| 105 | +### Parameters and Environment |
| 106 | +
|
| 107 | +```yaml |
| 108 | +parameters: |
| 109 | + # String interpolation |
| 110 | + log_file: "/var/log/%app_name%.log" |
| 111 | + |
| 112 | + # Environment variables |
| 113 | + secret_key: "%env(SECRET_KEY)%" |
| 114 | + |
| 115 | + # Default values |
| 116 | + redis_url: "%env(REDIS_URL):redis://localhost:6379%" |
| 117 | +``` |
| 118 | +
|
| 119 | +## 🧪 Testing Made Easy |
| 120 | +
|
| 121 | +With dependency injection, testing becomes straightforward: |
| 122 | +
|
| 123 | +```python |
| 124 | +import unittest |
| 125 | +from unittest.mock import Mock |
| 126 | + |
| 127 | +class TestUserService(unittest.TestCase): |
| 128 | + def test_create_user(self): |
| 129 | + # Mock the repository |
| 130 | + mock_repo = Mock() |
| 131 | + mock_repo.save.return_value = True |
| 132 | + |
| 133 | + # Inject the mock |
| 134 | + user_service = UserService(mock_repo) |
| 135 | + |
| 136 | + # Test with confidence |
| 137 | + result = user_service.create_user("[email protected]") |
| 138 | + self.assertTrue(result) |
| 139 | + mock_repo.save.assert_called_once() |
| 140 | +``` |
| 141 | + |
| 142 | +## 📖 Learn More |
| 143 | + |
| 144 | +- [Documentation](https://github.com/rande/python-simple-ioc/wiki) |
| 145 | +- [Examples](examples/) |
| 146 | +- [API Reference](docs/) |
| 147 | + |
| 148 | +## 🤝 Contributing |
| 149 | + |
| 150 | +Contributions are welcome! This project follows Python community standards: |
| 151 | + |
| 152 | +- PEP 8 code style |
| 153 | +- Type hints for better IDE support |
| 154 | +- Comprehensive tests |
| 155 | +- Clear documentation |
| 156 | + |
| 157 | +## 📄 License |
| 158 | + |
| 159 | +Licensed under the Apache License 2.0. See [LICENSE](LICENSE) for details. |
| 160 | + |
| 161 | +--- |
| 162 | + |
| 163 | +*"Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex."* - The Zen of Python |
| 164 | + |
| 165 | +This library embodies these principles while providing the power and flexibility needed for serious Python applications. |
0 commit comments