Package: oop-di
IntroDependency Injection is the key pattern used to implement Inversion of Control and Dependency Inversion principles
(check D in SOLID).It makes it simple to standardize the way objects are initialized in the application.This package provides you with the set of components to automate dependency injection through the application.Installationpip install oop-diUsageTo make it work do the following steps:# Init the `ContainerDefinition` componentcontainer_definition=ContainerDefinition()# fill the service definitionscontainer_definition.add_service(SomeService)# compile the containercontainer=container_definition.compile()# get an instance of the service from the container ...service=container.get(SomeService)# ... or inject it. Kwonly arguments will be injected@container.inject()defsomething(*,service:SomeService):...something()# partial injection is also possibledefsomething_else(*,wont_inject:UnknownService,service:SomeService):...something_else(wont_inject=UnknownService())ExtensionsEnvExtensionYou can add all environment variables to your container by addingEnvExtensionto the container definition:container_definition=ContainerDefinition()container_definition.add_extension(EnvExtension())JsonExtensionYou can add define parameters/aliases/services in a JSON file.
Checkexamples/s4_json.pyfor more details.container_definition=ContainerDefinition()container_definition.add_extension(JsonExtension(Path(__file__).parent/"config.json"))Config example:{"parameters":{"test1_email":"test@example.com","test2_email":"test2@example.com","test3_email":"test3@example.com"},"services":{"examples.s4_module":[{"class":"ProductService"},{"class":"Mailer","name":"m1","tags":["admin_mailers"],"parameters":{"from_email":"test1_email"}},{"class":"Mailer","name":"m2","tags":["admin_mailers"],"parameters":{"from_email":"test2_email"}},{"class":"Mailer","name":"m3","tags":["admin_mailers"],"parameters":{"from_email":"test3_email"}},{"class":"MultiMailer","name":"@examples.s4_module.MailerInterface","parameters":{"mailers":"#admin_mailers"}}]}}Why notpython-dependency-injector?python-dependency-injectoris the most popular Python DI library with a large community, excellent docs, and tons
of features that are not available inoop-di. Probably,python-dependency-injectoris the best choice for you.
I've built this small library to handle the DI of my small experiments/pet projects.What I don't like inpython-dependency-injectorand tried to mitigate in this library:it requires a name for every service.oop-dican use class itself as the name of the service;it makes you pollute your function's params with= Provide[Container.service]defaults to inject something.oop-dijust auto-wires the services by checking the parameter's types;oop-dihas the tagging feature that can gather services into an array without an explicit definition of that array.Checkexamples/folder for more info:s1_simple.py- shows the basics: how to define services and how to inject the services somewhere.s2_extensions.py- how to split the definitions between modules.s3_tags.py,s3_tags2_dict.py- how to use tags to group services (list or dict grouping is possible).s4_json.py- JsonExtension usage example.