@@ -29,77 +29,101 @@ $ pip install py-dependency-injection
2929
3030The following examples demonstrates how to use the library.
3131
32- ### Example: Obtaining the Default Dependency Container
32+ ### Obtaining the default dependency container
3333
3434``` python
35- # Retrieve the default container, typically recommended for a single-application setup.
35+ # Typically all you need for a single-application setup.
3636
3737from dependency_injection.container import DependencyContainer
3838
39-
4039dependency_container = DependencyContainer.get_instance()
4140```
4241
43- ### Example: Obtaining a Second Dependency Container
42+ ### Obtaining multiple dependency containers
4443
4544``` python
46- # Create additional containers if needed, especially for multi-application scenarios.
45+ # Typically needed for multi-application scenarios.
4746
4847from dependency_injection.container import DependencyContainer
4948
50-
51- a_second_dependency_container = DependencyContainer.get_instance(name = " a_second_dependency_container" )
49+ second_container = DependencyContainer.get_instance(name = " second_container" )
50+ third_container = DependencyContainer.get_instance(name = " third_container" )
51+ # ...
5252```
5353
54- ### Example: Registering Dependencies
54+ ### Registering dependencies with the container
5555
5656``` python
57- # Register dependencies with three available scopes: transient, scoped, or singleton.
57+ # Register dependencies using one of the three available scopes;
58+ # transient, scoped, or singleton
5859
5960dependency_container.register_transient(SomeInterface, SomeClass)
6061dependency_container.register_scoped(AnotherInterface, AnotherClass)
6162dependency_container.register_singleton(ThirdInterface, ThirdClass)
6263```
6364
64- ### Example: Resolving Dependencies
65+ ### Resolving dependencies using the container
6566
6667``` python
6768# Resolve transient instance (created anew for each call).
6869transient_instance = dependency_container.resolve(SomeInterface)
6970
70- # Resolve scoped instance (consistent within a specific scope, e.g. a scope for the application action being run ).
71- scoped_instance = dependency_container.resolve(AnotherInterface, scope_name = " application_action_scope " )
71+ # Resolve scoped instance (consistent within a specific scope).
72+ scoped_instance = dependency_container.resolve(AnotherInterface, scope_name = " some_scope " )
7273
7374# Resolve singleton instance (consistent across the entire application).
7475singleton_instance = dependency_container.resolve(ThirdInterface)
7576```
7677
77- ### Example: Constructor Injection
78+ ### Constructor injection
7879
7980``` python
80- # Class instances resolved through the container have dependencies injected into their constructors.
81+ # Class instances resolved through the container have
82+ # dependencies injected into their constructors automatically.
8183
8284class Foo :
8385
84- def __init__ (self , transient_instance : SomeInterface, scoped_instance : AnotherInterface, singleton_instance : ThirdInterface):
86+ def __init__ (
87+ self ,
88+ transient_instance : SomeInterface,
89+ scoped_instance : AnotherInterface,
90+ singleton_instance : ThirdInterface
91+ ):
8592 self ._transient_instance = transient_instance
8693 self ._scoped_instance = scoped_instance
8794 self ._singleton_instance = singleton_instance
8895```
8996
90- ### Example: Method Injection
97+ ### Method injection with @ inject decorator
9198
9299``` python
93- # Inject dependencies into instance methods using the `@inject` decorator .
94- # You may pass 'container_name' and 'scope_name' as decorator arguments .
100+ # The decorator can be applied to classmethods and staticmethods .
101+ # Instance method injection is not allowed .
95102
96103from dependency_injection.decorator import inject
97104
98-
99105class Foo :
100106
107+ # Class method
108+ @ classmethod
109+ @inject ()
110+ def bar_class (cls , transient_instance : SomeInterface, scoped_instance : AnotherInterface, singleton_instance : ThirdInterface):
111+ transient_instance.do_something()
112+ scoped_instance.do_something()
113+ singleton_instance.do_something()
114+
115+ # Static method
116+ @ staticmethod
101117 @inject ()
102- def bar (self , transient_instance : SomeInterface, scoped_instance : AnotherInterface, singleton_instance : ThirdInterface):
118+ def bar_static_method (transient_instance : SomeInterface, scoped_instance : AnotherInterface, singleton_instance : ThirdInterface):
119+ transient_instance.do_something()
120+ scoped_instance.do_something()
121+ singleton_instance.do_something()
122+
123+ # Injecting with non-default container and scope
124+ @ staticmethod
125+ @inject (container_name = " second_container" , scope_name = " some_scope" )
126+ def bar_with_decorator_arguments (transient_instance : SomeInterface, scoped_instance : AnotherInterface, singleton_instance : ThirdInterface):
103127 transient_instance.do_something()
104128 scoped_instance.do_something()
105129 singleton_instance.do_something()
0 commit comments