Skip to main content

Python Dependency Injection Library

Project description

Wireup

Dependency Injection Container with a focus on developer experience, type safety and ease of use.

GitHub GitHub Workflow Status (with event) Code Climate maintainability Coverage PyPI - Python Version PyPI - Version

[!TIP] Simplify Dependency injection for Flask using the new Flask integration!

  • Automatically inject dependencies without having to manually call autowire.
  • Expose flask application configuration in the container.

⚡ Key Features

  • Inject Services and Configuration
  • Interfaces / Abstract classes
  • Multiple Containers
  • Static factories
  • Singleton/Transient dependencies
  • Framework Agnostic
  • Simplified usage in Flask and FastApi using the first-party integrations.

📋 Quickstart

Example showing a Database service, a repository and a web view which uses the repository to fetch all posts from a fictional blog db.

1. Register dependencies

from wireup import container

# Optionally wire parameters, they serve as configuration for services. 
# Think of a database url or environment name.
container.params.update(app.config.items())


# Register a class as a service in the container.
@container.register 
class DatabaseService:
    # connection_url will contain the value of the parameter 
    # with the given name in the annotation.
    def __init__(self, connection_url: Annotated[str, Wire(param="db_connection_url")]):
        self.engine = create_engine(connection_url)

        
# Initializer injection is supported for regular classes as well as dataclasses.
@container.register
@dataclass
class PostRepository:
    db: DatabaseService 

    def find_all(self) -> list[Post]:
        return self.db.query...

2. Inject

@app.get("/posts")
@container.autowire 
# Decorate all targets where the library must perform injection, such as views in an Api.
# Services are automatically injected based on annotated type.
# Optional for views when using flask or fastapi integration.
def get_posts(post_repository: PostRepository):
    return post_repository.find_all()

Installation

# Install using poetry:
poetry add wireup

# Install using pip:
pip install wireup

📑 Documentation

For more information check out the documentation

🎮 Demo application

A demo flask application is available at maldoinc/wireup-demo

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

wireup-0.7.2.tar.gz (18.6 kB view hashes)

Uploaded Source

Built Distribution

wireup-0.7.2-py3-none-any.whl (22.7 kB view hashes)

Uploaded Python 3

Supported by

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