object-graph-builder 0.0.2
Provides an interface to build the dependency injection container.
Keywords: Dependency injection,dependency-injection,dependency-injection-container,dependency-injection-framework,design-patterns,factory,ioc,ioc-container,python,pinject,singleton

object-graph-builder
====================

.. image:: https://img.shields.io/pypi/v/object-graph-builder.svg
   :target: https://pypi.org/project/object-graph-builder/
   :alt: Latest Version

.. image:: https://coveralls.io/repos/github/eshta/object-graph-builder/badge.svg
   :target: https://coveralls.io/github/eshta/object-graph-builder
   :alt: Coverage Status

.. image:: https://readthedocs.org/projects/object-graph-builder/badge/?version=latest
   :target: https://object-graph-builder.readthedocs.io/en/latest/?badge=latest
   :alt: Documentation Status

.. image:: https://travis-ci.org/eshta/object-graph-builder.svg?branch=master
   :target: https://travis-ci.org/eshta/object-graph-builder



Provides a multi-step dependency injection container builder to allow adding/recreating object graphs in different stages through an app life-cycle

Features
--------

* Offering a container builder, so one can dynamically build the object graph with many apps adding their specs, classes, modules
* Dependency Injection Container (`pinject <https://github.com/google/pinject>`_)


Requirements
------------

* Python 3.6+


Usage
-----

1. Framework Agnostic App
=========================
**In container.py**

.. code-block:: python

  import object_graph.builder

  builder =  object_graph.builder.ObjectGraphBuilder()


**App 1**

.. code-block:: python

  import container
  import pinject


  class MyService(object):
	  def __init__(long_name: SomeReallyLongClassName):
		  self.my_dep = long_name

  class MyBindingSpec(pinject.BindingSpec):
	   def configure(self, bind):
		   bind('long_name', to_class=SomeReallyLongClassName)

  container.builder.addBindingSpec(MyBindingSpec)

**App 2**

.. code-block:: python

  import container


  container.builder.addModules([app2.module1, app2.module2])

**Client**

.. code-block:: python

  import container

  object_graph = container.builder.get_object_graph()

  my_service = object_graph.provide(MyService)


2. Django Example
=================

One can define the builder in your settings.py and then import it
in each app and add the configurations you need

**In settings.py**

.. code-block:: python

  import object_graph.builder

  object_graph_builder =  object_graph.builder.ObjectGraphBuilder()


**App 1** in apps.App1Config.ready()

.. code-block:: python

  from config.settings import object_graph_builder
  import pinject


  class MyService(object):
	  def __init__(long_name: SomeReallyLongClassName):
		  self.my_dep = long_name

  class MyBindingSpec(pinject.BindingSpec):
	   def configure(self, bind):
		   bind('long_name', to_class=SomeReallyLongClassName)

  object_graph_builder.addBindingSpec(MyBindingSpec)

**App 2**
in apps.App2Config.ready()

.. code-block:: python

  from config.settings import object_graph_builder


  object_graph_builder.addModules([app2.module1, app2.module2])

**Client**

.. code-block:: python

  object_graph = object_graph_builder.get_object_graph()

  my_service = object_graph.provide(MyService)


Each time you call *object_graph_builder.get_object_graph()*, it will check if it needs to rebuild the object graph.


Prepare for development
-----------------------

A Python 3.6+ interpreter is required in addition to pipenv.

.. code-block:: bash

    $ make init


Now you're ready to run the tests:

.. code-block:: bash

    $ make test


Resources
---------

* `Documentation <https://object-graph-builder.readthedocs.io>`_
* `Bug Tracker <https://github.com/eshta/object-graph-builder/issues>`_
* `Code <https://github.com/eshta/object-graph-builder/>`_