mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
Adding injections example
This commit is contained in:
parent
3ee60fa01b
commit
e1009373bc
52
README.md
52
README.md
|
@ -73,12 +73,60 @@ arguments, other are using attributes or methods to be initialized. Injection,
|
|||
in terms of `Objects`, is an instruction how to provide dependency for the
|
||||
particular object.
|
||||
|
||||
Every Python object could be an injection value. Special case is a `Objects`
|
||||
provider as an injection value. In such case, injection value is a result of
|
||||
Every Python object could be an injection's value. Special case is a `Objects`
|
||||
provider as an injection's value. In such case, injection value is a result of
|
||||
injectable provider call (every time injection is done).
|
||||
|
||||
Injections are used by providers.
|
||||
|
||||
```python
|
||||
"""`KwArg` and `Attribute` injections example."""
|
||||
|
||||
import sqlite3
|
||||
|
||||
from objects.providers import Singleton
|
||||
from objects.providers import NewInstance
|
||||
|
||||
from objects.injections import KwArg
|
||||
from objects.injections import Attribute
|
||||
|
||||
|
||||
class ObjectA(object):
|
||||
|
||||
"""ObjectA has dependency on database."""
|
||||
|
||||
def __init__(self, database):
|
||||
"""Initializer.
|
||||
|
||||
Database dependency need to be injected via init arg."""
|
||||
self.database = database
|
||||
|
||||
def get_one(self):
|
||||
"""Select one from database and return it."""
|
||||
return self.database.execute('SELECT 1').fetchone()[0]
|
||||
|
||||
|
||||
# Database and `ObjectA` providers.
|
||||
database = Singleton(sqlite3.Connection,
|
||||
KwArg('database', ':memory:'),
|
||||
KwArg('timeout', 30),
|
||||
KwArg('detect_types', True),
|
||||
KwArg('isolation_level', 'EXCLUSIVE'),
|
||||
Attribute('row_factory', sqlite3.Row))
|
||||
|
||||
object_a = NewInstance(ObjectA,
|
||||
KwArg('database', database))
|
||||
|
||||
# Creating several `ObjectA` instances.
|
||||
object_a_1 = object_a()
|
||||
object_a_2 = object_a()
|
||||
|
||||
# Making some asserts.
|
||||
assert object_a_1 is not object_a_2
|
||||
assert object_a_1.database is object_a_2.database
|
||||
assert object_a_1.get_one() == object_a_2.get_one() == 1
|
||||
```
|
||||
|
||||
### Catalogs
|
||||
|
||||
Catalogs are named set of providers.
|
||||
|
|
45
examples/readme/injections.py
Normal file
45
examples/readme/injections.py
Normal file
|
@ -0,0 +1,45 @@
|
|||
"""`KwArg` and `Attribute` injections example."""
|
||||
|
||||
import sqlite3
|
||||
|
||||
from objects.providers import Singleton
|
||||
from objects.providers import NewInstance
|
||||
|
||||
from objects.injections import KwArg
|
||||
from objects.injections import Attribute
|
||||
|
||||
|
||||
class ObjectA(object):
|
||||
|
||||
"""ObjectA has dependency on database."""
|
||||
|
||||
def __init__(self, database):
|
||||
"""Initializer.
|
||||
|
||||
Database dependency need to be injected via init arg."""
|
||||
self.database = database
|
||||
|
||||
def get_one(self):
|
||||
"""Select one from database and return it."""
|
||||
return self.database.execute('SELECT 1').fetchone()[0]
|
||||
|
||||
|
||||
# Database and `ObjectA` providers.
|
||||
database = Singleton(sqlite3.Connection,
|
||||
KwArg('database', ':memory:'),
|
||||
KwArg('timeout', 30),
|
||||
KwArg('detect_types', True),
|
||||
KwArg('isolation_level', 'EXCLUSIVE'),
|
||||
Attribute('row_factory', sqlite3.Row))
|
||||
|
||||
object_a = NewInstance(ObjectA,
|
||||
KwArg('database', database))
|
||||
|
||||
# Creating several `ObjectA` instances.
|
||||
object_a_1 = object_a()
|
||||
object_a_2 = object_a()
|
||||
|
||||
# Making some asserts.
|
||||
assert object_a_1 is not object_a_2
|
||||
assert object_a_1.database is object_a_2.database
|
||||
assert object_a_1.get_one() == object_a_2.get_one() == 1
|
Loading…
Reference in New Issue
Block a user