From 19a20ffd92a3b34a9022a6e150f6dd7d51e7a65a Mon Sep 17 00:00:00 2001 From: Simon G Date: Wed, 29 Jan 2020 15:04:57 +0100 Subject: [PATCH] - don't allow registration of lifestyle.multiton without iMultitonRegistration - add iLifestyleProvider and move Lifestyle from IRegistratrionBase<> to it - add non generic IMultitonRegistration as base class --- .../Registrations/ILifestyleProvider.cs | 17 ++++++++++++++++ .../Registrations/IMultitonRegistration.cs | 10 +++++++++- .../Registrations/IRegistrationBase.cs | 7 ++----- LightweightIocContainer/IocContainer.cs | 6 +++++- .../LightweightIocContainer.xml | 20 ++++++++++++++----- .../IocContainerTest.cs | 6 ++++++ 6 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 LightweightIocContainer/Interfaces/Registrations/ILifestyleProvider.cs diff --git a/LightweightIocContainer/Interfaces/Registrations/ILifestyleProvider.cs b/LightweightIocContainer/Interfaces/Registrations/ILifestyleProvider.cs new file mode 100644 index 0000000..084849b --- /dev/null +++ b/LightweightIocContainer/Interfaces/Registrations/ILifestyleProvider.cs @@ -0,0 +1,17 @@ +// // Author: Simon Gockner +// // Created: 2020-01-29 +// // Copyright(c) 2020 SimonG. All Rights Reserved. + +namespace LightweightIocContainer.Interfaces.Registrations +{ + /// + /// Provides a to an + /// + public interface ILifestyleProvider + { + /// + /// The Lifestyle of Instances that are created with this + /// + Lifestyle Lifestyle { get; } + } +} \ No newline at end of file diff --git a/LightweightIocContainer/Interfaces/Registrations/IMultitonRegistration.cs b/LightweightIocContainer/Interfaces/Registrations/IMultitonRegistration.cs index ec32045..2106215 100644 --- a/LightweightIocContainer/Interfaces/Registrations/IMultitonRegistration.cs +++ b/LightweightIocContainer/Interfaces/Registrations/IMultitonRegistration.cs @@ -6,10 +6,18 @@ using System; namespace LightweightIocContainer.Interfaces.Registrations { + /// + /// Non generic + /// + public interface IMultitonRegistration + { + + } + /// /// A base without implementation /// - public interface IMultitonRegistration : IRegistrationBase + public interface IMultitonRegistration : IRegistrationBase, IMultitonRegistration { /// /// The of the multiton scope diff --git a/LightweightIocContainer/Interfaces/Registrations/IRegistrationBase.cs b/LightweightIocContainer/Interfaces/Registrations/IRegistrationBase.cs index b90b444..69179ed 100644 --- a/LightweightIocContainer/Interfaces/Registrations/IRegistrationBase.cs +++ b/LightweightIocContainer/Interfaces/Registrations/IRegistrationBase.cs @@ -10,11 +10,8 @@ namespace LightweightIocContainer.Interfaces.Registrations /// The that is used to register an Interface /// /// The registered Interface - public interface IRegistrationBase : IRegistration, IWithParameters + public interface IRegistrationBase : IRegistration, ILifestyleProvider, IWithParameters { - /// - /// The Lifestyle of Instances that are created with this - /// - Lifestyle Lifestyle { get; } + } } \ No newline at end of file diff --git a/LightweightIocContainer/IocContainer.cs b/LightweightIocContainer/IocContainer.cs index b6f9bcc..6f114be 100644 --- a/LightweightIocContainer/IocContainer.cs +++ b/LightweightIocContainer/IocContainer.cs @@ -188,12 +188,16 @@ namespace LightweightIocContainer /// /// The given /// The is already registered in this - private void Register(IRegistration registration) //FixMe: Don't allow lifestyle.multiton without iMultitonRegistration + private void Register(IRegistration registration) { //if type is already registered if (_registrations.Any(r => r.InterfaceType == registration.InterfaceType)) throw new MultipleRegistrationException(registration.InterfaceType); + //don't allow lifestyle.multiton without iMultitonRegistration + if (registration is ILifestyleProvider lifestyleProvider && lifestyleProvider.Lifestyle == Lifestyle.Multiton && !(registration is IMultitonRegistration)) + throw new InvalidRegistrationException("Can't register a type as Lifestyle.Multiton without a scope (Registration is not of type IMultitonRegistration)."); + _registrations.Add(registration); } diff --git a/LightweightIocContainer/LightweightIocContainer.xml b/LightweightIocContainer/LightweightIocContainer.xml index 436ab93..cce9467 100644 --- a/LightweightIocContainer/LightweightIocContainer.xml +++ b/LightweightIocContainer/LightweightIocContainer.xml @@ -527,6 +527,16 @@ The of the interface The of the implementation + + + Provides a to an + + + + + The Lifestyle of Instances that are created with this + + The base interface for every to register multiple interfaces @@ -577,6 +587,11 @@ The fifth interface The implementation + + + Non generic + + A base without implementation @@ -615,11 +630,6 @@ The registered Interface - - - The Lifestyle of Instances that are created with this - - The to register either only an interface or only a diff --git a/Test.LightweightIocContainer/IocContainerTest.cs b/Test.LightweightIocContainer/IocContainerTest.cs index ac56340..3812233 100644 --- a/Test.LightweightIocContainer/IocContainerTest.cs +++ b/Test.LightweightIocContainer/IocContainerTest.cs @@ -182,6 +182,12 @@ namespace Test.LightweightIocContainer Assert.DoesNotThrow(() => _iocContainer.RegisterMultiton()); } + [Test] + public void TestInvalidMultitonRegistration() + { + Assert.Throws(() => _iocContainer.Register(Lifestyle.Multiton)); + } + [Test] public void TestRegisterFactory() {