Skip to content

Commit

Permalink
Updated dependecies manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornado-Technology committed Aug 15, 2024
1 parent 8aefdc2 commit ea399bb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 17 deletions.
12 changes: 6 additions & 6 deletions Hypercube.Dependencies/DependenciesContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ public void Register<T>()
{
Register(typeof(T));
}

public void Register<TType, TImpl>()
{
Register(typeof(TType), typeof(TImpl));
}

public void Register(Type type)
{
Register(type, type);
}


public void Register<TType, TImpl>()
{
Register(typeof(TType), typeof(TImpl));
}

public void Register(Type type, Type impl)
{
object DefaultFactory(DependenciesContainer container)
Expand Down
64 changes: 53 additions & 11 deletions Hypercube.Dependencies/DependencyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,98 @@ namespace Hypercube.Dependencies;
public static class DependencyManager
{
private static readonly ThreadLocal<DependenciesContainer> 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<T>()
{
Debug.Assert(Container.IsValueCreated);
Container.Value!.Register<T>();
}

public static void Register(Type type)
{
Debug.Assert(Container.IsValueCreated);
Container.Value!.Register(type);
}

public static void Register<TType, TImplementation>()
{
Debug.Assert(Container.IsValueCreated);
Container.Value!.Register<TType, TImplementation>();
}
public static void Register<T>()

public static void Register(Type type, Type impl)
{
Debug.Assert(Container.IsValueCreated);
Container.Value!.Register<T>();
Container.Value!.Register(type, impl);
}

public static void Register<T>(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<T>(Func<DependenciesContainer, T> factory)
{
Debug.Assert(Container.IsValueCreated);
Container.Value!.Register(factory);
}

public static T Resolve<T>()
{
Debug.Assert(Container.IsValueCreated);
return Container.Value!.Resolve<T>();
}

public static object Resolve(Type type)
{
Debug.Assert(Container.IsValueCreated);
return Container.Value!.Resolve(type);
}

public static object Instantiate<T>()
{
Debug.Assert(Container.IsValueCreated);
return Container.Value!.Instantiate<T>();
}

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);
Expand Down

0 comments on commit ea399bb

Please sign in to comment.