Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,21 @@ params object[] context
context) && type.IsInstanceOfType(createdInstance))
return createdInstance;

throw new NotFoundInstanceOrCreateException(type);
throw new NotFoundInstanceOrCreateException(type, TryGetRequesterTypeFromContext(context));
}

private static Type TryGetRequesterTypeFromContext(object[] context)
{
foreach (var contextParameter in context)
{
if (contextParameter is not KeyValuePair<string, Type> requesterContext ||
requesterContext.Key != Injector.RequesterTypeContextKey)
continue;

return requesterContext.Value;
}

return null;
}

public TInstanceBindType BindDummy<TInstanceBindType, TDummyType>(params object[] context)
Expand Down Expand Up @@ -318,4 +332,4 @@ public void Dispose()
_isDisposed = true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ namespace RedCatEngine.DependencyInjection.Exceptions
public class NotFoundInstanceOrCreateException : Exception
{
public readonly Type NotFoundType;
public readonly Type RequesterType;
private const string ErrorMessageFormat = "Not found instances or create for Type {0}";
private const string ErrorMessageWithRequesterFormat =
"Not found instances or create for Type {0}. Requested by Type {1}";

public NotFoundInstanceOrCreateException(Type notFoundType)
: base(string.Format(ErrorMessageFormat, notFoundType))
public NotFoundInstanceOrCreateException(Type notFoundType, Type requesterType = null)
: base(string.Format(
requesterType == null
? ErrorMessageFormat
: ErrorMessageWithRequesterFormat,
notFoundType,
requesterType))
{
NotFoundType = notFoundType;
RequesterType = requesterType;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace RedCatEngine.DependencyInjection.Specials
{
public class Injector
{
public const string RequesterTypeContextKey = "__requester_type_context__";

private readonly IGetterApplicationContainer _getter;
private readonly ProviderService _providerService;

Expand Down Expand Up @@ -55,6 +57,12 @@ public void InjectContextToMethodsWithAttribute<TAttribute>(object objectToInjec

private object[] GetParametersForMethod(MethodBase method, object[] context)
{
var parameterContext = new object[context.Length + 1];
Array.Copy(context, parameterContext, context.Length);
parameterContext[context.Length] = new KeyValuePair<string, Type>(
RequesterTypeContextKey,
method.DeclaringType);

var parameters = new List<object>();

foreach (var parameterInfo in method.GetParameters())
Expand All @@ -75,9 +83,9 @@ private object[] GetParametersForMethod(MethodBase method, object[] context)
continue;
}

parameters.Add(_getter.GetSingle(parameterInfo.ParameterType, context));
parameters.Add(_getter.GetSingle(parameterInfo.ParameterType, parameterContext));
}
return parameters.ToArray();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,29 @@
Assert.Fail("Not catch error");
}

[Test]
public void GivenApplicationContainer_WhenInjectNotContainInstance_ThenCatchRequesterTypeInException()
{
var applicationContainer = new ApplicationContainer();
try
{
applicationContainer.GetSingle<SimpleInjectedDemoParentClass>();
}
catch (Exception exception)
{
Assert.IsTrue(exception is NotFoundInstanceOrCreateException, "Incorrect error");

Check failure on line 61 in RedCatEngineUnityProject/Packages/DependencyInjection/Tests/ExceptionDiContainerTests.cs

View workflow job for this annotation

GitHub Actions / DependencyInjection test results

RedCatEngine.DependencyInjection.Tests.ExceptionDiContainerTests.GivenApplicationContainer_WhenInjectNotContainInstance_ThenCatchRequesterTypeInException

Incorrect error Expected: True But was: False
Raw output
at RedCatEngine.DependencyInjection.Tests.ExceptionDiContainerTests.GivenApplicationContainer_WhenInjectNotContainInstance_ThenCatchRequesterTypeInException () [0x00019] in /github/workspace/RedCatEngineUnityProject/Packages/DependencyInjection/Tests/ExceptionDiContainerTests.cs:61
Assert.IsTrue(
((NotFoundInstanceOrCreateException)exception).NotFoundType == typeof(SimpleDemoFirstDataChildClass),
"Incorrect not found type");
Assert.IsTrue(
((NotFoundInstanceOrCreateException)exception).RequesterType == typeof(SimpleInjectedDemoParentClass),
"Incorrect requester type");
return;
}

Assert.Fail("Not catch error");
}

[Test]
public void GivenApplicationContainer_WhenTryCreateInterface_ThenCatchNotCorrectType()
{
Expand Down Expand Up @@ -113,4 +136,4 @@
Assert.Fail("Not catch error");
}
}
}
}
Loading