mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 01:26:51 +03:00
modifying concepts
This commit is contained in:
parent
e00a5f8c76
commit
80e2a1fdf1
|
@ -2,7 +2,7 @@
|
||||||
Concept example of objects catalogs.
|
Concept example of objects catalogs.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import objects
|
from objects import Catalog, Singleton, NewInstance, KwArg, Attribute
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,30 +19,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(provides=sqlite3.Connection,
|
||||||
database=objects.KwArg('example.db'),
|
database=KwArg('example.db'),
|
||||||
row_factory=objects.Attribute(sqlite3.Row))
|
row_factory=Attribute(sqlite3.Row))
|
||||||
""" :type: (objects.Provider) -> sqlite3.Connection """
|
""" :type: (Provider) -> sqlite3.Connection """
|
||||||
|
|
||||||
object_a = objects.NewInstance(provides=A,
|
object_a = NewInstance(provides=A,
|
||||||
db=objects.KwArg(database))
|
db=KwArg(database))
|
||||||
""" :type: (objects.Provider) -> A """
|
""" :type: (Provider) -> A """
|
||||||
|
|
||||||
object_b = objects.NewInstance(provides=B,
|
object_b = NewInstance(provides=B,
|
||||||
a=objects.KwArg(object_a),
|
a=KwArg(object_a),
|
||||||
db=objects.KwArg(database))
|
db=KwArg(database))
|
||||||
""" :type: (objects.Provider) -> B """
|
""" :type: (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(),
|
||||||
|
@ -52,8 +52,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
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
Concept example of objects catalogs.
|
Concept example of objects catalogs.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import objects
|
from objects import Catalog, Singleton, NewInstance, KwArg, Attribute
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,30 +19,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(sqlite3.Connection,
|
database = Singleton(sqlite3.Connection,
|
||||||
objects.KwArg('database', ':memory:'),
|
KwArg('database', ':memory:'),
|
||||||
objects.Attribute('row_factory', sqlite3.Row))
|
Attribute('row_factory', sqlite3.Row))
|
||||||
""" :type: (objects.Provider) -> sqlite3.Connection """
|
""" :type: (objects.Provider) -> sqlite3.Connection """
|
||||||
|
|
||||||
object_a = objects.NewInstance(A,
|
object_a = NewInstance(A,
|
||||||
objects.KwArg('db', database))
|
KwArg('db', database))
|
||||||
""" :type: (objects.Provider) -> A """
|
""" :type: (objects.Provider) -> A """
|
||||||
|
|
||||||
object_b = objects.NewInstance(B,
|
object_b = NewInstance(B,
|
||||||
objects.KwArg('a', object_a),
|
KwArg('a', object_a),
|
||||||
objects.KwArg('db', database))
|
KwArg('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(),
|
||||||
|
@ -52,8 +52,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