Skip to content

Commit

Permalink
TryRsolve
Browse files Browse the repository at this point in the history
  • Loading branch information
AlonTalmi authored and hadashiA committed Apr 4, 2024
1 parent f2afd2a commit 11efdf6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 11 deletions.
51 changes: 48 additions & 3 deletions VContainer/Assets/VContainer/Runtime/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ public interface IObjectResolver : IDisposable
/// This version of resolve looks for all of scopes
/// </remarks>
object Resolve(Type type);

/// <summary>
/// Try resolve from type
/// </summary>
/// <remarks>
/// This version of resolve looks for all of scopes
/// </remarks>
/// <returns>Successfully resolved</returns>
bool TryResolve(Type type, out object resolved);

/// <summary>
/// Resolve from meta with registration
Expand Down Expand Up @@ -75,6 +84,18 @@ internal ScopedContainer(
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object Resolve(Type type) => Resolve(FindRegistration(type));

public bool TryResolve(Type type, out object resolved)
{
if (TryFindRegistration(type, out var registration))
{
resolved = Resolve(registration);
return true;
}

resolved = default;
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object Resolve(Registration registration)
{
Expand Down Expand Up @@ -155,17 +176,28 @@ object CreateTrackedInstance(Registration registration)

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal Registration FindRegistration(Type type)
{
if (TryFindRegistration(type, out var registration))
return registration;

throw new VContainerException(type, $"No such registration of type: {type}");
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal bool TryFindRegistration(Type type, out Registration registration)
{
IScopedObjectResolver scope = this;
while (scope != null)
{
if (scope.TryGetRegistration(type, out var registration))
if (scope.TryGetRegistration(type, out registration))
{
return registration;
return true;
}
scope = scope.Parent;
}
throw new VContainerException(type, $"No such registration of type: {type}");

registration = default;
return false;
}
}

Expand Down Expand Up @@ -203,6 +235,19 @@ public object Resolve(Type type)
throw new VContainerException(type, $"No such registration of type: {type}");
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryResolve(Type type, out object resolved)
{
if (registry.TryGet(type, out var registration))
{
resolved = Resolve(registration);
return true;
}

resolved = default;
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object Resolve(Registration registration)
{
Expand Down
24 changes: 24 additions & 0 deletions VContainer/Assets/VContainer/Runtime/IObjectResolverExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,30 @@ public static class IObjectResolverExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Resolve<T>(this IObjectResolver resolver) => (T)resolver.Resolve(typeof(T));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryResolve<T>(this IObjectResolver resolver, out T resolved)
{
if (resolver.TryResolve(typeof(T), out var r))
{
resolved = (T)r;
return true;
}

resolved = default;
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T ResolveOrDefault<T>(this IObjectResolver resolver, T defaultValue = default)
{
if (resolver.TryResolve(typeof(T), out var value))
{
return (T)value;
}

return defaultValue;
}

// Using from CodeGen
[Preserve]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,7 @@ public void Dispatch()
{
PlayerLoopHelper.EnsureInitialized();

EntryPointExceptionHandler exceptionHandler = null;
try
{
exceptionHandler = container.Resolve<EntryPointExceptionHandler>();
}
catch (VContainerException ex) when (ex.InvalidType == typeof(EntryPointExceptionHandler))
{
}
EntryPointExceptionHandler exceptionHandler = container.ResolveOrDefault<EntryPointExceptionHandler>();

var initializables = container.Resolve<ContainerLocal<IReadOnlyList<IInitializable>>>().Value;
for (var i = 0; i < initializables.Count; i++)
Expand Down

0 comments on commit 11efdf6

Please sign in to comment.