mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 11:04:01 +03:00
0.0.2 version
This commit is contained in:
parent
5b9723c530
commit
2a29c43115
27
README.md
27
README.md
|
@ -11,7 +11,7 @@ Example:
|
||||||
Concept example of objects catalogs.
|
Concept example of objects catalogs.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import objects
|
from objects import Catalog, Singleton, NewInstance, InitArg, Attribute
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
|
|
||||||
|
@ -28,29 +28,30 @@ class B(object):
|
||||||
|
|
||||||
|
|
||||||
# Catalog of objects providers.
|
# Catalog of objects providers.
|
||||||
class Catalog(objects.Catalog):
|
class AppCatalog(Catalog):
|
||||||
"""
|
"""
|
||||||
Objects catalog.
|
Objects catalog.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
database = objects.Singleton(provides=sqlite3.Connection,
|
database = Singleton(sqlite3.Connection,
|
||||||
database='example.db')
|
InitArg('database', ':memory:'),
|
||||||
|
Attribute('row_factory', sqlite3.Row))
|
||||||
""" :type: (objects.Provider) -> sqlite3.Connection """
|
""" :type: (objects.Provider) -> sqlite3.Connection """
|
||||||
|
|
||||||
object_a = objects.NewInstance(provides=A,
|
object_a = NewInstance(A,
|
||||||
db=database)
|
InitArg('db', database))
|
||||||
""" :type: (objects.Provider) -> A """
|
""" :type: (objects.Provider) -> A """
|
||||||
|
|
||||||
object_b = objects.NewInstance(provides=B,
|
object_b = NewInstance(B,
|
||||||
a=object_a,
|
InitArg('a', object_a),
|
||||||
db=database)
|
InitArg('db', database))
|
||||||
""" :type: (objects.Provider) -> B """
|
""" :type: (objects.Provider) -> B """
|
||||||
|
|
||||||
|
|
||||||
# Catalog injection into consumer class.
|
# Catalog injection into consumer class.
|
||||||
class Consumer(object):
|
class Consumer(object):
|
||||||
catalog = Catalog(Catalog.object_a,
|
catalog = AppCatalog(AppCatalog.object_a,
|
||||||
Catalog.object_b)
|
AppCatalog.object_b)
|
||||||
|
|
||||||
def return_a_b(self):
|
def return_a_b(self):
|
||||||
return (self.catalog.object_a(),
|
return (self.catalog.object_a(),
|
||||||
|
@ -60,8 +61,8 @@ a1, b1 = Consumer().return_a_b()
|
||||||
|
|
||||||
|
|
||||||
# Catalog static provides.
|
# Catalog static provides.
|
||||||
a2 = Catalog.object_a()
|
a2 = AppCatalog.object_a()
|
||||||
b2 = Catalog.object_b()
|
b2 = AppCatalog.object_b()
|
||||||
|
|
||||||
# Some asserts.
|
# Some asserts.
|
||||||
assert a1 is not a2
|
assert a1 is not a2
|
||||||
|
|
Loading…
Reference in New Issue
Block a user