Add serviceContainer.py

main
ado 2023-10-10 09:29:04 -04:00
commit f0a9ce6846
1 changed files with 19 additions and 0 deletions

19
serviceContainer.py Normal file
View File

@ -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