Skip to content
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

Commit dfe36db

Browse files
committed
(maint) fixed resolving of multiple registrations
1 parent 0a28113 commit dfe36db

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/Spectre.Console.Registrars.SimpleInjector.Tests/RegistrarTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,33 @@ public void Resolver_Should_Not_Call_Lazy_Factory_If_Not_Needed()
7272
factoryCalled.ShouldBeFalse();
7373
}
7474

75+
[Fact]
76+
public void Resolver_Registering_Multiple_Does_Not_Throw()
77+
{
78+
var fixture = new RegistrarFixture();
79+
fixture.GivenMultiRegistrationTypes(typeof(ISomeInterface));
80+
fixture.GivenOnRegistrar(r => r.Register(typeof(ISomeInterface), typeof(SomeDependency)));
81+
fixture.GivenOnRegistrar(r => r.Register(typeof(ISomeInterface), typeof(SomeOtherDependency)));
82+
83+
var actual = fixture.GetResolver().Resolve(typeof(ISomeInterface));
84+
85+
actual.ShouldNotBeNull();
86+
}
87+
88+
[Fact]
89+
public void Resolver_Resolving_From_Multiple_Returns_The_Last_Registration()
90+
{
91+
var fixture = new RegistrarFixture();
92+
fixture.GivenMultiRegistrationTypes(typeof(ISomeInterface));
93+
fixture.GivenOnRegistrar(r => r.Register(typeof(ISomeInterface), typeof(SomeDependency)));
94+
fixture.GivenOnRegistrar(r => r.Register(typeof(ISomeInterface), typeof(SomeOtherDependency)));
95+
96+
var actual = fixture.GetResolver().Resolve(typeof(ISomeInterface));
97+
98+
actual.ShouldNotBeNull();
99+
actual.ShouldBeOfType<SomeOtherDependency>();
100+
}
101+
75102
private interface ISomeInterface
76103
{
77104
}

src/Spectre.Console.Registrars.SimpleInjector/SimlpeInjectorRegistrar.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,18 @@ public void RegisterLazy(Type service, Func<object> factory)
8484
public ITypeResolver Build()
8585
{
8686
container.Verify();
87-
return new SimpleInjectorTypeResolver(container);
87+
return new SimpleInjectorTypeResolver(container, multiRegistrationTypes);
8888
}
8989

9090
private class SimpleInjectorTypeResolver : ITypeResolver
9191
{
9292
private readonly Container container;
93+
private readonly Type[] multiRegistrationTypes;
9394

94-
public SimpleInjectorTypeResolver(Container container)
95+
public SimpleInjectorTypeResolver(Container container, Type[] multiRegistrationTypes)
9596
{
9697
this.container = container;
98+
this.multiRegistrationTypes = multiRegistrationTypes;
9799
}
98100

99101
public object Resolve(Type type)
@@ -103,6 +105,11 @@ public object Resolve(Type type)
103105
throw new ArgumentNullException(nameof(type));
104106
}
105107

108+
if (multiRegistrationTypes.Contains(type))
109+
{
110+
return container.GetAllInstances(type).LastOrDefault();
111+
}
112+
106113
return container.GetInstance(type);
107114
}
108115
}

0 commit comments

Comments
 (0)