yaak.inject

YAAK stands for Yet Another Application Kit. It’s a set of tools that help developing enterprise applications in python.

yaak.inject is a package from the YAAK toolkit that provides dependency injection to your applications. See this Martin Fowler’s article for an explanation of dependency injection and its usefulness when developing enterprise application.

Installation

You should have easy_install (from setuptools or something equivalent) installed on your system.

To install the package, just type:

$ easy_install yaak.inject

You can also install the package from a source tarball. Decompress the source archive and type:

$ python setup.py install

Support

This project is hosted on bitbucket.org. Please report issues via the bug tracker.

The package documentation can be found here.

Automated tests are run over the mercurial repository regularly. Build results can be found here.

Getting Started

The yaak.inject module implements dependency injection. Here is a tutorial that explains how to use this module.

First, import the yaak.inject module so that you can use the injection functionality in your application:

>>> from yaak import inject

Create a class whose instances have to be injected a feature identified by the string IService (but could be any hashable type, such as a class):

>>> class Client(object):
...   service = inject.Attr('IService')  # inject a feature as an attribute
...   def use_service(self):
...     self.service.do_something()  # use the injected feature
...

Also, create a class (or any callable) that implements the feature:

>>> class Service(object):
...   def do_something(self):
...     print "Service: I'm working hard"
...

Then, when you configure your application, you need to wire an implementation for each feature. In this case, we provide an implementation for the IService feature:

>>> inject.provide('IService', Service)

Note that we provide a factory (class) for the feature and not the instance itself. You’ll see later why.

Now, a Client instance can use the service:

>>> client = Client()
>>> client.use_service()
Service: I'm working hard

When you use the default provide() behavior, all instances of the Client class will be injected the same Service instance:

>>> another_client = Client()
>>> client.service is another_client.service
True

In fact, the default behavior when you provide() a feature is to create a thread-local singleton that is injected in all instances that request the feature. That’s what we call the scope: it defines the lifespan of the feature instance.

You may want a different IService instance for each Client. You can do that by changing the default scope to Scope.Transient when you provide the feature:

>>> inject.provide('IService', Service, scope=inject.Scope.Transient)

Then, a different Service instance is injected in each new Client instance:

>>> client = Client()
>>> another_client = Client()
>>> client.service is another_client.service
False

You can also declare injected features as function/method parameters instead of attributes:

>>> class Client(object):
...   @inject.Param(service='IService')
...   def __init__(self, text, service):
...     self.text = text
...     self.service = service
...   def use_service(self):
...     print self.text
...     self.service.do_something()
...

Then you could use the Client class and get the parameters injected automatically if you don’t provide a value for them:

>>> client = Client('This is a text')
>>> client.use_service()
This is a text
Service: I'm working hard

That’s the easiest way to declare injected parameters. But if you want to keep your class decoupled from the injection framework, you can also define the injection afterwards:

>>> class Client(object):
...   def __init__(self, text, service):
...     self.text = text
...     self.service = service
...   def use_service(self):
...     print self.text
...     self.service.do_something()
...
>>> inject_service = inject.Param(service='IService')
>>> InjectedClient = inject_service(Client)
>>> client = InjectedClient('This is a text')
>>> client.use_service()
This is a text
Service: I'm working hard

Changelog

0.2.1 (11-March-2012)

  • The setup.py file does not import code anymore in order to retrieve the version information, since it may cause some installation problems
  • Fixed bad years in the changelog, and reordered the items so that the most recent changes appear first
  • Changed the aliases for releasing new versions
  • Fixed line endings (unix style)
  • Removed the extensions of the text files since it’s a convention in the Python world.

0.2.0 (24-Oct-2011)

  • Fixed the broken lock acquire/release implementation when updating the application context dictionary.
  • The locking mechanism is now available for all scopes.
  • The context manager is now responsible for updating the context dictionaries.
  • Fixed duplicate factory calls when providing a factory returning None
  • ScopeManager.enter_scope now raise a ScopeReenterError when re-entering a scope
  • ScopeManager.exit_scope now raise a UndefinedScopeError when exiting an undeclared scope
  • Fixed the API documentation

0.1.0 (23-Oct-2011)

  • Initial release

MIT License

Copyright (c) 2011-2012 Sylvain Prat

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Indices and tables