From e90c70967679f1880fae08322d3da4257ea024d2 Mon Sep 17 00:00:00 2001 From: ado Date: Tue, 10 Oct 2023 10:14:16 -0400 Subject: [PATCH] Add serviceContainer_factory.py --- serviceContainer_factory.py | 51 +++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 serviceContainer_factory.py diff --git a/serviceContainer_factory.py b/serviceContainer_factory.py new file mode 100644 index 0000000..e224e3d --- /dev/null +++ b/serviceContainer_factory.py @@ -0,0 +1,51 @@ +from dataclasses import dataclass +from types import LambdaType, FunctionType +from typing import Mapping, Type, TypeVar + +T = TypeVar("T") + +class ServiceLookupException(Exception): + pass + +@dataclass +class ServiceContainer: + _singletons: Mapping[str, T] + _factories: Mapping[str, T] + + def register(self, name: str, instance: T) -> None: + if type(instance) is LambdaType or type(instance) is FunctionType: + self._factories[name] = instance + else: + self._singletons[name] = instance + + def retrieve(self, name: str, _: Type[T]) -> T: + if name in self._singletons: + return self._singletons[name] + elif name in self._factories: + return self._factories[name]() + + raise ServiceLookupException(f"Could not locate service with name {name}") + + +@dataclass +class Foo: + bar: str + + def __post_init__(self): + print(f"Initialized with {self.bar}") + + +container = ServiceContainer({}, {}) +container.register("foo_lam", lambda : Foo("bar")) +foo_single = Foo("baz") +container.register("foo_single", foo_single) + +mc1 = container.retrieve("foo_lam", Type[Foo]) +mc2 = container.retrieve("foo_lam", Type[Foo]) +mc3 = container.retrieve("foo_single", Type[Foo]) +mc4 = container.retrieve("foo_single", Type[Foo]) + +print(mc1.bar) +print(mc2.bar) +print(mc3.bar) +print(mc4.bar) \ No newline at end of file