File tree Expand file tree Collapse file tree 4 files changed +47
-0
lines changed
Expand file tree Collapse file tree 4 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ Container copying
2+ -----------------
3+
4+ You can create declarative container copies using ``@containers.copy() `` decorator.
5+
6+ .. literalinclude :: ../../examples/containers/declarative_copy_decorator.py
7+ :language: python
8+ :lines: 3-
9+ :emphasize-lines: 18-22
10+
11+ Decorator ``@containers.copy() `` copies providers from source container to destination container.
12+ Destination container provider will replace source provider, if names match.
13+
14+ .. disqus ::
Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ Containers module API docs - :py:mod:`dependency_injector.containers`.
2323 dynamic
2424 specialization
2525 overriding
26+ copying
2627 reset_singletons
2728 check_dependencies
2829 traversal
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ follows `Semantic versioning`_
99
1010Development version
1111-------------------
12+ - Add docs on ``@containers.copy() `` decorator.
1213- Refactor ``@containers.copy() `` decorator.
1314- Refactor async mode support in containers module.
1415
Original file line number Diff line number Diff line change 1+ """Declarative container provider copying with ``@copy()`` decorator."""
2+
3+ import sqlite3
4+ from unittest import mock
5+
6+ from dependency_injector import containers , providers
7+
8+
9+ class Service :
10+ def __init__ (self , db ):
11+ self .db = db
12+
13+
14+ class SourceContainer (containers .DeclarativeContainer ):
15+
16+ database = providers .Singleton (sqlite3 .connect , ':memory:' )
17+ service = providers .Factory (Service , db = database )
18+
19+
20+ # Copy ``SourceContainer`` providers into ``DestinationContainer``:
21+ @containers .copy (SourceContainer )
22+ class DestinationContainer (SourceContainer ):
23+
24+ database = providers .Singleton (mock .Mock )
25+
26+
27+ if __name__ == '__main__' :
28+ container = DestinationContainer ()
29+
30+ service = container .service ()
31+ assert isinstance (service .db , mock .Mock )
You can’t perform that action at this time.
0 commit comments