Add serviceContainer.py
commit
f0a9ce6846
|
@ -0,0 +1,19 @@
|
|||
from dataclasses import dataclass
|
||||
from typing import Mapping, Type, TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
@dataclass
|
||||
class ServiceContainer:
|
||||
_dependencies: Mapping[str, T] = {}
|
||||
|
||||
def register(self, name: str, instance: T) -> None:
|
||||
self._dependencies[name] = instance
|
||||
|
||||
def retrieve(self, name: str, _: Type[T]) -> T:
|
||||
return self._dependencies[name]
|
||||
|
||||
|
||||
|
||||
# in python 3.12 we can use retrieve[T] :)
|
||||
# https://docs.python.org/3/reference/compound_stmts.html#type-params
|
Loading…
Reference in New Issue