mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-12-01 22:14:04 +03:00
Removing old docs
This commit is contained in:
parent
6d3f212531
commit
f278f21101
|
@ -1,83 +0,0 @@
|
||||||
Providers
|
|
||||||
=========
|
|
||||||
|
|
||||||
|
|
||||||
Providers delegation
|
|
||||||
--------------------
|
|
||||||
|
|
||||||
Overriding of providers
|
|
||||||
-----------------------
|
|
||||||
|
|
||||||
Any provider can be overridden by another provider.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
"""Providers overriding example."""
|
|
||||||
|
|
||||||
import sqlite3
|
|
||||||
|
|
||||||
from objects.providers import Factory
|
|
||||||
from objects.providers import Singleton
|
|
||||||
|
|
||||||
from objects.injections import KwArg
|
|
||||||
from objects.injections import Attribute
|
|
||||||
|
|
||||||
|
|
||||||
class ObjectA(object):
|
|
||||||
|
|
||||||
"""ObjectA has dependency on database."""
|
|
||||||
|
|
||||||
def __init__(self, database):
|
|
||||||
"""Initializer.
|
|
||||||
|
|
||||||
Database dependency need to be injected via init arg."""
|
|
||||||
self.database = database
|
|
||||||
|
|
||||||
def get_one(self):
|
|
||||||
"""Select one from database and return it."""
|
|
||||||
return self.database.execute('SELECT 1')
|
|
||||||
|
|
||||||
|
|
||||||
class ObjectAMock(ObjectA):
|
|
||||||
|
|
||||||
"""Mock of ObjectA.
|
|
||||||
|
|
||||||
Has no dependency on database.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
"""Initializer."""
|
|
||||||
|
|
||||||
def get_one(self):
|
|
||||||
"""Select one from database and return it.
|
|
||||||
|
|
||||||
Mock makes no database queries and always returns two instead of one.
|
|
||||||
"""
|
|
||||||
return 2
|
|
||||||
|
|
||||||
|
|
||||||
# Database and `ObjectA` providers.
|
|
||||||
database = Singleton(sqlite3.Connection,
|
|
||||||
KwArg('database', ':memory:'),
|
|
||||||
KwArg('timeout', 30),
|
|
||||||
KwArg('detect_types', True),
|
|
||||||
KwArg('isolation_level', 'EXCLUSIVE'),
|
|
||||||
Attribute('row_factory', sqlite3.Row))
|
|
||||||
|
|
||||||
object_a_factory = Factory(ObjectA,
|
|
||||||
KwArg('database', database))
|
|
||||||
|
|
||||||
|
|
||||||
# Overriding `ObjectA` provider with `ObjectAMock` provider.
|
|
||||||
object_a_factory.override(Factory(ObjectAMock))
|
|
||||||
|
|
||||||
# Creating several `ObjectA` instances.
|
|
||||||
object_a_1 = object_a_factory()
|
|
||||||
object_a_2 = object_a_factory()
|
|
||||||
|
|
||||||
# Making some asserts.
|
|
||||||
assert object_a_1 is not object_a_2
|
|
||||||
assert object_a_1.get_one() == object_a_2.get_one() == 2
|
|
||||||
|
|
|
@ -1,37 +0,0 @@
|
||||||
Injections
|
|
||||||
==========
|
|
||||||
|
|
||||||
Injections are *Objects* entities that are used for specification of dependency
|
|
||||||
injection types.
|
|
||||||
|
|
||||||
Different functions, classes and objects can take dependency injections in
|
|
||||||
various forms. Some of them take dependencies like keyword arguments during
|
|
||||||
call time, other require setting of attributes or calling of specialized
|
|
||||||
methods for doing dependency injections.
|
|
||||||
|
|
||||||
So, when you are doing dependency injection you need to specify its type and
|
|
||||||
that is the place where *Injections* need to be used.
|
|
||||||
|
|
||||||
Some key points of *Objects* injections:
|
|
||||||
|
|
||||||
- Every *Objects* injection always takes injectable value as an
|
|
||||||
``injectable`` param. Every Python object could be an injectable.
|
|
||||||
- Every *Objects* injection always has ``value`` property that returns
|
|
||||||
injection's injectable. ``value`` property is calculated every time it is
|
|
||||||
accessed. Every Python object, except of *Objects* providers, that was
|
|
||||||
provided as and ``injectable`` will be returned by ``value`` property
|
|
||||||
*"as is"*. *Objects* providers will be called every time during ``value``
|
|
||||||
accessing and result of such calls will be returned.
|
|
||||||
- Every *Objects* *Injection* can have additional params that are needed
|
|
||||||
for doing particular type of injection.
|
|
||||||
|
|
||||||
There are several types of *Injections*:
|
|
||||||
|
|
||||||
- ``KwArg`` - is used for making keyword argument injections for any kind
|
|
||||||
of callables (functions, methods, objects instantiation and so on). Takes
|
|
||||||
keyword argument name as string and injectable.
|
|
||||||
- ``Attribute`` - is used for making injections by setting of injection's
|
|
||||||
value to a particular attribute. Takes attribute name as string and
|
|
||||||
injectable.
|
|
||||||
- ``Method`` - is used for making injections by calling of method with
|
|
||||||
injectable value. Takes method name as string and injectable.
|
|
Loading…
Reference in New Issue
Block a user