{"id":17720348,"url":"https://github.com/BradLewis/simple-injection","last_synced_at":"2025-03-14T04:30:55.291Z","repository":{"id":42545781,"uuid":"294623299","full_name":"BradLewis/simple-injection","owner":"BradLewis","description":"An object-oriented approach to dependency injection in python.","archived":false,"fork":false,"pushed_at":"2022-04-17T15:41:18.000Z","size":68,"stargazers_count":12,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-08-08T16:54:23.200Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://simple-injection.readthedocs.io/en/latest/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BradLewis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-09-11T07:20:01.000Z","updated_at":"2024-06-13T10:14:10.000Z","dependencies_parsed_at":"2022-08-29T23:30:51.076Z","dependency_job_id":null,"html_url":"https://github.com/BradLewis/simple-injection","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BradLewis%2Fsimple-injection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BradLewis%2Fsimple-injection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BradLewis%2Fsimple-injection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BradLewis%2Fsimple-injection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BradLewis","download_url":"https://codeload.github.com/BradLewis/simple-injection/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243526372,"owners_count":20305108,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-25T15:26:42.873Z","updated_at":"2025-03-14T04:30:50.270Z","avatar_url":"https://github.com/BradLewis.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# Simple Injection\n\n[![Build Status](https://travis-ci.com/BradLewis/simple-injection.svg?branch=master)](https://travis-ci.com/BradLewis/simple-injection) [![codecov](https://codecov.io/gh/BradLewis/simple-injection/branch/master/graph/badge.svg)](https://codecov.io/gh/BradLewis/simple-injection) [![Documentation Status](https://readthedocs.org/projects/simple-injection/badge/?version=latest)](https://simple-injection.readthedocs.io/en/latest/?badge=latest) [![PyPI version](https://badge.fury.io/py/simple-injection.svg)](https://pypi.python.org/pypi/simple-injection/) [![PyPI license](https://img.shields.io/pypi/l/simple-injection.svg)](https://pypi.python.org/pypi/simple-injection/)\n\n\n## Introduction\n\nSimple Injection is a simple, objected-oriented approach to dependency injection in python.\nThe goal of Simple Injection is to allow simple and effective dependency injection in python applications without the use of anything other than what is natively in your application. This means no decorators or anything else anywhere in your code is required for Simple Injection. All that is required is python typings.\n\n## Features\n\n* Only dependent on native python code and typings. Services are injected through typing annotations, rather than variable names or decorators in your code.\n* Easily define service lifetimes.\n* Enforces typings in your application.\n* Bind abstract services to their implementations.\n* Add services in any order, resolution occurs when a service is request.\n\n## Installation\n\nSimply install Simple Injection through  [pip](https://pip.pypa.io/en/stable/).\n\n```bash\npip install simple-injection\n```\n\n## Usage\n\nDue to Simple Injection relying only on typings, it is easy to add to your application.\n\n```python\nfrom simple_injection import ServiceCollection\n\n\nclass Dependency:\n    def hello(self):\n        print(\"Hello from Dependency!\")\n\nclass Service:\n    def __init__(self, dependency: Dependency):\n        self._dependency = dependency\n\n    def hello(self):\n        self._dependency.hello()\n\ncollection = ServiceCollection()\ncollection.add_transient(Dependency)\ncollection.add_transient(Service)\n\ncollection.resolve(Service).hello()\n# Outputs: Hello from Dependency!\n```\n\nThis approach to dependency injection makes it easy to use mocks when developing and unit testing.\n\n```python\nclass MockDependency:\n    def hello(self):\n        print(\"Hello from MockDependency!\")\n\ncollection = ServiceCollection()\ncollection.add_transient(Dependency, MockDependency)\ncollection.add_transient(Service)\n\ncollection.resolve(Service).hello()\n# Outputs: Hello from MockDependency!\n```\n\nThis can also be achieved through the use of an interface (or base class) that both the dependency and the mock inherit from, but as the above example shows, it is not required.\n\nSimple Injection will also allow you to simply inject strings and other constants to your dependencies, easily injecting the needed dependency to your class with the constant.\n\n```python\nfrom simple_injection import ServiceCollection, ServiceResolverFlags\n\n\nclass Dependency:\n    def __init__(self, my_str: str):\n        self.my_str = my_str\n\nclass Service:\n    def __init__(self, dependency: Dependency, my_int: int):\n        self.my_int\n        self._dependency = dependency\n\n    def get_str(self):\n        return self._dependency.my_str\n\ncollection = ServiceCollection()\ncollection.add_transient(Dependency, args=[\"Example string!\"])\ncollection.add_transient(Service, args=[ServiceResolverFlags.REQUIRED_SERVICE ,23])\n\nservice = collection.resolve(Service)\nservice.my_int # 23\nservice.get_str() # Example string!\n```\n\nSee [examples](./examples) for more examples.\n\n## Documentation\n\nDocumentation for Simple Injection can be found on [readthedocs](https://simple-injection.readthedocs.io/en/latest/).\n\n## Contributing\n\nContributions are more than welcome. Feel welcome to add issues or make pull requests!\n\n## License\n\n[MIT](https://choosealicense.com/licenses/mit/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBradLewis%2Fsimple-injection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FBradLewis%2Fsimple-injection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBradLewis%2Fsimple-injection/lists"}