Skip to content

Alias Provider

Used to "alias" another existing dependency.

Simple Usage
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import typing as t
import uzi

_Ta = t.TypeVar('_Ta') 
_Tb = t.TypeVar('_Tb') 

container = uzi.Container()

# a) using the helper method
container.alias(_Tb, _Ta)
# or 
# b) manually creating and attaching the provider
container[_Ta] = uzi.providers.Alias(_Ta)

obj = object()
# bind `_Ta` to a constant `obj`
container.value(_Ta, obj) 

if __name__ == '__main__':
    injector = uzi.Injector(uzi.DepGraph(container))

    assert obj is injector.make(_Ta) is injector.make(_Tb)

In the above snippet, dependents of both _Tb and _Ta will be provided with obj.

Use Case

import typing as t
from uzi import Container, providers

_Ta = t.TypeVar('_Ta') 
_Tb = t.TypeVar('_Tb') 


class Cache:
    ...

class DbCache(Cache):
    ...

class MemoryCache(Cache):
    ...

class RedisCache(Cache):
    ...


container = Container()

container.singleton(DbCache)
container.singleton(RedisCache)
container.singleton(MemoryCache)

container.alias(Cache, RedisCache)
Back to top