From ea399bb64cc8fa1c50f6bfbf731e0f002c64f5e5 Mon Sep 17 00:00:00 2001 From: Tornado Tech <54727692+Tornado-Technology@users.noreply.github.com> Date: Thu, 15 Aug 2024 14:39:36 +1000 Subject: [PATCH] Updated dependecies manager --- .../DependenciesContainer.cs | 12 ++-- Hypercube.Dependencies/DependencyManager.cs | 64 +++++++++++++++---- 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/Hypercube.Dependencies/DependenciesContainer.cs b/Hypercube.Dependencies/DependenciesContainer.cs index c211921..afde00d 100644 --- a/Hypercube.Dependencies/DependenciesContainer.cs +++ b/Hypercube.Dependencies/DependenciesContainer.cs @@ -40,17 +40,17 @@ public void Register() { Register(typeof(T)); } - - public void Register() - { - Register(typeof(TType), typeof(TImpl)); - } public void Register(Type type) { Register(type, type); } - + + public void Register() + { + Register(typeof(TType), typeof(TImpl)); + } + public void Register(Type type, Type impl) { object DefaultFactory(DependenciesContainer container) diff --git a/Hypercube.Dependencies/DependencyManager.cs b/Hypercube.Dependencies/DependencyManager.cs index d4d3753..424305c 100644 --- a/Hypercube.Dependencies/DependencyManager.cs +++ b/Hypercube.Dependencies/DependencyManager.cs @@ -18,56 +18,98 @@ namespace Hypercube.Dependencies; public static class DependencyManager { private static readonly ThreadLocal Container = new(); - + public static void InitThread(DependenciesContainer collection, bool replaceExisting = false) { if (Container.IsValueCreated && !replaceExisting) throw new InvalidOperationException(); - + Container.Value = collection; } - + public static DependenciesContainer InitThread() { if (Container.IsValueCreated) return Container.Value!; - + var deps = new DependenciesContainer(); Container.Value = deps; - + return deps; } + public static void Register() + { + Debug.Assert(Container.IsValueCreated); + Container.Value!.Register(); + } + + public static void Register(Type type) + { + Debug.Assert(Container.IsValueCreated); + Container.Value!.Register(type); + } + public static void Register() { Debug.Assert(Container.IsValueCreated); Container.Value!.Register(); } - - public static void Register() + + public static void Register(Type type, Type impl) { Debug.Assert(Container.IsValueCreated); - Container.Value!.Register(); + Container.Value!.Register(type, impl); } - + public static void Register(T instance) { Debug.Assert(Container.IsValueCreated); Container.Value!.Register(instance); } - + + public static void Register(Type type, object instance) + { + Debug.Assert(Container.IsValueCreated); + Container.Value!.Register(type, instance); + } + public static void Register(Func factory) { Debug.Assert(Container.IsValueCreated); Container.Value!.Register(factory); } - + public static T Resolve() { Debug.Assert(Container.IsValueCreated); return Container.Value!.Resolve(); } + public static object Resolve(Type type) + { + Debug.Assert(Container.IsValueCreated); + return Container.Value!.Resolve(type); + } + + public static object Instantiate() + { + Debug.Assert(Container.IsValueCreated); + return Container.Value!.Instantiate(); + } + + public static object Instantiate(Type type) + { + Debug.Assert(Container.IsValueCreated); + return Container.Value!.Instantiate(type); + } + + public static void InstantiateAll() + { + Debug.Assert(Container.IsValueCreated); + Container.Value!.InstantiateAll(); + } + public static void Inject(object instance) { Debug.Assert(Container.IsValueCreated);