From a42fefacef788c1335870850ea3df6acd7b36ce3 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Fri, 23 Oct 2015 17:04:39 +0300 Subject: [PATCH] Update some examples --- README.rst | 64 +++++++++++++++++---------------- dependency_injector/__init__.py | 2 +- docs/main/changelog.rst | 3 ++ examples/concept.py | 64 +++++++++++++++++---------------- 4 files changed, 72 insertions(+), 61 deletions(-) diff --git a/README.rst b/README.rst index 21aabde7..8938b12b 100644 --- a/README.rst +++ b/README.rst @@ -70,58 +70,62 @@ Examples import dependency_injector as di - class ObjectA(object): - """Example class ObjectA, that has dependency on database.""" + class UsersService(object): + """Users service, that has dependency on database.""" def __init__(self, db): """Initializer.""" self.db = db - class ObjectB(object): - """Example class ObjectB, that has dependencies on ObjectA and database.""" + class AuthService(object): + """Auth service, that has dependencies on users service and database.""" - def __init__(self, a, db): + def __init__(self, db, users_service): """Initializer.""" - self.a = a self.db = db + self.users_service = users_service - class Catalog(di.AbstractCatalog): - """Catalog of providers.""" + class Services(di.AbstractCatalog): + """Catalog of service providers.""" - database = di.Singleton(sqlite3.Connection, - database=':memory:') - """:type: (di.Provider) -> sqlite3.Connection""" + database = di.Singleton(sqlite3.connect, ':memory:') + """:type: () -> sqlite3.Connection""" - object_a_factory = di.Factory(ObjectA, - db=database) - """:type: (di.Provider) -> ObjectA""" + users = di.Factory(UsersService, + db=database) + """:type: () -> UsersService""" - object_b_factory = di.Factory(ObjectB, - a=object_a_factory, - db=database) - """:type: (di.Provider) -> ObjectB""" + auth = di.Factory(AuthService, + db=database, + users_service=users) + """:type: () -> AuthService""" - # Catalog static provides. - a1, a2 = Catalog.object_a_factory(), Catalog.object_a_factory() - b1, b2 = Catalog.object_b_factory(), Catalog.object_b_factory() + # Retrieving catalog providers: + users_service = Services.users() + auth_service = Services.auth() - assert a1 is not a2 - assert b1 is not b2 - assert a1.db is a2.db is b1.db is b2.db is Catalog.database() + # Making some asserts: + assert users_service.db is auth_service.db is Services.database() + assert isinstance(auth_service.users_service, UsersService) + assert users_service is not Services.users() + assert auth_service is not Services.auth() - # Example of inline injections. - @di.inject(a=Catalog.object_a_factory) - @di.inject(b=Catalog.object_b_factory) - @di.inject(database=Catalog.database) - def example(a, b, database): + # Making some "inline" injections: + @di.inject(users_service=Services.users) + @di.inject(auth_service=Services.auth) + @di.inject(database=Services.database) + def example(users_service, auth_service, database): """Example callback.""" - assert a.db is b.db is database is Catalog.database() + assert users_service.db is auth_service.db + assert auth_service.db is database + assert database is Services.database() + # Making a call of decorated callback: example() You can get more *Dependency Injector* examples in ``/examples`` directory on diff --git a/dependency_injector/__init__.py b/dependency_injector/__init__.py index 322f64be..e0d5cead 100644 --- a/dependency_injector/__init__.py +++ b/dependency_injector/__init__.py @@ -39,7 +39,7 @@ from .utils import ensure_is_catalog_bundle from .errors import Error -VERSION = '0.10.0' +VERSION = '0.10.1' __all__ = ( diff --git a/docs/main/changelog.rst b/docs/main/changelog.rst index 1a39d1d9..b02dd808 100644 --- a/docs/main/changelog.rst +++ b/docs/main/changelog.rst @@ -7,6 +7,9 @@ that were made in every particular version. From version 0.7.6 *Dependency Injector* framework strictly follows `Semantic versioning`_ +0.10.1 +------ +- Update some examples. 0.10.0 ------ diff --git a/examples/concept.py b/examples/concept.py index 6617c7ca..72f5e62a 100644 --- a/examples/concept.py +++ b/examples/concept.py @@ -4,56 +4,60 @@ import sqlite3 import dependency_injector as di -class ObjectA(object): - """Example class ObjectA, that has dependency on database.""" +class UsersService(object): + """Users service, that has dependency on database.""" def __init__(self, db): """Initializer.""" self.db = db -class ObjectB(object): - """Example class ObjectB, that has dependencies on ObjectA and database.""" +class AuthService(object): + """Auth service, that has dependencies on users service and database.""" - def __init__(self, a, db): + def __init__(self, db, users_service): """Initializer.""" - self.a = a self.db = db + self.users_service = users_service -class Catalog(di.AbstractCatalog): - """Catalog of providers.""" +class Services(di.AbstractCatalog): + """Catalog of service providers.""" - database = di.Singleton(sqlite3.Connection, - database=':memory:') - """:type: (di.Provider) -> sqlite3.Connection""" + database = di.Singleton(sqlite3.connect, ':memory:') + """:type: () -> sqlite3.Connection""" - object_a_factory = di.Factory(ObjectA, - db=database) - """:type: (di.Provider) -> ObjectA""" + users = di.Factory(UsersService, + db=database) + """:type: () -> UsersService""" - object_b_factory = di.Factory(ObjectB, - a=object_a_factory, - db=database) - """:type: (di.Provider) -> ObjectB""" + auth = di.Factory(AuthService, + db=database, + users_service=users) + """:type: () -> AuthService""" -# Catalog static provides. -a1, a2 = Catalog.object_a_factory(), Catalog.object_a_factory() -b1, b2 = Catalog.object_b_factory(), Catalog.object_b_factory() +# Retrieving catalog providers: +users_service = Services.users() +auth_service = Services.auth() -assert a1 is not a2 -assert b1 is not b2 -assert a1.db is a2.db is b1.db is b2.db is Catalog.database() +# Making some asserts: +assert users_service.db is auth_service.db is Services.database() +assert isinstance(auth_service.users_service, UsersService) +assert users_service is not Services.users() +assert auth_service is not Services.auth() -# Example of inline injections. -@di.inject(a=Catalog.object_a_factory) -@di.inject(b=Catalog.object_b_factory) -@di.inject(database=Catalog.database) -def example(a, b, database): +# Making some "inline" injections: +@di.inject(users_service=Services.users) +@di.inject(auth_service=Services.auth) +@di.inject(database=Services.database) +def example(users_service, auth_service, database): """Example callback.""" - assert a.db is b.db is database is Catalog.database() + assert users_service.db is auth_service.db + assert auth_service.db is database + assert database is Services.database() +# Making a call of decorated callback: example()