PyDInjector


NamePyDInjector JSON
Version 1.0.2 PyPI version JSON
download
home_pagehttps://github.com/ivanmrosa/PYDInjector
SummarySimple dependency injection container for python
upload_time2023-02-01 00:39:46
maintainer
docs_urlNone
authorIvan Muniz Rosa
requires_python
licenseMIT License
keywords dependency injection di inversion of control ioc container dependency injection inversion of control python
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # PYDInjector
**Simple Dependency Injection Container for python**

## Description

PyDInjector is dependency injector container that facilitates to follow the *Inversion of Control* principle. PyDInjector uses the python type hint to
resolve the dependencies and to inject them into the objects. To do that we need to configured the relation of the implemented objects with their types
(Interfaces/ABC). After configured is possible to use the *@inject* decorator to indicates whether the container should inject the dependency.

There are to types of injections: the scoped injection and the singleton injection.

### Scoped injection

Escoped injection will provide a new instance of the object always that the scope where the @inject decorator was used is call. For example, if we decorate
the constructor *def __ini__(OurService : IOurService):*, the container will provide a new instance of *OurService* always whe instantiate this object. But 
if we decorate other method in this class like *def doSomenthing(OurService : IOurService):*, the container will provide a new instance of *OurService* 
always that we execute the method *doSomenthing*.

### Singleton injection

Singleton injection will instantiate the object once and then provide the same instance every time it been requested.

## Configuring the container injector

The configuration of the relationship between the types and the objects must be inserted at the beginning of the code execution. In order to be able to 
do that is necessary to import the  *DIContainer* from *PyDInjector*. Then is possible to use the methods 
*DIContainer.AddScoped* and *DIContainer.AddSingleton*.


    from PyDInjector import DIContainer
    DIContainer.AddScoped(IBody, HumanBody)
    DIContainer.AddScoped(IBodyAction, HumanBodyAction)
    DIContainer.AddScoped(IFoodProvider, FoodProvider)
    DIContainer.AddSingleton(ICooker, Cooker)

The first parameter is the Interface and the second one is the class implementation.

See the utilization below:


    
    class HumanBody(IBody):    
        @inject #The container will inject a instance of BodyAction to this constructor
        def __init__(self, actions: IBodyAction) -> None:        
            super().__init__()
            self.__actions : IBodyAction = actions
            self.__identification : str = str(uuid.uuid1())    
        def move(self) -> str:
            return self.__actions.move()
        def jump(self) -> str:
            return self.__actions.jump()
        def getLegsQuantity(self) -> int:
            return 2
        def getHandsQuantity(self) -> int:
            return 2
        def getInstanceIdentification(self) -> str:
            return self.__identification
        def getActionIdentification(self) -> str:
            return self.__actions.getInstanceIdentification()
        @inject #The container will inject a instance of FoodProvider tho this method. If the configuration was singleton, then will be always the same instance. Otherwise a new instance will be provided at each execution 
        def eat(self, foodProvider: IFoodProvider) -> str:
            return self.__actions.eat(food=foodProvider.getFood())
        
    humanBody : IBody = HumanBody() #no need to pass the parameter here, because the container will provide the dependency       
    humanBody.getHandsQuantity()
    humanBody.eat() #no need to pass the parameter here, because the container will provide the dependency       


