mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-06-13 18:13:12 +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
|
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
|
||||||
|
@ -28,8 +28,8 @@ class ObjectA(object):
|
||||||
# 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.
|
||||||
database.override(Singleton(sqlite3.Connection,
|
database.override(Singleton(sqlite3.Connection,
|
||||||
|
@ -40,8 +40,8 @@ database.override(Singleton(sqlite3.Connection,
|
||||||
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
|
||||||
|
|
|
@ -7,7 +7,7 @@ import sqlite3
|
||||||
|
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
|
|
||||||
from objects.providers import NewInstance
|
from objects.providers import Factory
|
||||||
from objects.providers import Singleton
|
from objects.providers import Singleton
|
||||||
from objects.injections import KwArg
|
from objects.injections import KwArg
|
||||||
from objects.injections import Attribute
|
from objects.injections import Attribute
|
||||||
|
@ -37,8 +37,8 @@ database = Singleton(sqlite3.Connection,
|
||||||
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))
|
||||||
|
|
||||||
|
|
||||||
# Flask application.
|
# Flask application.
|
||||||
|
@ -48,7 +48,7 @@ app = Flask(__name__)
|
||||||
# Flask view with inject decorator.
|
# Flask view with inject decorator.
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
@inject(KwArg('database', database))
|
@inject(KwArg('database', database))
|
||||||
@inject(KwArg('object_a', object_a))
|
@inject(KwArg('object_a', object_a_factory))
|
||||||
def hello(database):
|
def hello(database):
|
||||||
one = database.execute('SELECT 1').fetchone()[0]
|
one = database.execute('SELECT 1').fetchone()[0]
|
||||||
return 'Query returned {0}, db connection {1}'.format(one, database)
|
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
|
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()
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
"""`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
|
||||||
|
@ -32,12 +32,12 @@ database = Singleton(sqlite3.Connection,
|
||||||
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
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
|
|
||||||
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
|
||||||
|
@ -50,16 +50,16 @@ database = Singleton(sqlite3.Connection,
|
||||||
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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user