mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 01:26:51 +03:00
Refactoring of examples according to NewInstance provider to Factory provider renaming
This commit is contained in:
parent
c824eae566
commit
0dd2884262
|
@ -3,7 +3,7 @@
|
|||
import sqlite3
|
||||
|
||||
from objects.providers import Singleton
|
||||
from objects.providers import NewInstance
|
||||
from objects.providers import Factory
|
||||
from objects.providers import ExternalDependency
|
||||
|
||||
from objects.injections import KwArg
|
||||
|
@ -28,8 +28,8 @@ class ObjectA(object):
|
|||
# Database and `ObjectA` providers.
|
||||
database = ExternalDependency(instance_of=sqlite3.Connection)
|
||||
|
||||
object_a = NewInstance(ObjectA,
|
||||
KwArg('database', database))
|
||||
object_a_factory = Factory(ObjectA,
|
||||
KwArg('database', database))
|
||||
|
||||
# Satisfaction of external dependency.
|
||||
database.override(Singleton(sqlite3.Connection,
|
||||
|
@ -40,8 +40,8 @@ database.override(Singleton(sqlite3.Connection,
|
|||
Attribute('row_factory', sqlite3.Row)))
|
||||
|
||||
# Creating several `ObjectA` instances.
|
||||
object_a_1 = object_a()
|
||||
object_a_2 = object_a()
|
||||
object_a_1 = object_a_factory()
|
||||
object_a_2 = object_a_factory()
|
||||
|
||||
# Making some asserts.
|
||||
assert object_a_1 is not object_a_2
|
||||
|
|
|
@ -7,7 +7,7 @@ import sqlite3
|
|||
|
||||
from flask import Flask
|
||||
|
||||
from objects.providers import NewInstance
|
||||
from objects.providers import Factory
|
||||
from objects.providers import Singleton
|
||||
from objects.injections import KwArg
|
||||
from objects.injections import Attribute
|
||||
|
@ -37,8 +37,8 @@ database = Singleton(sqlite3.Connection,
|
|||
KwArg('isolation_level', 'EXCLUSIVE'),
|
||||
Attribute('row_factory', sqlite3.Row))
|
||||
|
||||
object_a = NewInstance(ObjectA,
|
||||
KwArg('database', database))
|
||||
object_a_factory = Factory(ObjectA,
|
||||
KwArg('database', database))
|
||||
|
||||
|
||||
# Flask application.
|
||||
|
@ -48,7 +48,7 @@ app = Flask(__name__)
|
|||
# Flask view with inject decorator.
|
||||
@app.route('/')
|
||||
@inject(KwArg('database', database))
|
||||
@inject(KwArg('object_a', object_a))
|
||||
@inject(KwArg('object_a', object_a_factory))
|
||||
def hello(database):
|
||||
one = database.execute('SELECT 1').fetchone()[0]
|
||||
return 'Query returned {0}, db connection {1}'.format(one, database)
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
"""`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
|
||||
|
||||
|
||||
# NewInstance provider creates new instance of specified class on every call.
|
||||
new_object = NewInstance(object)
|
||||
# Factory provider creates new instance of specified class on every call.
|
||||
new_object = Factory(object)
|
||||
|
||||
object_1 = new_object()
|
||||
object_2 = new_object()
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
"""`NewInstance` and `Singleton` providers with injections example."""
|
||||
"""`Factory` and `Singleton` providers with injections example."""
|
||||
|
||||
import sqlite3
|
||||
|
||||
from objects.providers import Singleton
|
||||
from objects.providers import NewInstance
|
||||
from objects.providers import Factory
|
||||
|
||||
from objects.injections import KwArg
|
||||
from objects.injections import Attribute
|
||||
|
@ -32,12 +32,12 @@ database = Singleton(sqlite3.Connection,
|
|||
KwArg('isolation_level', 'EXCLUSIVE'),
|
||||
Attribute('row_factory', sqlite3.Row))
|
||||
|
||||
object_a = NewInstance(ObjectA,
|
||||
KwArg('database', database))
|
||||
object_a_factory = Factory(ObjectA,
|
||||
KwArg('database', database))
|
||||
|
||||
# Creating several `ObjectA` instances.
|
||||
object_a_1 = object_a()
|
||||
object_a_2 = object_a()
|
||||
object_a_1 = object_a_factory()
|
||||
object_a_2 = object_a_factory()
|
||||
|
||||
# Making some asserts.
|
||||
assert object_a_1 is not object_a_2
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
import sqlite3
|
||||
|
||||
from objects.providers import Factory
|
||||
from objects.providers import Singleton
|
||||
from objects.providers import NewInstance
|
||||
|
||||
from objects.injections import KwArg
|
||||
from objects.injections import Attribute
|
||||
|
@ -50,16 +50,16 @@ database = Singleton(sqlite3.Connection,
|
|||
KwArg('isolation_level', 'EXCLUSIVE'),
|
||||
Attribute('row_factory', sqlite3.Row))
|
||||
|
||||
object_a = NewInstance(ObjectA,
|
||||
KwArg('database', database))
|
||||
object_a_factory = Factory(ObjectA,
|
||||
KwArg('database', database))
|
||||
|
||||
|
||||
# Overriding `ObjectA` provider with `ObjectAMock` provider.
|
||||
object_a.override(NewInstance(ObjectAMock))
|
||||
object_a_factory.override(Factory(ObjectAMock))
|
||||
|
||||
# Creating several `ObjectA` instances.
|
||||
object_a_1 = object_a()
|
||||
object_a_2 = object_a()
|
||||
object_a_1 = object_a_factory()
|
||||
object_a_2 = object_a_factory()
|
||||
|
||||
# Making some asserts.
|
||||
assert object_a_1 is not object_a_2
|
||||
|
|
Loading…
Reference in New Issue
Block a user