Code Monkey home page Code Monkey logo

ioccontainer's Introduction

ioccontainer

Retrieving the container

Accessing the container is as simple as from ioccontainer import c. You can now use c to set up service providers and access services.

Defining a service

from ioccontainer import c

class MyService:
    def __init__(self, val: int=1):
        self.val = val

def provide_my_service():
    return MyService()

c.provide(MyService, provide_my_service)

You can also use the @provider decorator.

from ioccontainer import c, provider

@provider(MyService)
def provide_my_service():
    return MyService()

Using a service

from ioccontainer import c

my_service = c.get(MyService)

You can also use the @inject decorator. You can specify which service to inject into a variable as an argument, or use annotations.

from ioccontainer import inject

@inject(my_service=MyService)
def get_val_from_specified_service(my_service):
    return my_service.val

@inject('my_service')
def get_val_from_annotated_service(my_service: MyService):
    return my_service.val

You can specify as many injections as you want inside of the @inject decorator.

from ioccontainer import inject
@inject('db', 'my_service', users=UserRepository)
def do_something(db: DatabaseAdapter, action, my_service: MyService, users):
    pass

do_something(action='some_action')

Scopes

There are different scopes that you can apply to services.

NO_SCOPE

A new instance of the service will be created each time it is resolved. This is the default scope.

from ioccontainer import c, provider, scopes

@provider(MyService, scopes.NO_SCOPE)
def provide_my_service():
    return MyService()

first = c.get(MyService)
second = c.get(MyService)
print(first.val)  # 1
print(second.val)  # 1

first.val = 2
print(first.val)  # 2
print(second.val)  # 1

SINGLETON

The same instance of the service will be created each time it is resolved

from ioccontainer import c, provider, scopes

@provider(MyService, scopes.SINGLETON)
def provide_my_service():
    return MyService()

first = c.get(MyService)
second = c.get(MyService)
print(first.val)  # 1
print(second.val)  # 1

first.val = 2
print(first.val)  # 2
print(second.val)  # 2

THREAD

The same instance of the service will be created each time it is resolved within a thread.

from ioccontainer import provider, scopes

@provider(MyService, scopes.THREAD)
def provide_my_service():
    return MyService()

Running tests

Install the package with test dependencies

pip install -e ".[test]"

Run tox

tox

ioccontainer's People

Contributors

jmwri avatar

Watchers

James Cloos avatar  avatar

Forkers

mirror-dump

ioccontainer's Issues

Inject should override value if None

When using the @inject decorator, the provided parameter should be overridden if it is None. At the moment the provider parameter takes precedence 100% of the time.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.