Skip to content

Commit

Permalink
Fix IEnumerable<T> can't be resolved during startup.
Browse files Browse the repository at this point in the history
  • Loading branch information
rizi authored and jeremydmiller committed Nov 18, 2024
1 parent 74e4ac2 commit a9fd707
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;

using Shouldly;
using StructureMap.Testing.Widget;
using Xunit;
Expand Down Expand Up @@ -42,7 +44,7 @@ public void fix_for_391_dont_resolve_collections_with_not_registered_type()
{
var containerWithNoRegistrations = Container.Empty();

containerWithNoRegistrations.IsService(typeof(IEnumerable<ConcreteClass>)).ShouldBeFalse();
containerWithNoRegistrations.IsService(typeof(IEnumerable<ConcreteClass>)).ShouldBeTrue();
containerWithNoRegistrations.IsService(typeof(IReadOnlyCollection<ConcreteClass>)).ShouldBeFalse();
containerWithNoRegistrations.IsService(typeof(IList<ConcreteClass>)).ShouldBeFalse();
containerWithNoRegistrations.IsService(typeof(List<ConcreteClass>)).ShouldBeFalse();
Expand All @@ -58,6 +60,50 @@ public void fix_for_391_dont_resolve_collections_with_not_registered_type()
containerWithRegistrations.IsService(typeof(List<ConcreteClass>)).ShouldBeTrue();
}

[Fact]
public void fix_for_405_add_support_for_net9()
{
var containerWithNoRegistrations = Container.Empty();

containerWithNoRegistrations.IsService(typeof(IEnumerable<object>)).ShouldBeTrue();
containerWithNoRegistrations.IsService(typeof(IEnumerable<string>)).ShouldBeTrue();
containerWithNoRegistrations.IsService(typeof(IEnumerable<int>)).ShouldBeTrue();
containerWithNoRegistrations.IsService(typeof(IEnumerable<int?>)).ShouldBeTrue();
containerWithNoRegistrations.IsService(typeof(IEnumerable<float>)).ShouldBeTrue();
containerWithNoRegistrations.IsService(typeof(IEnumerable<float?>)).ShouldBeTrue();
containerWithNoRegistrations.IsService(typeof(IEnumerable<double>)).ShouldBeTrue();
containerWithNoRegistrations.IsService(typeof(IEnumerable<double?>)).ShouldBeTrue();
containerWithNoRegistrations.IsService(typeof(IEnumerable<decimal>)).ShouldBeTrue();
containerWithNoRegistrations.IsService(typeof(IEnumerable<decimal?>)).ShouldBeTrue();
containerWithNoRegistrations.IsService(typeof(IEnumerable<DateTime>)).ShouldBeTrue();
containerWithNoRegistrations.IsService(typeof(IEnumerable<DateTime?>)).ShouldBeTrue();
containerWithNoRegistrations.IsService(typeof(IEnumerable<DateOnly?>)).ShouldBeTrue();
containerWithNoRegistrations.IsService(typeof(IEnumerable<DateOnly?>)).ShouldBeTrue();

containerWithNoRegistrations.IsService(typeof(IReadOnlyCollection<object>)).ShouldBeFalse();
containerWithNoRegistrations.IsService(typeof(IReadOnlyList<object>)).ShouldBeFalse();
containerWithNoRegistrations.IsService(typeof(IList<object>)).ShouldBeFalse();
containerWithNoRegistrations.IsService(typeof(List<object>)).ShouldBeFalse();

List<int> integerList = new List<int>();

var containerWithRegistrations = Container.For(services =>
{
services.ForConcreteType<ConcreteClass>();
services.For<IEnumerable<int>>().Use(integerList);
});

containerWithRegistrations.IsService(typeof(IEnumerable<ConcreteClass>)).ShouldBeTrue();
containerWithRegistrations.IsService(typeof(IReadOnlyCollection<ConcreteClass>)).ShouldBeTrue();
containerWithRegistrations.IsService(typeof(IReadOnlyList<ConcreteClass>)).ShouldBeTrue();
containerWithRegistrations.IsService(typeof(IList<ConcreteClass>)).ShouldBeTrue();
containerWithRegistrations.IsService(typeof(List<ConcreteClass>)).ShouldBeTrue();

containerWithRegistrations.IsService(typeof(IEnumerable<int>)).ShouldBeTrue();

containerWithRegistrations.GetInstance<IEnumerable<int>>().ShouldBeSameAs(integerList);
}

public interface IService
{
}
Expand Down
4 changes: 2 additions & 2 deletions src/Lamar/ServiceGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ internal ServiceFamily TryToCreateMissingFamilyWithNetCoreRules(Type serviceType

private Type getServiceTypeThatTakesCollectionsIntoAccount(Type serviceType)
{
if (!typeof(IEnumerable).IsAssignableFrom(serviceType) || serviceType.GetGenericArguments().Length != 1)
if (!typeof(IEnumerable).IsAssignableFrom(serviceType) || serviceType.GetGenericArguments().Length != 1 || serviceType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
return serviceType;

Type type = serviceType.GetGenericArguments().Single();
Expand All @@ -507,7 +507,7 @@ private Type getServiceTypeThatTakesCollectionsIntoAccount(Type serviceType)

return isTypeRegistered ? serviceType : type;
}

internal void ClearPlanning()
{
_chain.Clear();
Expand Down

0 comments on commit a9fd707

Please sign in to comment.