Skip to content

Commit

Permalink
Improved ResolveAll for Array Parameters (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
tariksavas authored Jul 24, 2024
1 parent 6b3b0d1 commit edde86a
Show file tree
Hide file tree
Showing 36 changed files with 945 additions and 13 deletions.
26 changes: 26 additions & 0 deletions Runtime/Core/Context/DIContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,32 @@ public T Resolve<T>(Type type = null, string id = "") where T : class
return bindedObject.CreatedObject as T;
}

public T[] ResolveAll<T>(Type type = null, string id = "") where T : class
{
type ??= typeof(T);

Predicate<BindedObject> predicate = string.IsNullOrEmpty(id)
? x => x.Type == type || x.InterfaceType == type
: x => x.Id == id && (x.Type == type || x.InterfaceType == type);

OptimizedList<BindedObject> bindedObjects = _bindedObjects.FindAll(predicate);
if (bindedObjects == null || bindedObjects.Count == 0)
{
return _parentDIContainer?.ResolveAll<T>(type, id);
}

T[] createdObjects = new T[bindedObjects.Count];

for (int index = 0; index < bindedObjects.Count; index++)
{
TryCreateBindedObject(bindedObjects[index]);

createdObjects[index] = bindedObjects[index].CreatedObject as T;
}

return createdObjects;
}

private void TryCreateBindedObject(BindedObject bindedObject)
{
if (bindedObject.CreatedObject != null || !bindedObject.Type.TryGetDependencies(out object[] objects, this))
Expand Down
38 changes: 31 additions & 7 deletions Runtime/Utility/InjectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,40 @@ public static bool TryGetDependencies(this Type type, out object[] objects, DICo
Type parameterType = parameter.ParameterType;

InjectAttribute injectAttribute = parameter.GetCustomAttribute<InjectAttribute>();

object parameterObject = container.Resolve<object>(parameterType, injectAttribute?.Id);
if (parameterObject == null)

if (!parameterType.IsArray)
{
Debug.LogError($"Can not resolve depenceny! Type: {type} --- DependencyType: {parameterType}");
return false;
object parameterObject = container.Resolve<object>(parameterType, injectAttribute?.Id);
if (parameterObject == null)
{
Debug.LogError($"Can not resolve depenceny! Type: {type} --- DependencyType: {parameterType}");
return false;
}

Array.Resize(ref objects, objects.Length + 1);
objects[^1] = parameterObject;
}
else
{
Type elementType = parameterType.GetElementType();

object[] parameterObjects = container.ResolveAll<object>(elementType, injectAttribute?.Id);
if (parameterObjects == null)
{
Debug.LogError($"Can not resolve depenceny! Type: {type} --- DependencyType: {elementType}");
return false;
}

Array.Resize(ref objects, objects.Length + 1);
objects[^1] = parameterObject;
Array parameterArray = Array.CreateInstance(elementType, parameterObjects.Length);

for (int index = 0; index < parameterObjects.Length; index++)
{
parameterArray.SetValue(parameterObjects[index], index);
}

Array.Resize(ref objects, objects.Length + 1);
objects[^1] = parameterArray;
}
}

return true;
Expand Down
Loading

0 comments on commit edde86a

Please sign in to comment.