Skip to content

Commit

Permalink
hardening the code against a try find service family miss. Closes GH-340
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Sep 28, 2023
1 parent a8f6abd commit e8425a0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface IGenericService<T> {}
class ServiceA : IGenericService<ClassA> {}

[Fact]
public void Test_it()
public void do_not_blow_up()
{
var container = new Container(x =>
{
Expand All @@ -24,7 +24,9 @@ public void Test_it()
container.GetInstance<IGenericService<ClassA>>()
.ShouldNotBeNull();

// This should return false. There's no registration for the
// open type, only a specific closed type
container.Model.HasRegistrationFor(typeof(IGenericService<>))
.ShouldBeTrue();
.ShouldBeFalse();
}
}
3 changes: 2 additions & 1 deletion src/Lamar/ServiceGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ private ServiceFamily buildClosedGenericType(Type serviceType, IServiceCollectio

public IEnumerable<Instance> AllInstances()
{
return _families.Enumerate().Select(x => x.Value).ToArray().SelectMany(x => x.All).ToArray();
var serviceFamilies = _families.Enumerate().Select(x => x.Value).Where(x => x != null).ToArray();
return serviceFamilies.SelectMany(x => x.All).ToArray();
}

public bool HasFamily(Type serviceType)
Expand Down

0 comments on commit e8425a0

Please sign in to comment.