### Install with Pip Source: https://github.com/kodemore/kink/blob/master/README.md Installation instructions using pip. ```shell pip install kink ``` -------------------------------- ### Install with Poetry Source: https://github.com/kodemore/kink/blob/master/README.md Installation instructions using Poetry. ```shell poetry add kink ``` -------------------------------- ### Integration with FastAPI Source: https://github.com/kodemore/kink/blob/master/README.md This example demonstrates how to integrate `kink` with FastAPI for dependency injection. ```python from fastapi import APIRouter, Depends, status from fastapi.responses import JSONResponse, Response from kink import di router = APIRouter() # register service in the DI container di[ClientService] = ClientService() @router.post( "/clients", response_model=ClientDTO, responses={400: {"model": APIErrorMessage}, 500: {"model": APIErrorMessage}}, tags=["clients"], ) async def create_client( request: CreateClientDTO, service: ClientService = Depends(lambda: di[ClientService]) ) -> JSONResponse: result = service.create(request) return JSONResponse(content=result.dict(), status_code=status.HTTP_201_CREATED) ``` -------------------------------- ### Adding service to di container Source: https://github.com/kodemore/kink/blob/master/README.md Example of adding a service to the dependency injection container. ```python from kink import di from os import getenv di["db_name"] = getenv("DB_NAME") di["db_password"] = getenv("DB_PASSWORD") ``` -------------------------------- ### Retrieving all instances with the same alias Source: https://github.com/kodemore/kink/blob/master/README.md This example demonstrates how to retrieve all services that share the same alias, which is useful for implementing patterns like the strategy pattern. ```python from kink import inject from typing import Protocol, List class IUserRepository(Protocol): ... @inject(alias=IUserRepository) class MongoUserRepository: ... @inject(alias=IUserRepository) class MySQLUserRepository: ... @inject() class UserRepository: def __init__(self, repos: List[IUserRepository]) -> None: # all services that alias to IUserRepository will be passed here self._repos = repos def store_to_mysql(self, user: ...): self._repos[1].store(user) def store_to_mongo(self, user: ...): self._repos[0].store(user) ``` -------------------------------- ### Adding factorised services to dependency injection Source: https://github.com/kodemore/kink/blob/master/README.md Example of defining and using factorised services that are instantiated every time they are requested. ```python from kink import di from sqlite3 import connect di.factories["db_connection"] = lambda di: connect(di["db_name"]) connection_1 = di["db_connection"] connection_2 = di["db_connection"] connection_1 != connection_2 ``` -------------------------------- ### Adding on-demand service to the dependency injection container Source: https://github.com/kodemore/kink/blob/master/README.md Example of defining an on-demand service using a lambda function. ```python from kink import di from sqlite3 import connect di["db_connection"] = lambda di: connect(di["db_name"]) ``` -------------------------------- ### Clearing DI cache Source: https://github.com/kodemore/kink/blob/master/README.md This example shows how to clear the cached services in the DI container by calling the `di.clear_cache()` method. ```python from kink import inject, di ... # set and accesss your services di.clear_cache() # this will clear cache of all services inside di container that are not factorised services ``` -------------------------------- ### Constructor injection Source: https://github.com/kodemore/kink/blob/master/README.md Performing constructor injection with the kink DI container. ```python from kink import inject, di import MySQLdb # Set dependencies di["db_host"] = "localhost" di["db_name"] = "test" di["db_user"] = "user" di["db_password"] = "password" di["db_connection"] = lambda di: MySQLdb.connect(host=di["db_host"], user=di["db_user"], passwd=di["db_password"], db=di["db_name"]) @inject class AbstractRepository: def __init__(self, db_connection): self.connection = db_connection class UserRepository(AbstractRepository): ... repository = di[UserRepository] # will retrieve instance of UserRepository from di container repository.connection # mysql db connection is resolved and available to use. ``` -------------------------------- ### Services aliasing Source: https://github.com/kodemore/kink/blob/master/README.md Registering a service with an alias name using the `@inject` decorator. ```python from kink import inject from typing import Protocol class IUserRepository(Protocol): ... @inject(alias=IUserRepository) class UserRepository: ... assert di[IUserRepository] == di[UserRepository] # returns true ``` -------------------------------- ### Requesting services from dependency injection container Source: https://github.com/kodemore/kink/blob/master/README.md Accessing services from the kink DI container by referencing them like a dictionary. ```python from kink import di from sqlite3 import connect # Bootstrapping di["db_name"] = "test_db.db" di["db_connection"] = lambda di: connect(di["db_name"]) # Getting a service connection = di["db_connection"] # will return instance of sqlite3.Connection assert connection == di["db_connection"] # True ``` -------------------------------- ### Matching argument's type annotations Source: https://github.com/kodemore/kink/blob/master/README.md Using type annotations for autowiring dependencies in the kink DI container. ```python from kink import di, inject from sqlite3 import connect, Connection di["db_name"] = "test_db.db" di[Connection] = lambda di: connect(di["db_name"]) @inject class UserRepository: def __init__(self, db: Connection): self.db = db repo = di[UserRepository] assert repo.db == di[Connection] # True ``` -------------------------------- ### Removing services from dependency injection container Source: https://github.com/kodemore/kink/blob/master/README.md Removing services from the kink DI container using the `del` statement. ```python from kink import di # Add a service di["temp_service"] = "temporary_value" assert "temp_service" in di # Remove the service del di["temp_service"] assert "temp_service" not in di ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.