2015-05-08 18:44:44 +03:00
|
|
|
Catalogs
|
|
|
|
========
|
2015-07-28 09:46:35 +03:00
|
|
|
|
2015-08-04 17:05:34 +03:00
|
|
|
Catalogs are collections of providers. Main purpose of catalogs is to group
|
|
|
|
providers.
|
|
|
|
|
2015-09-02 12:23:18 +03:00
|
|
|
There are, actually, several popular cases of catalogs usage:
|
2015-08-04 17:05:34 +03:00
|
|
|
|
2015-09-02 12:23:18 +03:00
|
|
|
- Grouping of providers from the same architectural layer (for example,
|
2015-08-04 17:05:34 +03:00
|
|
|
``Services``, ``Models`` and ``Forms`` catalogs).
|
2015-09-02 12:23:18 +03:00
|
|
|
- Grouping of providers from the same functional groups (for example,
|
2015-08-04 17:05:34 +03:00
|
|
|
catalog ``Users``, that contains all functional parts of ``Users``
|
|
|
|
component).
|
|
|
|
|
2015-09-02 12:23:18 +03:00
|
|
|
Also, for both of these and some other cases, it might be useful to attach
|
2015-08-05 16:44:00 +03:00
|
|
|
some init / shutdown functionality or something else, that deals with group
|
|
|
|
of providers.
|
|
|
|
|
2015-07-28 09:46:35 +03:00
|
|
|
Writing catalogs
|
|
|
|
----------------
|
|
|
|
|
2015-09-02 12:23:18 +03:00
|
|
|
Catalogs have to extend base catalog class ``di.AbstractCatalog``.
|
2015-08-04 17:05:34 +03:00
|
|
|
|
|
|
|
Providers have to be defined like catalog's attributes. Every provider in
|
2015-09-02 12:23:18 +03:00
|
|
|
catalog has name. This name should follow ``some_provider`` convention,
|
|
|
|
that is standard naming convention for attribute names in Python.
|
2015-08-04 17:05:34 +03:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2015-09-02 12:23:18 +03:00
|
|
|
It might be useful to add such ``""":type: (di.Provider) -> Object1"""``
|
|
|
|
documentation blocks one line after provider definition for every provider.
|
|
|
|
It will help code analyzing tools and IDE's to understand that variable
|
|
|
|
above contains some callable object, that returns particular instance
|
|
|
|
in a result of call.
|
2015-08-04 17:05:34 +03:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2015-08-05 16:44:00 +03:00
|
|
|
.. image:: /images/catalogs/simple.png
|
|
|
|
:width: 100%
|
|
|
|
:align: center
|
|
|
|
|
2015-08-05 17:24:03 +03:00
|
|
|
.. literalinclude:: ../../examples/catalogs/simple.py
|
2015-08-04 17:05:34 +03:00
|
|
|
:language: python
|
|
|
|
|
2015-08-05 16:44:00 +03:00
|
|
|
Operating with catalog providers
|
|
|
|
--------------------------------
|
|
|
|
|
2015-10-07 19:41:53 +03:00
|
|
|
``di.AbstractCatalog`` has several features that could be useful for some kind
|
|
|
|
of operations on catalog's providers:
|
|
|
|
|
|
|
|
- ``di.AbstractCatalog.providers`` is read-only attribute that contains
|
|
|
|
``dict`` of all catalog providers, including providers that are inherited
|
|
|
|
from parent catalogs, where key is the name of provider and value is
|
|
|
|
provider itself.
|
|
|
|
- ``di.AbstractCatalog.cls_providers`` is read-only attribute contains ``dict``
|
|
|
|
of current catalog providers, where key is the name of provider and value is
|
|
|
|
provider itself.
|
|
|
|
- ``di.AbstractCatalog.inherited_providers`` is read-only attribute contains
|
|
|
|
``dict`` of all providers that are inherited from parent catalogs, where key
|
|
|
|
is the name of provider and value is provider itself.
|
|
|
|
- ``di.AbstractCatalog.filter(provider_type=di.Provider)`` is a class method
|
|
|
|
that could be used for filtering catalog providers by provider types
|
|
|
|
(for example, for getting all ``di.Factory`` providers).
|
|
|
|
``di.AbstractCatalog.filter()`` method use ``di.AbstractCatalog.providers``.
|
2015-08-05 16:44:00 +03:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2015-08-05 17:24:03 +03:00
|
|
|
.. literalinclude:: ../../examples/catalogs/operating_with_providers.py
|
2015-08-05 16:44:00 +03:00
|
|
|
:language: python
|
|
|
|
|
2015-08-04 17:05:34 +03:00
|
|
|
Overriding of catalogs
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
Catalogs can be overridden by other catalogs. This, actually, means that
|
|
|
|
all of the providers from overriding catalog will override providers with the
|
|
|
|
same names in overridden catalog.
|
|
|
|
|
|
|
|
There are two ways to override catalog by another catalog:
|
|
|
|
|
2015-09-02 12:23:18 +03:00
|
|
|
- Use ``di.AbstractCatalog.override(AnotherCatalog)`` method.
|
|
|
|
- Use ``@di.override(AnotherCatalog)`` class decorator.
|
2015-08-04 17:05:34 +03:00
|
|
|
|
2015-09-02 12:23:18 +03:00
|
|
|
Example of overriding catalog using ``di.AbstractCatalog.override()`` method:
|
2015-08-04 17:05:34 +03:00
|
|
|
|
2015-08-05 17:24:03 +03:00
|
|
|
.. literalinclude:: ../../examples/catalogs/override.py
|
2015-08-04 17:05:34 +03:00
|
|
|
:language: python
|
|
|
|
|
2015-09-02 12:23:18 +03:00
|
|
|
Example of overriding catalog using ``@di.override()`` decorator:
|
2015-07-28 09:46:35 +03:00
|
|
|
|
2015-08-05 17:24:03 +03:00
|
|
|
.. literalinclude:: ../../examples/catalogs/override_decorator.py
|
2015-08-04 17:05:34 +03:00
|
|
|
:language: python
|