|
2 | 2 | Getting Started
|
3 | 3 | ###############
|
4 | 4 |
|
| 5 | +.. note:: |
| 6 | + `py-dependency-injection` has transitioned to beta! Features are now stable and ready for broader testing. Feedback is encouraged to help refine the library for its final release. |
| 7 | + |
| 8 | +############ |
| 9 | +Introduction |
| 10 | +############ |
| 11 | + |
| 12 | +`py-dependency-injection` is a lightweight and flexible dependency injection library for Python. It simplifies managing dependencies in your applications, promoting cleaner and more testable code. |
| 13 | + |
| 14 | +This guide will help you understand the key concepts and how to start using the library. For detailed examples, see the `Examples` section. |
| 15 | + |
| 16 | +############ |
5 | 17 | Installation
|
6 |
| ---------------- |
| 18 | +############ |
7 | 19 |
|
8 |
| -Install using `pip <http://pypi.python.org/pypi/pip/>`_:: |
| 20 | +Install the library using pip: |
| 21 | + |
| 22 | +.. code-block:: bash |
9 | 23 |
|
10 | 24 | $ pip install py-dependency-injection
|
| 25 | +
|
| 26 | +The library supports Python versions 3.7 through 3.12. |
| 27 | + |
| 28 | +########################## |
| 29 | +Core Concepts and Features |
| 30 | +########################## |
| 31 | + |
| 32 | +`py-dependency-injection` offers: |
| 33 | + |
| 34 | +- **Scoped Dependency Management**: Define lifetimes for your dependencies (e.g., transient, scoped, singleton). |
| 35 | +- **Flexible Registrations**: Use constructors, factories, or predefined instances for dependency registration. |
| 36 | +- **Tag-Based Organization**: Categorize and resolve dependencies using tags. |
| 37 | +- **Multiple Containers**: Isolate dependencies for different parts of your application. |
| 38 | + |
| 39 | +Refer to the `Examples` section for detailed usage scenarios. |
| 40 | + |
| 41 | +#################### |
| 42 | +Quick Start Overview |
| 43 | +#################### |
| 44 | + |
| 45 | +1. **Create a Dependency Container**: |
| 46 | + - The `DependencyContainer` is the core object for managing dependencies. |
| 47 | + |
| 48 | +2. **Register Dependencies**: |
| 49 | + - Dependencies can be registered with different lifetimes: transient, scoped, or singleton. |
| 50 | + |
| 51 | +3. **Resolve Dependencies**: |
| 52 | + - Use the container to resolve dependencies where needed. |
| 53 | + |
| 54 | +Basic workflow: |
| 55 | + |
| 56 | +.. code-block:: python |
| 57 | +
|
| 58 | + from dependency_injection.container import DependencyContainer |
| 59 | +
|
| 60 | + class Connection: |
| 61 | + pass |
| 62 | +
|
| 63 | + class PostgresConnection(Connection): |
| 64 | + pass |
| 65 | +
|
| 66 | + # Create a container |
| 67 | + container = DependencyContainer.get_instance() |
| 68 | +
|
| 69 | + # Register and resolve dependencies |
| 70 | + container.register_singleton(Connection, PostgresConnection) |
| 71 | + connection = container.resolve(Connection) |
| 72 | + print(type(connection).__name__) # Output: PostgresConnection |
| 73 | +
|
| 74 | +############## |
| 75 | +Best Practices |
| 76 | +############## |
| 77 | + |
| 78 | +- **Use Constructor Injection**: Preferred for most cases as it promotes clear and testable designs. |
| 79 | +- **Leverage Tags for Organization**: Group dependencies logically using tags. |
| 80 | +- **Choose the Right Scope**: Use scoped or singleton lifetimes to optimize performance and resource usage. |
| 81 | +- **Keep Dependencies Decoupled**: Avoid tightly coupling your components to the container. |
| 82 | +- **Isolate Contexts with Containers**: Use multiple containers to manage dependencies for separate modules or contexts. |
| 83 | + |
| 84 | +################# |
| 85 | +Where to Go Next? |
| 86 | +################# |
| 87 | + |
| 88 | +- **Examples**: |
| 89 | + Explore detailed examples of how to register, resolve, and manage dependencies effectively in the `Examples` section. |
| 90 | + |
| 91 | +- **Community and Support**: |
| 92 | + Join our community on GitHub to ask questions, report issues, or contribute to the project. |
0 commit comments