From 4ab55e8bfc522a616f8f51abebf389a3c143abfb Mon Sep 17 00:00:00 2001 From: Simon G Date: Mon, 6 Dec 2021 13:59:42 +0100 Subject: [PATCH] #52: update visibility for registration properties --- .../Registrations/Fluent/IOnCreate.cs | 2 +- .../Registrations/Fluent/IWithFactory.cs | 13 ++++--- .../Registrations/Fluent/IWithParameters.cs | 19 +++++----- .../Registrations/IRegistrationBase.cs | 2 +- LightweightIocContainer/IocContainer.cs | 14 ++++---- .../LightweightIocContainer.xml | 36 +++++++++++-------- .../Registrations/RegistrationBase.cs | 3 +- .../Registrations/TypedRegistration.cs | 8 ++++- .../Validation/IocValidator.cs | 2 +- .../RegistrationBaseTest.cs | 5 ++- 10 files changed, 61 insertions(+), 43 deletions(-) diff --git a/LightweightIocContainer/Interfaces/Registrations/Fluent/IOnCreate.cs b/LightweightIocContainer/Interfaces/Registrations/Fluent/IOnCreate.cs index 4938507..107e766 100644 --- a/LightweightIocContainer/Interfaces/Registrations/Fluent/IOnCreate.cs +++ b/LightweightIocContainer/Interfaces/Registrations/Fluent/IOnCreate.cs @@ -16,7 +16,7 @@ namespace LightweightIocContainer.Interfaces.Registrations.Fluent /// This is invoked when an instance of this type is created. /// Can be set in the by calling /// - Action OnCreateAction { get; } + internal Action OnCreateAction { get; } } /// diff --git a/LightweightIocContainer/Interfaces/Registrations/Fluent/IWithFactory.cs b/LightweightIocContainer/Interfaces/Registrations/Fluent/IWithFactory.cs index 054f15e..01150a3 100644 --- a/LightweightIocContainer/Interfaces/Registrations/Fluent/IWithFactory.cs +++ b/LightweightIocContainer/Interfaces/Registrations/Fluent/IWithFactory.cs @@ -11,11 +11,6 @@ namespace LightweightIocContainer.Interfaces.Registrations.Fluent /// public interface IWithFactory { - /// - /// The Factory added with the method - /// - ITypedFactory Factory { get; } - /// /// Register an abstract typed factory for the /// @@ -31,4 +26,12 @@ namespace LightweightIocContainer.Interfaces.Registrations.Fluent /// The current instance of this IRegistrationBase WithFactory() where TFactoryImplementation : TFactoryInterface; } + + internal interface IWithFactoryInternal : IWithFactory + { + /// + /// The Factory added with the method + /// + ITypedFactory Factory { get; } + } } \ No newline at end of file diff --git a/LightweightIocContainer/Interfaces/Registrations/Fluent/IWithParameters.cs b/LightweightIocContainer/Interfaces/Registrations/Fluent/IWithParameters.cs index b4a1f1d..7330932 100644 --- a/LightweightIocContainer/Interfaces/Registrations/Fluent/IWithParameters.cs +++ b/LightweightIocContainer/Interfaces/Registrations/Fluent/IWithParameters.cs @@ -13,19 +13,13 @@ namespace LightweightIocContainer.Interfaces.Registrations.Fluent /// public interface IWithParameters { - /// - /// An of parameters that are used to an instance of this - /// Can be set in the by calling - /// - object[] Parameters { get; } - /// /// Pass parameters that will be used to an instance of this /// Parameters set with this method are always inserted at the beginning of the argument list if more parameters are given when resolving /// /// The parameters /// The current instance of this - /// are already set or no parameters given + /// are already set or no parameters given IRegistrationBase WithParameters(params object[] parameters); /// @@ -34,7 +28,16 @@ namespace LightweightIocContainer.Interfaces.Registrations.Fluent /// /// The parameters with their position /// The current instance of this - /// are already set or no parameters given + /// are already set or no parameters given IRegistrationBase WithParameters(params (int index, object parameter)[] parameters); } + + internal interface IWithParametersInternal : IWithParameters + { + /// + /// An of parameters that are used to an instance of this + /// Can be set in the by calling + /// + object[] Parameters { get; } + } } \ No newline at end of file diff --git a/LightweightIocContainer/Interfaces/Registrations/IRegistrationBase.cs b/LightweightIocContainer/Interfaces/Registrations/IRegistrationBase.cs index c655ca9..d729663 100644 --- a/LightweightIocContainer/Interfaces/Registrations/IRegistrationBase.cs +++ b/LightweightIocContainer/Interfaces/Registrations/IRegistrationBase.cs @@ -9,7 +9,7 @@ namespace LightweightIocContainer.Interfaces.Registrations /// /// The that is used to register an Interface and extends the with fluent options /// - public interface IRegistrationBase : IRegistration, IWithFactory, IWithParameters, ILifestyleProvider + public interface IRegistrationBase : IRegistration, IWithFactory, IWithParameters { } diff --git a/LightweightIocContainer/IocContainer.cs b/LightweightIocContainer/IocContainer.cs index 60ed864..892467f 100644 --- a/LightweightIocContainer/IocContainer.cs +++ b/LightweightIocContainer/IocContainer.cs @@ -293,9 +293,9 @@ namespace LightweightIocContainer T resolvedInstance = registration switch { - IRegistrationBase { Lifestyle: Lifestyle.Singleton } defaultRegistration => GetOrCreateSingletonInstance(defaultRegistration, arguments, resolveStack), - IRegistrationBase { Lifestyle: Lifestyle.Multiton } and IMultitonRegistration multitonRegistration => GetOrCreateMultitonInstance(multitonRegistration, arguments, resolveStack), - IRegistrationBase defaultRegistration => CreateInstance(defaultRegistration, arguments, resolveStack), + RegistrationBase { Lifestyle: Lifestyle.Singleton } defaultRegistration => GetOrCreateSingletonInstance(defaultRegistration, arguments, resolveStack), + RegistrationBase { Lifestyle: Lifestyle.Multiton } and IMultitonRegistration multitonRegistration => GetOrCreateMultitonInstance(multitonRegistration, arguments, resolveStack), + RegistrationBase defaultRegistration => CreateInstance(defaultRegistration, arguments, resolveStack), ITypedFactoryRegistration typedFactoryRegistration => typedFactoryRegistration.Factory.Factory, _ => throw new UnknownRegistrationException($"There is no registration of type {registration.GetType().Name}.") }; @@ -386,7 +386,7 @@ namespace LightweightIocContainer /// A newly created instance of the given private T CreateInstance(IRegistration registration, object[] arguments, List resolveStack) { - if (registration is IWithParameters { Parameters: { } } registrationWithParameters) + if (registration is IWithParametersInternal { Parameters: { } } registrationWithParameters) arguments = UpdateArgumentsWithRegistrationParameters(registrationWithParameters, arguments); T instance; @@ -427,12 +427,12 @@ namespace LightweightIocContainer } /// - /// Update the given arguments with the of the given + /// Update the given arguments with the of the given /// /// The of the given /// The constructor arguments - /// The argument list updated with the - private object[] UpdateArgumentsWithRegistrationParameters(IWithParameters registration, object[] arguments) + /// The argument list updated with the + private object[] UpdateArgumentsWithRegistrationParameters(IWithParametersInternal registration, object[] arguments) { if (arguments != null && arguments.Any()) //if more arguments were passed to resolve { diff --git a/LightweightIocContainer/LightweightIocContainer.xml b/LightweightIocContainer/LightweightIocContainer.xml index cee4b5a..b609eed 100644 --- a/LightweightIocContainer/LightweightIocContainer.xml +++ b/LightweightIocContainer/LightweightIocContainer.xml @@ -574,11 +574,6 @@ Provides a method to an - - - The Factory added with the method - - Register an abstract typed factory for the @@ -594,15 +589,14 @@ The type of the implementation for the custom factory The current instance of this - + - Provides a method to an + The Factory added with the method - + - An of parameters that are used to an instance of this - Can be set in the by calling + Provides a method to an @@ -612,7 +606,7 @@ The parameters The current instance of this - are already set or no parameters given + are already set or no parameters given @@ -621,7 +615,13 @@ The parameters with their position The current instance of this - are already set or no parameters given + are already set or no parameters given + + + + An of parameters that are used to an instance of this + Can be set in the by calling + @@ -977,13 +977,13 @@ The current resolve stack A newly created instance of the given - + - Update the given arguments with the of the given + Update the given arguments with the of the given The of the given The constructor arguments - The argument list updated with the + The argument list updated with the @@ -1479,6 +1479,12 @@ Can be set in the by calling + + + This is invoked when an instance of this type is created. + Can be set in the by calling + + Pass an that will be invoked when an instance of this type is created diff --git a/LightweightIocContainer/Registrations/RegistrationBase.cs b/LightweightIocContainer/Registrations/RegistrationBase.cs index 940e3a7..2bc1e07 100644 --- a/LightweightIocContainer/Registrations/RegistrationBase.cs +++ b/LightweightIocContainer/Registrations/RegistrationBase.cs @@ -10,13 +10,14 @@ using LightweightIocContainer.Interfaces; using LightweightIocContainer.Interfaces.Factories; using LightweightIocContainer.Interfaces.Installers; using LightweightIocContainer.Interfaces.Registrations; +using LightweightIocContainer.Interfaces.Registrations.Fluent; namespace LightweightIocContainer.Registrations { /// /// The that is used to register an Interface /// - public abstract class RegistrationBase : IRegistrationBase + public abstract class RegistrationBase : IRegistrationBase, IWithFactoryInternal, IWithParametersInternal, ILifestyleProvider { private readonly IocContainer _container; diff --git a/LightweightIocContainer/Registrations/TypedRegistration.cs b/LightweightIocContainer/Registrations/TypedRegistration.cs index 5aa5f3b..a2bfa98 100644 --- a/LightweightIocContainer/Registrations/TypedRegistration.cs +++ b/LightweightIocContainer/Registrations/TypedRegistration.cs @@ -35,7 +35,13 @@ namespace LightweightIocContainer.Registrations /// This is invoked when an instance of this type is created. /// Can be set in the by calling /// - public Action OnCreateAction { get; private set; } + private Action OnCreateAction { get; set; } + + /// + /// This is invoked when an instance of this type is created. + /// Can be set in the by calling + /// + Action IOnCreate.OnCreateAction => OnCreateAction; /// /// Pass an that will be invoked when an instance of this type is created diff --git a/LightweightIocContainer/Validation/IocValidator.cs b/LightweightIocContainer/Validation/IocValidator.cs index 588c4ce..7278023 100644 --- a/LightweightIocContainer/Validation/IocValidator.cs +++ b/LightweightIocContainer/Validation/IocValidator.cs @@ -46,7 +46,7 @@ namespace LightweightIocContainer.Validation foreach (IRegistration registration in _iocContainer.Registrations) { - if (registration is IWithFactory { Factory: { } } withFactoryRegistration) + if (registration is IWithFactoryInternal { Factory: { } } withFactoryRegistration) { (from createMethod in withFactoryRegistration.Factory.CreateMethods select createMethod.GetParameters().Select(p => p.ParameterType) diff --git a/Test.LightweightIocContainer/RegistrationBaseTest.cs b/Test.LightweightIocContainer/RegistrationBaseTest.cs index cbb7a4f..81cc84a 100644 --- a/Test.LightweightIocContainer/RegistrationBaseTest.cs +++ b/Test.LightweightIocContainer/RegistrationBaseTest.cs @@ -5,7 +5,6 @@ using JetBrains.Annotations; using LightweightIocContainer; using LightweightIocContainer.Exceptions; -using LightweightIocContainer.Interfaces.Registrations; using LightweightIocContainer.Registrations; using Moq; using NUnit.Framework; @@ -58,7 +57,7 @@ namespace Test.LightweightIocContainer IBar bar = new Bar(); ITest test = new Test(); - IRegistrationBase testRegistration = registrationFactory.Register(Lifestyle.Transient).WithParameters(bar, test); + RegistrationBase testRegistration = (RegistrationBase) registrationFactory.Register(Lifestyle.Transient).WithParameters(bar, test); Assert.AreEqual(bar, testRegistration.Parameters[0]); Assert.AreEqual(test, testRegistration.Parameters[1]); @@ -72,7 +71,7 @@ namespace Test.LightweightIocContainer IBar bar = new Bar(); ITest test = new Test(); - IRegistrationBase testRegistration = registrationFactory.Register(Lifestyle.Transient).WithParameters((0, bar), (3, test), (2, "SomeString")); + RegistrationBase testRegistration = (RegistrationBase) registrationFactory.Register(Lifestyle.Transient).WithParameters((0, bar), (3, test), (2, "SomeString")); Assert.AreEqual(bar, testRegistration.Parameters[0]); Assert.IsInstanceOf(testRegistration.Parameters[1]);