mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 11:04:01 +03:00
Last modifications on NewInstance to Factory provider renaming
This commit is contained in:
parent
0dd2884262
commit
a5337b2fc8
19
README.rst
19
README.rst
|
@ -84,12 +84,12 @@ Examples
|
||||||
|
|
||||||
from objects.catalog import AbstractCatalog
|
from objects.catalog import AbstractCatalog
|
||||||
|
|
||||||
|
from objects.providers import Factory
|
||||||
from objects.providers import Singleton
|
from objects.providers import Singleton
|
||||||
from objects.providers import NewInstance
|
|
||||||
|
|
||||||
from objects.injections import KwArg
|
from objects.injections import KwArg
|
||||||
from objects.injections import Attribute
|
from objects.injections import Attribute
|
||||||
from objects.injections import inject
|
from objects.decorators import inject
|
||||||
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
|
@ -122,19 +122,19 @@ Examples
|
||||||
Attribute('row_factory', sqlite3.Row))
|
Attribute('row_factory', sqlite3.Row))
|
||||||
""":type: (objects.Provider) -> sqlite3.Connection"""
|
""":type: (objects.Provider) -> sqlite3.Connection"""
|
||||||
|
|
||||||
object_a = NewInstance(ObjectA,
|
object_a_factory = Factory(ObjectA,
|
||||||
KwArg('db', database))
|
KwArg('db', database))
|
||||||
""":type: (objects.Provider) -> ObjectA"""
|
""":type: (objects.Provider) -> ObjectA"""
|
||||||
|
|
||||||
object_b = NewInstance(ObjectB,
|
object_b_factory = Factory(ObjectB,
|
||||||
KwArg('a', object_a),
|
KwArg('a', object_a_factory),
|
||||||
KwArg('db', database))
|
KwArg('db', database))
|
||||||
""":type: (objects.Provider) -> ObjectB"""
|
""":type: (objects.Provider) -> ObjectB"""
|
||||||
|
|
||||||
|
|
||||||
# Catalog static provides.
|
# Catalog static provides.
|
||||||
a1, a2 = Catalog.object_a(), Catalog.object_a()
|
a1, a2 = Catalog.object_a_factory(), Catalog.object_a_factory()
|
||||||
b1, b2 = Catalog.object_b(), Catalog.object_b()
|
b1, b2 = Catalog.object_b_factory(), Catalog.object_b_factory()
|
||||||
|
|
||||||
assert a1 is not a2
|
assert a1 is not a2
|
||||||
assert b1 is not b2
|
assert b1 is not b2
|
||||||
|
@ -142,8 +142,8 @@ Examples
|
||||||
|
|
||||||
|
|
||||||
# Example of inline injections.
|
# Example of inline injections.
|
||||||
@inject(KwArg('a', Catalog.object_a))
|
@inject(KwArg('a', Catalog.object_a_factory))
|
||||||
@inject(KwArg('b', Catalog.object_b))
|
@inject(KwArg('b', Catalog.object_b_factory))
|
||||||
@inject(KwArg('database', Catalog.database))
|
@inject(KwArg('database', Catalog.database))
|
||||||
def example(a, b, database):
|
def example(a, b, database):
|
||||||
assert a.db is b.db is database is Catalog.database()
|
assert a.db is b.db is database is Catalog.database()
|
||||||
|
@ -151,6 +151,7 @@ Examples
|
||||||
|
|
||||||
example()
|
example()
|
||||||
|
|
||||||
|
|
||||||
You can get more *Objects* examples in ``/examples`` directory on
|
You can get more *Objects* examples in ``/examples`` directory on
|
||||||
GitHub:
|
GitHub:
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ initialization.
|
||||||
|
|
||||||
There are few *Instance* providers:
|
There are few *Instance* providers:
|
||||||
|
|
||||||
- ``NewInstance`` provider creates new instance of specified class on every
|
- ``Factory`` provider creates new instance of specified class on every
|
||||||
call.
|
call.
|
||||||
- ``Singleton`` provider creates new instance of specified class on first
|
- ``Singleton`` provider creates new instance of specified class on first
|
||||||
call and returns same instance on every next call.
|
call and returns same instance on every next call.
|
||||||
|
@ -27,14 +27,14 @@ Example:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
"""`NewInstance` and `Singleton` providers example."""
|
"""`Factory` and `Singleton` providers example."""
|
||||||
|
|
||||||
from objects.providers import NewInstance
|
from objects.providers import Factory
|
||||||
from objects.providers import Singleton
|
from objects.providers import Singleton
|
||||||
|
|
||||||
|
|
||||||
# NewInstance provider creates new instance of specified class on every call.
|
# Factory provider creates new instance of specified class on every call.
|
||||||
new_object = NewInstance(object)
|
new_object = Factory(object)
|
||||||
|
|
||||||
object_1 = new_object()
|
object_1 = new_object()
|
||||||
object_2 = new_object()
|
object_2 = new_object()
|
||||||
|
@ -85,12 +85,12 @@ Example:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
"""`NewInstance` and `Singleton` providers with injections example."""
|
"""`Factory` and `Singleton` providers with injections example."""
|
||||||
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
from objects.providers import Singleton
|
from objects.providers import Singleton
|
||||||
from objects.providers import NewInstance
|
from objects.providers import Factory
|
||||||
|
|
||||||
from objects.injections import KwArg
|
from objects.injections import KwArg
|
||||||
from objects.injections import Attribute
|
from objects.injections import Attribute
|
||||||
|
@ -119,18 +119,19 @@ Example:
|
||||||
KwArg('isolation_level', 'EXCLUSIVE'),
|
KwArg('isolation_level', 'EXCLUSIVE'),
|
||||||
Attribute('row_factory', sqlite3.Row))
|
Attribute('row_factory', sqlite3.Row))
|
||||||
|
|
||||||
object_a = NewInstance(ObjectA,
|
object_a_factory = Factory(ObjectA,
|
||||||
KwArg('database', database))
|
KwArg('database', database))
|
||||||
|
|
||||||
# Creating several `ObjectA` instances.
|
# Creating several `ObjectA` instances.
|
||||||
object_a_1 = object_a()
|
object_a_1 = object_a_factory()
|
||||||
object_a_2 = object_a()
|
object_a_2 = object_a_factory()
|
||||||
|
|
||||||
# Making some asserts.
|
# Making some asserts.
|
||||||
assert object_a_1 is not object_a_2
|
assert object_a_1 is not object_a_2
|
||||||
assert object_a_1.database is object_a_2.database is database()
|
assert object_a_1.database is object_a_2.database is database()
|
||||||
assert object_a_1.get_one() == object_a_2.get_one() == 1
|
assert object_a_1.get_one() == object_a_2.get_one() == 1
|
||||||
|
|
||||||
|
|
||||||
Static providers
|
Static providers
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
|
@ -163,6 +164,7 @@ Example:
|
||||||
value_provider = Value(123)
|
value_provider = Value(123)
|
||||||
assert value_provider() == 123
|
assert value_provider() == 123
|
||||||
|
|
||||||
|
|
||||||
Callable providers
|
Callable providers
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
|
@ -236,7 +238,7 @@ Example:
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
from objects.providers import Singleton
|
from objects.providers import Singleton
|
||||||
from objects.providers import NewInstance
|
from objects.providers import Factory
|
||||||
from objects.providers import ExternalDependency
|
from objects.providers import ExternalDependency
|
||||||
|
|
||||||
from objects.injections import KwArg
|
from objects.injections import KwArg
|
||||||
|
@ -261,7 +263,7 @@ Example:
|
||||||
# Database and `ObjectA` providers.
|
# Database and `ObjectA` providers.
|
||||||
database = ExternalDependency(instance_of=sqlite3.Connection)
|
database = ExternalDependency(instance_of=sqlite3.Connection)
|
||||||
|
|
||||||
object_a = NewInstance(ObjectA,
|
object_a_factory = Factory(ObjectA,
|
||||||
KwArg('database', database))
|
KwArg('database', database))
|
||||||
|
|
||||||
# Satisfaction of external dependency.
|
# Satisfaction of external dependency.
|
||||||
|
@ -273,14 +275,15 @@ Example:
|
||||||
Attribute('row_factory', sqlite3.Row)))
|
Attribute('row_factory', sqlite3.Row)))
|
||||||
|
|
||||||
# Creating several `ObjectA` instances.
|
# Creating several `ObjectA` instances.
|
||||||
object_a_1 = object_a()
|
object_a_1 = object_a_factory()
|
||||||
object_a_2 = object_a()
|
object_a_2 = object_a_factory()
|
||||||
|
|
||||||
# Making some asserts.
|
# Making some asserts.
|
||||||
assert object_a_1 is not object_a_2
|
assert object_a_1 is not object_a_2
|
||||||
assert object_a_1.database is object_a_2.database is database()
|
assert object_a_1.database is object_a_2.database is database()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Config providers
|
Config providers
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
|
@ -300,8 +303,8 @@ Example:
|
||||||
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
|
from objects.providers import Factory
|
||||||
from objects.providers import Singleton
|
from objects.providers import Singleton
|
||||||
from objects.providers import NewInstance
|
|
||||||
|
|
||||||
from objects.injections import KwArg
|
from objects.injections import KwArg
|
||||||
from objects.injections import Attribute
|
from objects.injections import Attribute
|
||||||
|
@ -348,17 +351,18 @@ Example:
|
||||||
KwArg('isolation_level', 'EXCLUSIVE'),
|
KwArg('isolation_level', 'EXCLUSIVE'),
|
||||||
Attribute('row_factory', sqlite3.Row))
|
Attribute('row_factory', sqlite3.Row))
|
||||||
|
|
||||||
object_a = NewInstance(ObjectA,
|
object_a_factory = Factory(ObjectA,
|
||||||
KwArg('database', database))
|
KwArg('database', database))
|
||||||
|
|
||||||
|
|
||||||
# Overriding `ObjectA` provider with `ObjectAMock` provider.
|
# Overriding `ObjectA` provider with `ObjectAMock` provider.
|
||||||
object_a.override(NewInstance(ObjectAMock))
|
object_a_factory.override(Factory(ObjectAMock))
|
||||||
|
|
||||||
# Creating several `ObjectA` instances.
|
# Creating several `ObjectA` instances.
|
||||||
object_a_1 = object_a()
|
object_a_1 = object_a_factory()
|
||||||
object_a_2 = object_a()
|
object_a_2 = object_a_factory()
|
||||||
|
|
||||||
# Making some asserts.
|
# Making some asserts.
|
||||||
assert object_a_1 is not object_a_2
|
assert object_a_1 is not object_a_2
|
||||||
assert object_a_1.get_one() == object_a_2.get_one() == 2
|
assert object_a_1.get_one() == object_a_2.get_one() == 2
|
||||||
|
|
||||||
|
|
|
@ -2,12 +2,11 @@
|
||||||
|
|
||||||
from objects.catalog import AbstractCatalog
|
from objects.catalog import AbstractCatalog
|
||||||
|
|
||||||
|
from objects.providers import Factory
|
||||||
from objects.providers import Singleton
|
from objects.providers import Singleton
|
||||||
from objects.providers import NewInstance
|
|
||||||
|
|
||||||
from objects.injections import KwArg
|
from objects.injections import KwArg
|
||||||
from objects.injections import Attribute
|
from objects.injections import Attribute
|
||||||
|
|
||||||
from objects.decorators import inject
|
from objects.decorators import inject
|
||||||
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
@ -41,19 +40,19 @@ class Catalog(AbstractCatalog):
|
||||||
Attribute('row_factory', sqlite3.Row))
|
Attribute('row_factory', sqlite3.Row))
|
||||||
""":type: (objects.Provider) -> sqlite3.Connection"""
|
""":type: (objects.Provider) -> sqlite3.Connection"""
|
||||||
|
|
||||||
object_a = NewInstance(ObjectA,
|
object_a_factory = Factory(ObjectA,
|
||||||
KwArg('db', database))
|
KwArg('db', database))
|
||||||
""":type: (objects.Provider) -> ObjectA"""
|
""":type: (objects.Provider) -> ObjectA"""
|
||||||
|
|
||||||
object_b = NewInstance(ObjectB,
|
object_b_factory = Factory(ObjectB,
|
||||||
KwArg('a', object_a),
|
KwArg('a', object_a_factory),
|
||||||
KwArg('db', database))
|
KwArg('db', database))
|
||||||
""":type: (objects.Provider) -> ObjectB"""
|
""":type: (objects.Provider) -> ObjectB"""
|
||||||
|
|
||||||
|
|
||||||
# Catalog static provides.
|
# Catalog static provides.
|
||||||
a1, a2 = Catalog.object_a(), Catalog.object_a()
|
a1, a2 = Catalog.object_a_factory(), Catalog.object_a_factory()
|
||||||
b1, b2 = Catalog.object_b(), Catalog.object_b()
|
b1, b2 = Catalog.object_b_factory(), Catalog.object_b_factory()
|
||||||
|
|
||||||
assert a1 is not a2
|
assert a1 is not a2
|
||||||
assert b1 is not b2
|
assert b1 is not b2
|
||||||
|
@ -61,8 +60,8 @@ assert a1.db is a2.db is b1.db is b2.db is Catalog.database()
|
||||||
|
|
||||||
|
|
||||||
# Example of inline injections.
|
# Example of inline injections.
|
||||||
@inject(KwArg('a', Catalog.object_a))
|
@inject(KwArg('a', Catalog.object_a_factory))
|
||||||
@inject(KwArg('b', Catalog.object_b))
|
@inject(KwArg('b', Catalog.object_b_factory))
|
||||||
@inject(KwArg('database', Catalog.database))
|
@inject(KwArg('database', Catalog.database))
|
||||||
def example(a, b, database):
|
def example(a, b, database):
|
||||||
assert a.db is b.db is database is Catalog.database()
|
assert a.db is b.db is database is Catalog.database()
|
||||||
|
|
|
@ -73,7 +73,7 @@ class Factory(Provider):
|
||||||
|
|
||||||
"""Factory provider.
|
"""Factory provider.
|
||||||
|
|
||||||
Factory providers will create and return new instance on every call.
|
Factory provider creates new instance of specified class on every call.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__slots__ = ('provides', 'kwargs', 'attributes', 'methods')
|
__slots__ = ('provides', 'kwargs', 'attributes', 'methods')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user