From b4cf096e967e5b5e3a05bb639b7289600659ea01 Mon Sep 17 00:00:00 2001 From: Simon G Date: Thu, 9 Dec 2021 09:45:00 +0100 Subject: [PATCH] #51: add comments --- LightweightIocContainer/IocContainer.cs | 88 +++++++++++++++++-- .../InternalToBeResolvedPlaceholder.cs | 7 ++ 2 files changed, 90 insertions(+), 5 deletions(-) diff --git a/LightweightIocContainer/IocContainer.cs b/LightweightIocContainer/IocContainer.cs index 5660186..79127fc 100644 --- a/LightweightIocContainer/IocContainer.cs +++ b/LightweightIocContainer/IocContainer.cs @@ -269,6 +269,12 @@ namespace LightweightIocContainer internal object Resolve(Type type, object[] arguments, List resolveStack) => GenericMethodCaller.CallPrivate(this, nameof(ResolveInternal), type, arguments, resolveStack); + /// + /// Recursively resolve a with the given parameters for an + /// + /// The that includes the type and resolve stack + /// The current resolve stack + /// A recursively resolved instance of the given private object Resolve(InternalToBeResolvedPlaceholder toBeResolvedPlaceholder, List resolveStack) { if (toBeResolvedPlaceholder.Parameters == null) @@ -339,6 +345,11 @@ namespace LightweightIocContainer return newInstance; } + /// + /// Try to get an existing singleton instance for a given + /// + /// The given + /// A singleton instance if existing for the given , null if not private object TryGetSingletonInstance(Type type) => _singletons.FirstOrDefault(s => s.type == type).instance; //if a singleton instance exists return it /// @@ -512,6 +523,17 @@ namespace LightweightIocContainer return null; } + /// + /// Try to get the resolve stack for a given + /// + /// The given + /// The given arguments + /// The current resolve stack + /// + /// result: True if successful, false if not + /// parameters: The parameters needed to resolve the given + /// exception: A if no matching constructor was found + /// private (bool result, List parameters, NoMatchingConstructorFoundException exception) TryGetTypeResolveStack(Type type, object[] arguments, List resolveStack) { NoMatchingConstructorFoundException noMatchingConstructorFoundException = null; @@ -533,13 +555,24 @@ namespace LightweightIocContainer return (false, null, noMatchingConstructorFoundException); } - private (bool result, List parameters, List constructorNotMatchingExceptions) TryGetConstructorResolveStack(ConstructorInfo constructor, object[] arguments, List resolveStack) + /// + /// Try to get the resolve stack for a given constructor + /// + /// The for the given constructor + /// The given arguments + /// The current resolve stack + /// + /// result: True if successful, false if not + /// parameters: The parameters needed to resolve the given + /// exception: A List of s if the constructor is not matching + /// + private (bool result, List parameters, List exceptions) TryGetConstructorResolveStack(ConstructorInfo constructor, object[] arguments, List resolveStack) { List constructorParameters = constructor.GetParameters().ToList(); if (!constructorParameters.Any()) return (true, null, null); - List constructorNotMatchingExceptions = new(); + List exceptions = new(); List parameters = new(); List passedArguments = null; @@ -584,12 +617,12 @@ namespace LightweightIocContainer if (result) fittingArgument = new InternalToBeResolvedPlaceholder(registeredType, parametersToResolve); else - constructorNotMatchingExceptions.Add(new ConstructorNotMatchingException(constructor, exception)); + exceptions.Add(new ConstructorNotMatchingException(constructor, exception)); } } catch (Exception exception) { - constructorNotMatchingExceptions.Add(new ConstructorNotMatchingException(constructor, exception)); + exceptions.Add(new ConstructorNotMatchingException(constructor, exception)); } if (fittingArgument is InternalResolvePlaceholder && passedArguments != null) @@ -607,12 +640,22 @@ namespace LightweightIocContainer parameters.Add(fittingArgument); } - return (!parameters.Any(p => p is InternalResolvePlaceholder), parameters, constructorNotMatchingExceptions); + return (!parameters.Any(p => p is InternalResolvePlaceholder), parameters, exceptions); } + /// + /// Find the for the given + /// + /// The given + /// The for the given [CanBeNull] private IRegistration FindRegistration() => FindRegistration(typeof(T)); + /// + /// Find the for the given + /// + /// The given + /// The for the given [CanBeNull] private IRegistration FindRegistration(Type type) { @@ -632,6 +675,12 @@ namespace LightweightIocContainer return !openGenericRegistrations.Any() ? null : openGenericRegistrations.FirstOrDefault(r => r.InterfaceType == type.GetGenericTypeDefinition()); } + /// + /// Try to get the sorted constructors for the given + /// + /// The given + /// A list of sorted for the given + /// No public constructor was found for the given private List TryGetSortedConstructors(Type type) { List sortedConstructors = type.GetConstructors().OrderByDescending(c => c.GetParameters().Length).ToList(); @@ -641,6 +690,13 @@ namespace LightweightIocContainer return sortedConstructors; } + /// + /// Get the implementation type for the given + /// + /// The given + /// The given of the interface + /// The implementation for the given + /// Unknown passed private Type GetType(IRegistration registration) => registration switch { @@ -649,9 +705,31 @@ namespace LightweightIocContainer _ => throw new UnknownRegistrationException($"Unknown registration used: {registration.GetType().Name}.") }; + /// + /// Non generic method to get the implementation type for the given + /// + /// The given of the interface + /// The given + /// The implementation for the given + /// Unknown passed private Type GetTypeNonGeneric(Type type, IRegistration registration) => (Type) GenericMethodCaller.CallPrivate(this, nameof(GetType), type, registration); + /// + /// Check the given resolve stack for circular dependencies + /// + /// The given resolve stack + /// The given + /// The new resolve stack + /// A circular dependency was detected private List CheckForCircularDependencies(List resolveStack) => CheckForCircularDependencies(typeof(T), resolveStack); + + /// + /// Check the given resolve stack for circular dependencies + /// + /// The given + /// The given resolve stack + /// The new resolve stack + /// A circular dependency was detected private List CheckForCircularDependencies(Type type, List resolveStack) { if (resolveStack == null) //first resolve call diff --git a/LightweightIocContainer/ResolvePlaceholders/InternalToBeResolvedPlaceholder.cs b/LightweightIocContainer/ResolvePlaceholders/InternalToBeResolvedPlaceholder.cs index 73d819a..373dbd4 100644 --- a/LightweightIocContainer/ResolvePlaceholders/InternalToBeResolvedPlaceholder.cs +++ b/LightweightIocContainer/ResolvePlaceholders/InternalToBeResolvedPlaceholder.cs @@ -18,7 +18,14 @@ namespace LightweightIocContainer.ResolvePlaceholders Parameters = parameters; } + /// + /// The to be resolved + /// public Type ResolvedType { get; } + + /// + /// The parameters needed to resolve the + /// public List Parameters { get; } } } \ No newline at end of file