From 024d820f914556df4cedaebb1df8157af478948b Mon Sep 17 00:00:00 2001 From: Simon Gockner Date: Sat, 14 Dec 2019 21:24:38 +0100 Subject: [PATCH] #33: add multipleRegistration handling, rename RegisterMultiton --- .../Interfaces/IIocContainer.cs | 12 +++++- LightweightIocContainer/IocContainer.cs | 41 ++++++++++++++++--- .../IocContainerTest.cs | 12 +++--- 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/LightweightIocContainer/Interfaces/IIocContainer.cs b/LightweightIocContainer/Interfaces/IIocContainer.cs index 0b5d2bd..01dc6e9 100644 --- a/LightweightIocContainer/Interfaces/IIocContainer.cs +++ b/LightweightIocContainer/Interfaces/IIocContainer.cs @@ -29,6 +29,16 @@ namespace LightweightIocContainer.Interfaces /// The created IDefaultRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface; + /// + /// Register multiple interfaces for a that implements them + /// + /// The base interface to register + /// A second interface to register + /// The that implements both interfaces + /// The for this + /// The created + IMultipleRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface2, TInterface1; + /// /// Register a without an interface /// @@ -44,7 +54,7 @@ namespace LightweightIocContainer.Interfaces /// The Type that implements the interface /// The Type of the multiton scope /// The created - IMultitonRegistration Register() where TImplementation : TInterface; + IMultitonRegistration RegisterMultiton() where TImplementation : TInterface; /// /// Register an Interface as an abstract typed factory diff --git a/LightweightIocContainer/IocContainer.cs b/LightweightIocContainer/IocContainer.cs index 342804a..45666d3 100644 --- a/LightweightIocContainer/IocContainer.cs +++ b/LightweightIocContainer/IocContainer.cs @@ -68,6 +68,26 @@ namespace LightweightIocContainer return registration; } + /// + /// Register multiple interfaces for a that implements them + /// + /// The base interface to register + /// A second interface to register + /// The that implements both interfaces + /// The for this + /// The created + public IMultipleRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface2, TInterface1 + { + IMultipleRegistration multipleRegistration = _registrationFactory.Register(lifestyle); + + foreach (var registration in multipleRegistration.Registrations) + { + Register(registration); + } + + return multipleRegistration; + } + /// /// Register a without an interface /// @@ -89,9 +109,9 @@ namespace LightweightIocContainer /// The Type that implements the interface /// The Type of the multiton scope /// The created - public IMultitonRegistration Register() where TImplementation : TInterface + public IMultitonRegistration RegisterMultiton() where TImplementation : TInterface { - IMultitonRegistration registration = _registrationFactory.Register(); + IMultitonRegistration registration = _registrationFactory.RegisterMultiton(); Register(registration); return registration; @@ -249,14 +269,22 @@ namespace LightweightIocContainer /// An existing or newly created singleton instance of the given private T GetOrCreateSingletonInstance(IRegistrationBase registration, object[] arguments, List resolveStack) { + Type type; + if (registration is ITypedRegistrationBase typedRegistration) + type = typedRegistration.ImplementationType; + else if (registration is ISingleTypeRegistration singleTypeRegistration) + type = singleTypeRegistration.InterfaceType; + else + throw new UnknownRegistrationException($"There is no registration {registration.GetType().Name} that can have lifestyle singleton."); + //if a singleton instance exists return it - object instance = _singletons.FirstOrDefault(s => s.type == typeof(T)).instance; + object instance = _singletons.FirstOrDefault(s => s.type == type).instance; if (instance != null) return (T) instance; //if it doesn't already exist create a new instance and add it to the list T newInstance = CreateInstance(registration, arguments, resolveStack); - _singletons.Add((typeof(T), newInstance)); + _singletons.Add((type, newInstance)); return newInstance; } @@ -338,7 +366,8 @@ namespace LightweightIocContainer else throw new UnknownRegistrationException($"There is no registration of type {registration.GetType().Name}."); - registration.OnCreateAction?.Invoke(instance); //TODO: Allow async OnCreateAction? + if (registration is IOnCreate onCreateRegistration) + onCreateRegistration.OnCreateAction?.Invoke(instance); //TODO: Allow async OnCreateAction? return instance; } @@ -403,7 +432,7 @@ namespace LightweightIocContainer if (!sortedConstructors.Any()) //no public constructor available throw new NoPublicConstructorFoundException(type); - NoMatchingConstructorFoundException noMatchingConstructorFoundException = null; + NoMatchingConstructorFoundException noMatchingConstructorFoundException = null; //TestMe: Is this thrown when a matching constructor is found but first a non matching one is found? foreach (ConstructorInfo ctor in sortedConstructors) { diff --git a/Test.LightweightIocContainer/IocContainerTest.cs b/Test.LightweightIocContainer/IocContainerTest.cs index 717255b..c9b855d 100644 --- a/Test.LightweightIocContainer/IocContainerTest.cs +++ b/Test.LightweightIocContainer/IocContainerTest.cs @@ -166,7 +166,7 @@ namespace Test.LightweightIocContainer [Test] public void TestRegisterMultiton() { - Assert.DoesNotThrow(() => _iocContainer.Register()); + Assert.DoesNotThrow(() => _iocContainer.RegisterMultiton()); } [Test] @@ -271,7 +271,7 @@ namespace Test.LightweightIocContainer [Test] public void TestResolveMultiton() { - _iocContainer.Register(); + _iocContainer.RegisterMultiton(); MultitonScope scope1 = new MultitonScope(); MultitonScope scope2 = new MultitonScope(); @@ -288,7 +288,7 @@ namespace Test.LightweightIocContainer [Test] public void TestResolveMultitonNoArgs() { - _iocContainer.Register(); + _iocContainer.RegisterMultiton(); MultitonResolveException exception = Assert.Throws(() => _iocContainer.Resolve()); Assert.AreEqual(typeof(ITest), exception.Type); @@ -297,7 +297,7 @@ namespace Test.LightweightIocContainer [Test] public void TestResolveMultitonWrongArgs() { - _iocContainer.Register(); + _iocContainer.RegisterMultiton(); MultitonResolveException exception = Assert.Throws(() => _iocContainer.Resolve(new object())); Assert.AreEqual(typeof(ITest), exception.Type); @@ -407,7 +407,7 @@ namespace Test.LightweightIocContainer [Test] public void TestResolveMultitonFromFactory() { - _iocContainer.Register(); + _iocContainer.RegisterMultiton(); _iocContainer.RegisterFactory(); MultitonScope scope1 = new MultitonScope(); @@ -427,7 +427,7 @@ namespace Test.LightweightIocContainer [Test] public void TestResolveMultitonFromFactoryClearInstances() { - _iocContainer.Register(); + _iocContainer.RegisterMultiton(); _iocContainer.RegisterFactory(); MultitonScope scope1 = new MultitonScope();