diff --git a/RedCatEngineUnityProject/Packages/DependencyInjection/Containers/ApplicationContainer.cs b/RedCatEngineUnityProject/Packages/DependencyInjection/Containers/ApplicationContainer.cs index 7fc7a00..2ac1e0c 100644 --- a/RedCatEngineUnityProject/Packages/DependencyInjection/Containers/ApplicationContainer.cs +++ b/RedCatEngineUnityProject/Packages/DependencyInjection/Containers/ApplicationContainer.cs @@ -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 requesterContext || + requesterContext.Key != Injector.RequesterTypeContextKey) + continue; + + return requesterContext.Value; + } + + return null; } public TInstanceBindType BindDummy(params object[] context) @@ -318,4 +332,4 @@ public void Dispose() _isDisposed = true; } } -} \ No newline at end of file +} diff --git a/RedCatEngineUnityProject/Packages/DependencyInjection/Exceptions/NotFoundInstanceOrCreateException.cs b/RedCatEngineUnityProject/Packages/DependencyInjection/Exceptions/NotFoundInstanceOrCreateException.cs index 2a868c0..88966e1 100644 --- a/RedCatEngineUnityProject/Packages/DependencyInjection/Exceptions/NotFoundInstanceOrCreateException.cs +++ b/RedCatEngineUnityProject/Packages/DependencyInjection/Exceptions/NotFoundInstanceOrCreateException.cs @@ -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; } } -} \ No newline at end of file +} diff --git a/RedCatEngineUnityProject/Packages/DependencyInjection/Specials/Injector.cs b/RedCatEngineUnityProject/Packages/DependencyInjection/Specials/Injector.cs index 34c8f7f..d0e1e67 100644 --- a/RedCatEngineUnityProject/Packages/DependencyInjection/Specials/Injector.cs +++ b/RedCatEngineUnityProject/Packages/DependencyInjection/Specials/Injector.cs @@ -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; @@ -55,6 +57,12 @@ public void InjectContextToMethodsWithAttribute(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( + RequesterTypeContextKey, + method.DeclaringType); + var parameters = new List(); foreach (var parameterInfo in method.GetParameters()) @@ -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(); } } -} \ No newline at end of file +} diff --git a/RedCatEngineUnityProject/Packages/DependencyInjection/Tests/ExceptionDiContainerTests.cs b/RedCatEngineUnityProject/Packages/DependencyInjection/Tests/ExceptionDiContainerTests.cs index 066d9fc..455cfed 100644 --- a/RedCatEngineUnityProject/Packages/DependencyInjection/Tests/ExceptionDiContainerTests.cs +++ b/RedCatEngineUnityProject/Packages/DependencyInjection/Tests/ExceptionDiContainerTests.cs @@ -48,6 +48,29 @@ public void GivenApplicationContainer_WhenGetAllNotContainInstance_ThenCatchNotF Assert.Fail("Not catch error"); } + [Test] + public void GivenApplicationContainer_WhenInjectNotContainInstance_ThenCatchRequesterTypeInException() + { + var applicationContainer = new ApplicationContainer(); + try + { + applicationContainer.GetSingle(); + } + catch (Exception exception) + { + Assert.IsTrue(exception is NotFoundInstanceOrCreateException, "Incorrect error"); + 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() { @@ -113,4 +136,4 @@ public void GivenApplicationContainer_WhenBindManyConstructorType_ThenCatchExcep Assert.Fail("Not catch error"); } } -} \ No newline at end of file +}