Skip to main content

Provides an interface to build the dependency injection container.

Project description

object-graph-builder

Latest Version Coverage Status Documentation Status https://travis-ci.org/eshta/object-graph-builder.svg?branch=master

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)

Requirements

  • Python 3.6+

Usage

1. Framework Agnostic App

In container.py

import object_graph.builder

builder =  object_graph.builder.ObjectGraphBuilder()

App 1

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

import container


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

Client

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

import object_graph.builder

object_graph_builder =  object_graph.builder.ObjectGraphBuilder()

App 1 in apps.App1Config.ready()

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()

from config.settings import object_graph_builder


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

Client

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.

$ make init

Now you’re ready to run the tests:

$ make test

Project details


Release history Release notifications

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for object-graph-builder, version 0.0.2
Filename, size File type Python version Upload date Hashes
Filename, size object-graph-builder-0.0.2.tar.gz (4.5 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page