Skip to content

Commit 44d5c43

Browse files
committed
Add requester type to DI not-found exception
1 parent 0662d4e commit 44d5c43

4 files changed

Lines changed: 62 additions & 8 deletions

File tree

RedCatEngineUnityProject/Packages/DependencyInjection/Containers/ApplicationContainer.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,21 @@ params object[] context
205205
context) && type.IsInstanceOfType(createdInstance))
206206
return createdInstance;
207207

208-
throw new NotFoundInstanceOrCreateException(type);
208+
throw new NotFoundInstanceOrCreateException(type, TryGetRequesterTypeFromContext(context));
209+
}
210+
211+
private static Type TryGetRequesterTypeFromContext(object[] context)
212+
{
213+
foreach (var contextParameter in context)
214+
{
215+
if (contextParameter is not KeyValuePair<string, Type> requesterContext ||
216+
requesterContext.Key != Injector.RequesterTypeContextKey)
217+
continue;
218+
219+
return requesterContext.Value;
220+
}
221+
222+
return null;
209223
}
210224

211225
public TInstanceBindType BindDummy<TInstanceBindType, TDummyType>(params object[] context)
@@ -318,4 +332,4 @@ public void Dispose()
318332
_isDisposed = true;
319333
}
320334
}
321-
}
335+
}

RedCatEngineUnityProject/Packages/DependencyInjection/Exceptions/NotFoundInstanceOrCreateException.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,21 @@ namespace RedCatEngine.DependencyInjection.Exceptions
55
public class NotFoundInstanceOrCreateException : Exception
66
{
77
public readonly Type NotFoundType;
8+
public readonly Type RequesterType;
89
private const string ErrorMessageFormat = "Not found instances or create for Type {0}";
10+
private const string ErrorMessageWithRequesterFormat =
11+
"Not found instances or create for Type {0}. Requested by Type {1}";
912

10-
public NotFoundInstanceOrCreateException(Type notFoundType)
11-
: base(string.Format(ErrorMessageFormat, notFoundType))
13+
public NotFoundInstanceOrCreateException(Type notFoundType, Type requesterType = null)
14+
: base(string.Format(
15+
requesterType == null
16+
? ErrorMessageFormat
17+
: ErrorMessageWithRequesterFormat,
18+
notFoundType,
19+
requesterType))
1220
{
1321
NotFoundType = notFoundType;
22+
RequesterType = requesterType;
1423
}
1524
}
16-
}
25+
}

RedCatEngineUnityProject/Packages/DependencyInjection/Specials/Injector.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace RedCatEngine.DependencyInjection.Specials
1010
{
1111
public class Injector
1212
{
13+
public const string RequesterTypeContextKey = "__requester_type_context__";
14+
1315
private readonly IGetterApplicationContainer _getter;
1416
private readonly ProviderService _providerService;
1517

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

5658
private object[] GetParametersForMethod(MethodBase method, object[] context)
5759
{
60+
var parameterContext = new object[context.Length + 1];
61+
Array.Copy(context, parameterContext, context.Length);
62+
parameterContext[context.Length] = new KeyValuePair<string, Type>(
63+
RequesterTypeContextKey,
64+
method.DeclaringType);
65+
5866
var parameters = new List<object>();
5967

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

78-
parameters.Add(_getter.GetSingle(parameterInfo.ParameterType, context));
86+
parameters.Add(_getter.GetSingle(parameterInfo.ParameterType, parameterContext));
7987
}
8088
return parameters.ToArray();
8189
}
8290
}
83-
}
91+
}

RedCatEngineUnityProject/Packages/DependencyInjection/Tests/ExceptionDiContainerTests.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,29 @@ public void GivenApplicationContainer_WhenGetAllNotContainInstance_ThenCatchNotF
4848
Assert.Fail("Not catch error");
4949
}
5050

51+
[Test]
52+
public void GivenApplicationContainer_WhenInjectNotContainInstance_ThenCatchRequesterTypeInException()
53+
{
54+
var applicationContainer = new ApplicationContainer();
55+
try
56+
{
57+
applicationContainer.GetSingle<SimpleInjectedDemoParentClass>();
58+
}
59+
catch (Exception exception)
60+
{
61+
Assert.IsTrue(exception is NotFoundInstanceOrCreateException, "Incorrect error");
62+
Assert.IsTrue(
63+
((NotFoundInstanceOrCreateException)exception).NotFoundType == typeof(SimpleDemoFirstDataChildClass),
64+
"Incorrect not found type");
65+
Assert.IsTrue(
66+
((NotFoundInstanceOrCreateException)exception).RequesterType == typeof(SimpleInjectedDemoParentClass),
67+
"Incorrect requester type");
68+
return;
69+
}
70+
71+
Assert.Fail("Not catch error");
72+
}
73+
5174
[Test]
5275
public void GivenApplicationContainer_WhenTryCreateInterface_ThenCatchNotCorrectType()
5376
{
@@ -113,4 +136,4 @@ public void GivenApplicationContainer_WhenBindManyConstructorType_ThenCatchExcep
113136
Assert.Fail("Not catch error");
114137
}
115138
}
116-
}
139+
}

0 commit comments

Comments
 (0)