Definitions

Warning

This Document Page Under Construction

Lazy Loading

Lazy loading (also known as asynchronous loading) is a design pattern commonly used in computer programming and mostly in web design and development to defer initialization of an object until the point at which it is needed. It can contribute to efficiency in the program’s operation if properly and appropriately used.

Container loads the definitions you have written and uses them like instructions on how to create objects.

However those objects are only created when/if they are requested from the Container, for example through container.get(…) or when they need to be injected in another object. That means you can have a large amount of definitions, Container will not create all the objects unless asked to.

Definition types

This definition format is the most powerful of all. There are several kind of entries you can define:

Scalars

Scalars are simple Python values:

>>> from mediapills.dependency_injection import Container

>>> di = Container({
...   'database.driver': 'mysql',
...   'database.host': '127.0.0.1',
...   'database.port': 80,
...   'database.auth': False,
...   'database.user': 'root',
... })

>>> di['database.host']

'127.0.0.1'

You can also define object entries by creating them directly:

>>> from mediapills.dependency_injection import Container

>>> di = Container()

>>> di['key'] = 'value'

>>> di['key']

'value'

However this is not recommended as that object will be created for every entry invocation, even if not used (it will not be lazy loaded like explained at this section).

Generic Container Type Objects

Container supports any object that holds an arbitrary number of other objects. Examples of containers include tuple, list, set, dict; these are the built-in containers.

>>> from mediapills.dependency_injection import Container

>>> di = Container()

>>> di['parameters'] = {
...   'database.host': '127.0.0.1',
...   'database.port': '80',
...   'database.user': 'root',
... }

>>> di['parameters']

{'database.host': '127.0.0.1', 'database.port': '80', 'database.user': 'root'}

Factories

Warning

This Page Section Under Construction

Objects

Services are defined by anonymous functions that return an instance of an object:

# define some services
container['session_storage'] = lambda di: (
    SessionStorage('SESSION_ID')
)

container['session'] = lambda di: (
    Session(di['session_storage'])
)

Notice that the anonymous function has access to the current container instance, allowing references to other services or parameters.

As objects are only created when you get them, the order of the definitions does not matter.

Using the defined services is also very easy:

# get the session object
session = injector['session']

# the above call is roughly equivalent to the following code:
# storage = SessionStorage('SESSION_ID')
# session = Session(storage)

Autowired Objects

Warning

This Page Section Under Construction

Aliases

You can alias an entry to another using the Container:

# define arguments container
container['arguments'] = lambda _: sys.argv

# define arguments container alias with name properties
container['properties'] = lambda di: di['arguments']

Allows the interface of an existing location to be used as another name.

Environment Variables

You can get an environment variable’s value using the Container:

>>> container['env'] = lambda _: os.environ

>>> di['env'].get("LANGUAGE")

'en_US'

String Expressions

Warning

This Page Section Under Construction

Wildcards

Warning

This Page Section Under Construction