From 1069b1019cfe6f0feaec351e6087f18e1fc2bba4 Mon Sep 17 00:00:00 2001 From: dotnetjunkie Date: Wed, 1 May 2019 20:58:38 +0200 Subject: [PATCH] Added missing Container.Collection.Append(Type, Type, Lifestyle) overload. Fixes #691. --- .../Advanced/IndexableCollectionsTests.cs | 17 +++++++++ ...iner.Registration.CollectionRegistrator.cs | 37 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/SimpleInjector.Tests.Unit/Advanced/IndexableCollectionsTests.cs b/src/SimpleInjector.Tests.Unit/Advanced/IndexableCollectionsTests.cs index ed3b0957a..e6497c055 100644 --- a/src/SimpleInjector.Tests.Unit/Advanced/IndexableCollectionsTests.cs +++ b/src/SimpleInjector.Tests.Unit/Advanced/IndexableCollectionsTests.cs @@ -214,6 +214,23 @@ public void IndexOf_Null_ReturnsNegative1() Assert.AreEqual(-1, actualIndex); } + [TestMethod] + public void GetAllInstances_NonGenericAppendTypeWithLifestyle_RegistersTypeAccordingToExpectedLifestyle() + { + // Arrange + var container = ContainerFactory.New(); + + container.Collection.Append(typeof(IPlugin), typeof(Plugin0), Lifestyle.Singleton); + container.Collection.Append(typeof(IPlugin), typeof(Plugin1), Lifestyle.Transient); + + // Act + var plugins = container.GetAllInstances(); + + // Assert + Assert.AreSame(plugins.First(), plugins.First(), "Plugin0 should be Singleton."); + Assert.AreNotSame(plugins.Last(), plugins.Last(), "Plugin1 should be Transient."); + } + [TestMethod] public void Index_ValuePartOfTheCollection1_ReturnsCorrectIndex() { diff --git a/src/SimpleInjector/Container.Registration.CollectionRegistrator.cs b/src/SimpleInjector/Container.Registration.CollectionRegistrator.cs index 00fef02e7..082396d88 100644 --- a/src/SimpleInjector/Container.Registration.CollectionRegistrator.cs +++ b/src/SimpleInjector/Container.Registration.CollectionRegistrator.cs @@ -515,6 +515,43 @@ public void Append(Type serviceType, Type implementationType) this.AppendToCollectionInternal(serviceType, implementationType); } + /// + /// Appends a new registration to existing registrations made for a collection of + /// elements using one of the + /// Container.Collections.Register + /// overloads. + /// + /// The service type of the collection. + /// The implementation type to append. + /// The lifestyle that specifies how the returned instance will be cached. + /// Thrown when one of the supplied arguments is a null + /// reference (Nothing in VB). + /// Thrown when the is not a + /// reference type, or ambiguous. + /// Thrown when the container is locked. + /// Thrown when the method is called for a registration + /// that is made with one of the Collections.Register overloads that accepts a dynamic collection + /// (an IEnumerable or IEnumerable<TService>). + public void Append(Type serviceType, Type implementationType, Lifestyle lifestyle) + { + Requires.IsNotNull(serviceType, nameof(serviceType)); + Requires.IsNotNull(implementationType, nameof(implementationType)); + Requires.IsNotNull(lifestyle, nameof(lifestyle)); + + Requires.IsReferenceType(serviceType, nameof(serviceType)); + Requires.IsNotAnAmbiguousType(serviceType, nameof(serviceType)); + + Requires.ServiceOrItsGenericTypeDefinitionIsAssignableFromImplementation( + serviceType, implementationType, nameof(implementationType)); + + Requires.OpenGenericTypeDoesNotContainUnresolvableTypeArguments( + serviceType, implementationType, nameof(implementationType)); + + var registration = lifestyle.CreateRegistration(implementationType, this.Container); + + this.AppendToCollectionInternal(serviceType, registration); + } + /// /// Appends the specified delegate to existing registrations /// made for a collection of elements using one of the