Skip to content

Commit

Permalink
provide base settings for ioc for easy use
Browse files Browse the repository at this point in the history
  • Loading branch information
JerryImMouse committed Jun 14, 2024
1 parent 6d2540c commit 06427c7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Jerry.Example/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using Jerry.Example.Examples.IoCExample;
using Jerry.Utilities.IoC.Frozen;
using Jerry.Utilities.IoC.General;

namespace Jerry.Example;

public static class Program
Expand Down
15 changes: 15 additions & 0 deletions Jerry.Utilities/IoC/General/IoCManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ public static void InitializeDependencies<TCollection>(IoCSettings settings) whe
if (!_initialized)
Initialize();

// load entry assembly if nothing was provided
if (!Reflection.AssembliesLoaded)
{
var asm = Assembly.GetEntryAssembly();
if (asm == null)
throw new NullReferenceException("No assemblies were cached, IoC has nothing to use...");
Reflection.LoadAssemblies([asm]);
}

// skip validation, settings already validated everything for us
var assemblies = settings.Assemblies;
var mode = settings.Mode;
Expand All @@ -86,6 +95,12 @@ public static void InitializeDependencies<TCollection>(IoCSettings settings) whe
RegisterTypesInternal<TCollection>(types.ToArray());
}

public static void InitializeDependencies<TCollection>() where TCollection : IDependencyCollection
{
var settings = new IoCManagerBuilder().BaseSettings();
InitializeDependencies<TCollection>(settings);
}

private static void RegisterTypesInternal<TCollection>(Type[] types) where TCollection : IDependencyCollection
{
var dict = new Dictionary<Type, object>();
Expand Down
8 changes: 8 additions & 0 deletions Jerry.Utilities/IoC/General/IoCManagerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public IoCSettings Build()
return IoCSettings.Build(_mode, _attribute, _inheritor, _registerInheritor, _assemblies);
}
#endregion

public IoCSettings BaseSettings()
{
return
WithAttribute<RegisterDependencyAttribute>()
.WithMode(IoCMode.Attribute)
.Build();
}
}

public class IoCSettings
Expand Down
2 changes: 1 addition & 1 deletion Jerry.Utilities/Jerry.Utilities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PackageId>JerryImMouse.Project.Utilities</PackageId>
<Version>0.0.5</Version>
<Version>0.0.6</Version>
<Authors>JerryImMouse</Authors>
<Company>JerryImMouse</Company>
</PropertyGroup>
Expand Down
2 changes: 2 additions & 0 deletions Jerry.Utilities/Reflection/ReflectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class ReflectionManager
{
private readonly List<Assembly> _assemblies = new();
private static ReflectionManager? _instance;
public bool AssembliesLoaded = false;

public static ReflectionManager Instance
{
Expand All @@ -27,6 +28,7 @@ public void LoadAssemblies(Assembly[] assemblies)
{
_assemblies.AddRange(assemblies);
EnsureGetAllTypesCache();
AssembliesLoaded = true;
}

public void EnsureGetAllTypesCache()
Expand Down

0 comments on commit 06427c7

Please sign in to comment.