mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-03-03 19:25:48 +03:00
Update readme with new concept example
This commit is contained in:
parent
01970dba27
commit
234e5cb3fc
41
README.rst
41
README.rst
|
@ -65,16 +65,8 @@ Examples
|
||||||
|
|
||||||
"""Concept example of `Dependency Injector`."""
|
"""Concept example of `Dependency Injector`."""
|
||||||
|
|
||||||
from dependency_injector.catalog import AbstractCatalog
|
|
||||||
|
|
||||||
from dependency_injector.providers import Factory
|
|
||||||
from dependency_injector.providers import Singleton
|
|
||||||
|
|
||||||
from dependency_injector.injections import KwArg
|
|
||||||
from dependency_injector.injections import Attribute
|
|
||||||
from dependency_injector.injections import inject
|
|
||||||
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
import dependency_injector as di
|
||||||
|
|
||||||
|
|
||||||
class ObjectA(object):
|
class ObjectA(object):
|
||||||
|
@ -96,23 +88,22 @@ Examples
|
||||||
self.db = db
|
self.db = db
|
||||||
|
|
||||||
|
|
||||||
class Catalog(AbstractCatalog):
|
class Catalog(di.AbstractCatalog):
|
||||||
|
|
||||||
"""Catalog of dependency_injector providers."""
|
"""Catalog of providers."""
|
||||||
|
|
||||||
database = Singleton(sqlite3.Connection,
|
database = di.Singleton(sqlite3.Connection,
|
||||||
KwArg('database', ':memory:'),
|
database=':memory:')
|
||||||
Attribute('row_factory', sqlite3.Row))
|
""":type: (di.Provider) -> sqlite3.Connection"""
|
||||||
""":type: (dependency_injector.Provider) -> sqlite3.Connection"""
|
|
||||||
|
|
||||||
object_a_factory = Factory(ObjectA,
|
object_a_factory = di.Factory(ObjectA,
|
||||||
KwArg('db', database))
|
db=database)
|
||||||
""":type: (dependency_injector.Provider) -> ObjectA"""
|
""":type: (di.Provider) -> ObjectA"""
|
||||||
|
|
||||||
object_b_factory = Factory(ObjectB,
|
object_b_factory = di.Factory(ObjectB,
|
||||||
KwArg('a', object_a_factory),
|
a=object_a_factory,
|
||||||
KwArg('db', database))
|
db=database)
|
||||||
""":type: (dependency_injector.Provider) -> ObjectB"""
|
""":type: (di.Provider) -> ObjectB"""
|
||||||
|
|
||||||
|
|
||||||
# Catalog static provides.
|
# Catalog static provides.
|
||||||
|
@ -125,9 +116,9 @@ Examples
|
||||||
|
|
||||||
|
|
||||||
# Example of inline injections.
|
# Example of inline injections.
|
||||||
@inject(KwArg('a', Catalog.object_a_factory))
|
@di.inject(a=Catalog.object_a_factory)
|
||||||
@inject(KwArg('b', Catalog.object_b_factory))
|
@di.inject(b=Catalog.object_b_factory)
|
||||||
@inject(KwArg('database', Catalog.database))
|
@di.inject(database=Catalog.database)
|
||||||
def example(a, b, database):
|
def example(a, b, database):
|
||||||
"""Example callback."""
|
"""Example callback."""
|
||||||
assert a.db is b.db is database is Catalog.database()
|
assert a.db is b.db is database is Catalog.database()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user