Skip to content

A lightweight Dependency Injection (DI) container for Python that allows automatic resolution of dependencies based on constructor type hints.

License

Notifications You must be signed in to change notification settings

oesand/pynjector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pynjector

PyPI

A lightweight Dependency Injection (DI) container for Python that allows automatic resolution of dependencies based on constructor type hints.

🚀 Features

  • Simple and intuitive API
  • Automatic dependency resolution based on type hints
  • Supports binding classes, factory functions, and pre-initialized instances
  • Inspired by C#'s dependency injection approach

📦 Installation

Install via pip:

pip install pynjector

Install via poetry:

poetry add pynjector

🎯 Usage

from src.pynjector import DIContainer


class Database:
    def query(self) -> str:
        return "Data from Database"


class Service:
    def __init__(self, db: Database):
        self.db = db

    def get_data(self) -> str:
        return self.db.query()


# Create the pynjector
container = DIContainer()

# Register dependencies
container.bind(Database)

# Resolve a service instance
service = container.resolve(Service)

print(service.get_data())  # Output: "Data from Database"

About

A lightweight Dependency Injection (DI) container for Python that allows automatic resolution of dependencies based on constructor type hints.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •