Skip to content

Commit

Permalink
Added missing Container.Collection.Append(Type, Type, Lifestyle) over…
Browse files Browse the repository at this point in the history
…load. Fixes #691.
  • Loading branch information
dotnetjunkie committed May 1, 2019
1 parent 6397f11 commit 1069b10
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
Expand Up @@ -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<IPlugin>();

// 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()
{
Expand Down
37 changes: 37 additions & 0 deletions src/SimpleInjector/Container.Registration.CollectionRegistrator.cs
Expand Up @@ -515,6 +515,43 @@ public void Append(Type serviceType, Type implementationType)
this.AppendToCollectionInternal(serviceType, implementationType);
}

/// <summary>
/// Appends a new registration to existing registrations made for a collection of
/// <paramref name="serviceType"/> elements using one of the
/// <see cref="Register(Type, IEnumerable{Type})">Container.Collections.Register</see>
/// overloads.
/// </summary>
/// <param name="serviceType">The service type of the collection.</param>
/// <param name="implementationType">The implementation type to append.</param>
/// <param name="lifestyle">The lifestyle that specifies how the returned instance will be cached.</param>
/// <exception cref="ArgumentNullException">Thrown when one of the supplied arguments is a null
/// reference (Nothing in VB).</exception>
/// <exception cref="ArgumentException">Thrown when the <paramref name="serviceType"/> is not a
/// reference type, or ambiguous.</exception>
/// <exception cref="InvalidOperationException">Thrown when the container is locked.</exception>
/// <exception cref="NotSupportedException">Thrown when the method is called for a registration
/// that is made with one of the <b>Collections.Register</b> overloads that accepts a dynamic collection
/// (an <b>IEnumerable</b> or <b>IEnumerable&lt;TService&gt;</b>).</exception>
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);
}

/// <summary>
/// Appends the specified delegate <paramref name="instanceCreator"/> to existing registrations
/// made for a collection of <typeparamref name="TService"/> elements using one of the
Expand Down

0 comments on commit 1069b10

Please sign in to comment.