{ "info": { "author": "Jon\u00e1\u0161 Kulh\u00e1nek", "author_email": "jonas.kulhanek@live.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8" ], "description": "# Simple Dependency Injection for Python 3.6+\nType-based DI container for Python.\n\n## Introduction\nAutoDI is a simple dependency injection container for python. Install it by running:\n```\npython3 -m pip install autodi\n```\n\n## Concepts\nThe main entrypoint is the `Container` class. There is a default container created when you import the package called `container`. The container class allows you to register your classes so that they can be instantiated in the future. Container itself cannot create any service instances. It is there only to register your services. To create new instances, you need to create a `Provider` first. Look at the next example:\n\n```\nfrom autodi import container\n\nclass Service:\n def __init__(self):\n pass\n\n# The service is now registered in the container\ncontainer.register(Service)\n\n# Creates a new provider\nprovider = container.create_provider()\n\n# Resolves the service instance from the container\nservice = provider.create(Service)\n```\n\nThe default lifetime for all services is `transient`. It means a new instance is created with each call. There are two other options: `singleton`, `scoped`.\n\nThe service dependencies are dynamically resolved using type annotations.\n```\nfrom autodi import container\n\nclass Service:\n def __init__(self):\n pass\n\nclass ComplexService:\n def __init__(self, simple: Service):\n self.simple = simple\n\n# Both services now registered in the container\ncontainer.register(Service)\ncontainer.register(ComplexService)\n\n# Creates a new provider\nprovider = container.create_provider()\n\n# Resolves the service instance from the container\nservice = provider.create(ComplexService)\n\n# service.simple is now a new Service instance\n```\n\n## Registration\nThere are several ways how to register a new service.\n\n#### By strong name\nYou can register a service by its strong name. It is in the form \":\". You can register the service as its own implementation or you can bind it to an interface:\n```\ncontainer.register(\"my_package:ServiceInterface\", \"my_package:ServiceImpl\")\n```\nor simply by:\n```\ncontainer.register(\"my_package:ServiceImpl\")\n```\n\n#### By type\nYou can register the service as its own implementation or you can bind it to an interface:\n```\ncontainer.register(my_package:ServiceInterface, my_package:ServiceImpl)\n```\nor simply by:\n```\ncontainer.register(my_package:ServiceImpl)\n```\n\n#### As a factory\nYou can register your own factory that will create your service:\n```\ndef create_service(other: SimpleDependency):\n return Service(other, \"some argument\")\n\ncontainer.register(my_package:ServiceInterface, create_service)\n```\n\n#### Using decorator\nYou can use decorator to register your class to the default container:\n```\nfrom autodi import container\n\n@container.register\nclass Service:\n def __init__(self, x: Dependency):\n self.dependency = x\n\n```\n\nor as an interface implementation:\n```\nfrom autodi import container\n\nclass Interface:\n def __init__(self):\n pass\n\n@container.register(Interface)\nclass Service(Interface):\n def __init__(self, x: Dependency):\n self.dependency = x\n\nservice = container.create_provider().create(Interface)\n```\n\nYou can specify the lifetime type by another parameter to the registration call\n```\nfrom autodi import container, Lifetime\ncontainer.register(\"my_package:Service\", lifetime = Lifetime.singleton)\n```\n\n## Scoping\nScoping is supported by the container. It is a simple way how to create a new service for each http request for example.\n\n```\nfrom autodi import container, Lifetime\nclass HttpRequestService:\n def __init__(self):\n pass\n\ncontainer.register(HttpRequestService, Lifetime.scoped)\nprovider = container.create_provider()\n\n# When handling requests\nwith provider.scope() as s:\n service = s.create(HttpRequestService)\n\n```\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/jkulhanek/python-autodi", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "autodi", "package_url": "https://pypi.org/project/autodi/", "platform": "", "project_url": "https://pypi.org/project/autodi/", "project_urls": { "Homepage": "https://github.com/jkulhanek/python-autodi" }, "release_url": "https://pypi.org/project/autodi/0.1.1/", "requires_dist": null, "requires_python": "", "summary": "DI container for Python", "version": "0.1.1" }, "last_serial": 4979231, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "44b980867f1e328d22d41e409ae2286e", "sha256": "47a3dc167e4586ad99c6d819b33f132471d91c5a9146f075633d82e2ccfe3080" }, "downloads": -1, "filename": "autodi-0.1.0-py3-none-any.whl", "has_sig": false, "md5_digest": "44b980867f1e328d22d41e409ae2286e", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 4357, "upload_time": "2019-03-24T16:14:39", "url": "https://files.pythonhosted.org/packages/0c/24/1990dbd52814b47dfc499903a32c6c41226c0644f7a380fc6a3ae74b5c5d/autodi-0.1.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "c636f718929901bcb6c07c5297a0edd7", "sha256": "1e6c298d9f381b1a59a36ced3a19eb4263aa28b01eddbc41f4d294d340a0679b" }, "downloads": -1, "filename": "autodi-0.1.0.tar.gz", "has_sig": false, "md5_digest": "c636f718929901bcb6c07c5297a0edd7", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 2820, "upload_time": "2019-03-24T16:14:41", "url": "https://files.pythonhosted.org/packages/f1/2b/20c656c31239d49d328e4c432a3042caed8e5d72665112594494258da670/autodi-0.1.0.tar.gz" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "6d26dd1b75d08453473d19ffeb439d29", "sha256": "aad7b1acc0a5327bca9cefe38b532594a71272264ffc32a2c14a7807c2c5fb70" }, "downloads": -1, "filename": "autodi-0.1.1-py3-none-any.whl", "has_sig": false, "md5_digest": "6d26dd1b75d08453473d19ffeb439d29", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 5590, "upload_time": "2019-03-24T16:55:59", "url": "https://files.pythonhosted.org/packages/06/53/4b0b8c0637090310c777f23493a169b116385bcd86f2f77e51c5077094ea/autodi-0.1.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "097553be90870e7deba3977e7c9076d4", "sha256": "ce5f5d8f2cdf3fb191078114c8cf0d8e49b1ea196f29b3c74f89fab6283af346" }, "downloads": -1, "filename": "autodi-0.1.1.tar.gz", "has_sig": false, "md5_digest": "097553be90870e7deba3977e7c9076d4", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4308, "upload_time": "2019-03-24T16:56:01", "url": "https://files.pythonhosted.org/packages/08/03/692c9eee798fa21261eea08fa93e4a4e1fb64349fe0e683820d6e62aa4c3/autodi-0.1.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "6d26dd1b75d08453473d19ffeb439d29", "sha256": "aad7b1acc0a5327bca9cefe38b532594a71272264ffc32a2c14a7807c2c5fb70" }, "downloads": -1, "filename": "autodi-0.1.1-py3-none-any.whl", "has_sig": false, "md5_digest": "6d26dd1b75d08453473d19ffeb439d29", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 5590, "upload_time": "2019-03-24T16:55:59", "url": "https://files.pythonhosted.org/packages/06/53/4b0b8c0637090310c777f23493a169b116385bcd86f2f77e51c5077094ea/autodi-0.1.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "097553be90870e7deba3977e7c9076d4", "sha256": "ce5f5d8f2cdf3fb191078114c8cf0d8e49b1ea196f29b3c74f89fab6283af346" }, "downloads": -1, "filename": "autodi-0.1.1.tar.gz", "has_sig": false, "md5_digest": "097553be90870e7deba3977e7c9076d4", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4308, "upload_time": "2019-03-24T16:56:01", "url": "https://files.pythonhosted.org/packages/08/03/692c9eee798fa21261eea08fa93e4a4e1fb64349fe0e683820d6e62aa4c3/autodi-0.1.1.tar.gz" } ] }