If you want to get an instance of an object manually, is possible to use the method *DIContainer.GetObject(IType)*.
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/ivanmrosa/PYDInjector",
    "name": "PyDInjector",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "dependency injection,DI,inversion of control,IOC,container,Dependency Injection,Inversion of Control,Python",
    "author": "Ivan Muniz Rosa",
    "author_email": "ivanmrosa@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/4c/5b/9f68fc9a08fa6304e28fcc062f4a07767659d0c2f4ceccf8e8097de10f63/PyDInjector-1.0.2.tar.gz",
    "platform": null,
    "description": "# PYDInjector\n**Simple Dependency Injection Container for python**\n\n## Description\n\nPyDInjector is dependency injector container that facilitates to follow the *Inversion of Control* principle. PyDInjector uses the python type hint to\nresolve the dependencies and to inject them into the objects. To do that we need to configured the relation of the implemented objects with their types\n(Interfaces/ABC). After configured is possible to use the *@inject* decorator to indicates whether the container should inject the dependency.\n\nThere are to types of injections: the scoped injection and the singleton injection.\n\n### Scoped injection\n\nEscoped injection will provide a new instance of the object always that the scope where the @inject decorator was used is call. For example, if we decorate\nthe constructor *def __ini__(OurService : IOurService):*, the container will provide a new instance of *OurService* always whe instantiate this object. But \nif we decorate other method in this class like *def doSomenthing(OurService : IOurService):*, the container will provide a new instance of *OurService* \nalways that we execute the method *doSomenthing*.\n\n### Singleton injection\n\nSingleton injection will instantiate the object once and then provide the same instance every time it been requested.\n\n## Configuring the container injector\n\nThe configuration of the relationship between the types and the objects must be inserted at the beginning of the code execution. In order to be able to \ndo that is necessary to import the  *DIContainer* from *PyDInjector*. Then is possible to use the methods \n*DIContainer.AddScoped* and *DIContainer.AddSingleton*.\n\n\n    from PyDInjector import DIContainer\n    DIContainer.AddScoped(IBody, HumanBody)\n    DIContainer.AddScoped(IBodyAction, HumanBodyAction)\n    DIContainer.AddScoped(IFoodProvider, FoodProvider)\n    DIContainer.AddSingleton(ICooker, Cooker)\n\nThe first parameter is the Interface and the second one is the class implementation.\n\nSee the utilization below:\n\n\n    \n    class HumanBody(IBody):    \n        @inject #The container will inject a instance of BodyAction to this constructor\n        def __init__(self, actions: IBodyAction) -> None:        \n            super().__init__()\n            self.__actions : IBodyAction = actions\n            self.__identification : str = str(uuid.uuid1())    \n        def move(self) -> str:\n            return self.__actions.move()\n        def jump(self) -> str:\n            return self.__actions.jump()\n        def getLegsQuantity(self) -> int:\n            return 2\n        def getHandsQuantity(self) -> int:\n            return 2\n        def getInstanceIdentification(self) -> str:\n            return self.__identification\n        def getActionIdentification(self) -> str:\n            return self.__actions.getInstanceIdentification()\n        @inject #The container will inject a instance of FoodProvider tho this method. If the configuration was singleton, then will be always the same instance. Otherwise a new instance will be provided at each execution \n        def eat(self, foodProvider: IFoodProvider) -> str:\n            return self.__actions.eat(food=foodProvider.getFood())\n        \n    humanBody : IBody = HumanBody() #no need to pass the parameter here, because the container will provide the dependency       \n    humanBody.getHandsQuantity()\n    humanBody.eat() #no need to pass the parameter here, because the container will provide the dependency       \n\n\nIf you want to get an instance of an object manually, is possible to use the method *DIContainer.GetObject(IType)*.",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "Simple dependency injection container for python",
    "version": "1.0.2",
    "split_keywords": [
        "dependency injection",
        "di",
        "inversion of control",
        "ioc",
        "container",
        "dependency injection",
        "inversion of control",
        "python"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4c5b9f68fc9a08fa6304e28fcc062f4a07767659d0c2f4ceccf8e8097de10f63",
                "md5": "9ae4e2bb7034b539886355b382057f42",
                "sha256": "0e37ab8e6680e32f3b7e9ec4195673d7e8b05d3e1904bef7d519fe2f10d6c538"
            },
            "downloads": -1,
            "filename": "PyDInjector-1.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "9ae4e2bb7034b539886355b382057f42",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 3258,
            "upload_time": "2023-02-01T00:39:46",
            "upload_time_iso_8601": "2023-02-01T00:39:46.512622Z",
            "url": "https://files.pythonhosted.org/packages/4c/5b/9f68fc9a08fa6304e28fcc062f4a07767659d0c2f4ceccf8e8097de10f63/PyDInjector-1.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-02-01 00:39:46",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "ivanmrosa",
    "github_project": "PYDInjector",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "pydinjector"
}
        
Elapsed time: 0.03375s