Skip to main content

python dependency injection library, BETA

Project description

Injector API

The Injector API is a lightweight dependency injection library designed to simplify the management of dependencies in Python projects.

Prerequisites

  • Python 3.7 or higher
  • A proper environment (virtualenv or conda recommended)

Installation

To install the Injector API, simply run the following pip command:


pip install injector-api

Usage

Configuration injectorConfig.json

After installation, you must configure the library to suit your project. Create a injectorConfig.json at the root of your project. This file should define the following variable:


{
    MODULE_APPLICATION = "your_directory_name_here"
}

Replace your_directory_name_here with the name of the directory where your module.py resides.

Defining and Registering Dependencies in module.py

The module.py is a key file in the Injector API library structure. Here, you define and register the dependencies you wish to inject into your application.

Dependency Registration

To register dependencies, you first need to import the container from the library, then use the register method. This method takes the interface you wish to implement and its concrete implementation as arguments.


from injector_api.dependency import container

# absolute routes, DO NOT USE RELATIVE ROUTES
from aplicacion.src.interfaces import IExampleService
from aplicacion.src.interfaces import ExampleServiceImpl1
from aplicacion.src.interfaces import ExampleServiceImpl2
from aplicacion.src.interfaces import InterfaceWithoutOtherService

# Your interfaces and classes here

container.register(IExampleService, ExampleServiceImpl1, override=True)
container.register(IExampleService, ExampleServiceImpl2, override=True)
container.register(InterfaceWithoutOtherService, ImplInterface,override=True)

Injecting into Functions and Methods

Once your dependencies are registered in module.py, you can use the @inject decorator to inject these dependencies into functions or methods.


from injector_api.cargaDinamica import inject

"""
  Using the decorator with () if you use only inject will throw an error, correctly use inject() in case of defining two services for an interface place {interface:0} where 0 corresponds to the first record of the injection
"""

@inject({IExampleService: 0})
def some_function(service: IExampleService):
    return service.do_something()

@inject()
def some_function_two(service: InterfaceWithoutOtherService):
    return service.do_something()

In the above example, the IExampleService dependency will be injected into the some_function, allowing you to use its methods and properties.

Location of module.py

Ensure that the module.py file is located in the directory you've specified in injectorConfig.py. This file will be automatically read by the library upon execution to load and manage dependencies.

Usage Guide for Injector Library in Django

This guide will walk you through setting up the Injector dependency injection library in a Django project.

1. Environment Setup

Ensure you have Django installed and a project initialized.

2. Configuring injectorConfig.json

In the root of your Django project (where manage.py is located), create a file named injectorConfig.json:


{
    "MODULE_APPLICATION": "myapp"
}

Here, myapp is the name of the Django application where the injector should look for the module.py file.

3. Setting up module.py

Inside your myapp application, create a file named module.py. This file will register the dependencies.


from injector_api.dependency import container
# absolute routes, DO NOT USE RELATIVE ROUTES
from myapp.services import MyService

container.register(SomeInterface, MyService)

Make sure SomeInterface and MyService are defined in your project and import absolute routes, DO NOT USE RELATIVE ROUTES

4. Using in a Django View

Now, let's say you have a Django view and you want to use your injected service. You can utilize the @inject decorator to automatically provide the necessary service implementations to your view functions or class-based views.

5. Important Notes:

  • Always ensure that the injectorConfig.json file is placed in a secure location and cannot be accessed by unauthorized users.
  • The module.py file should only contain dependency registrations. Avoid executing any business logic in this file.

Security Recommendations

While the Injector API simplifies dependency management, there are potential security risks when using dynamic loading. We recommend the following precautions:

  • Always validate dynamically loaded information or modules.
  • Avoid loading modules from locations that can be manipulated by untrusted users.
  • Use a secure, isolated environment for your application.
  • Keep the library and all its dependencies up to date.

Contribute

Contributions are welcome! Please submit pull requests or open issues on our GitHub repository.

License

This library is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

injector_api-0.0.21.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

injector_api-0.0.21-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file injector_api-0.0.21.tar.gz.

File metadata

  • Download URL: injector_api-0.0.21.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for injector_api-0.0.21.tar.gz
Algorithm Hash digest
SHA256 49cb2de5087676f0531a8fc6c9c9cdd3c21775303d3a0ec6a5c1a52b699437a4
MD5 a553e897ba917def6d6d23e4f3e2c2d0
BLAKE2b-256 9ea242f5cb0f08fb558e7a623d3140d581a1254bb59810a43fd804096ec2ddb1

See more details on using hashes here.

File details

Details for the file injector_api-0.0.21-py3-none-any.whl.

File metadata

  • Download URL: injector_api-0.0.21-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for injector_api-0.0.21-py3-none-any.whl
Algorithm Hash digest
SHA256 1e72fb4e55a8dc13bb376aad94e118627c71e63732126a606bcf48d728032db3
MD5 3168c34f4c870e085281ee727f105a21
BLAKE2b-256 2f2dcfc02028b3ce9ec123c84d1e1dcef7e918f0c7f958a15c0d83622a6d5fef

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page