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

Commit 97aaeba

Browse files
committed
(#4) Removed all references to possible multiple registrations
1 parent 5b0f8e0 commit 97aaeba

File tree

4 files changed

+6
-111
lines changed

4 files changed

+6
-111
lines changed

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,12 @@ internal class RegistrarFixture
1212
private readonly List<Action<Container>> containerActions = new List<Action<Container>>();
1313
private readonly List<Action<ITypeRegistrar>> registrarActions = new List<Action<ITypeRegistrar>>();
1414
private Lifestyle lifestyle;
15-
private Type[] multiRegistrationTypes;
1615

1716
internal void GivenLifestyle(Lifestyle lifestyle)
1817
{
1918
this.lifestyle = lifestyle;
2019
}
2120

22-
internal void GivenMultiRegistrationTypes(params Type[] multiRegistrationTypes)
23-
{
24-
this.multiRegistrationTypes = multiRegistrationTypes;
25-
}
26-
2721
internal void GivenOnContainer(Action<Container> action)
2822
{
2923
containerActions.Add(action);
@@ -42,7 +36,7 @@ internal ITypeResolver GetResolver()
4236
action(container);
4337
}
4438

45-
var registrar = new SimpleInjectorRegistrar(container, lifestyle, multiRegistrationTypes);
39+
var registrar = new SimpleInjectorRegistrar(container, lifestyle);
4640
foreach (var action in registrarActions)
4741
{
4842
action(registrar);

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

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,6 @@ public void Resolver_Should_Return_Lazy_From_Registrar()
6969
ReferenceEquals(expected, actual).ShouldBeTrue();
7070
}
7171

72-
[Fact]
73-
public void Resolver_Resolving_From_Multiple_Lazies_Returns_The_Last_Registration()
74-
{
75-
var fixture = new RegistrarFixture();
76-
fixture.GivenMultiRegistrationTypes(typeof(ISomeInterface));
77-
fixture.GivenOnRegistrar(r => r.RegisterLazy(typeof(ISomeInterface), () => new SomeDependency()));
78-
fixture.GivenOnRegistrar(r => r.RegisterLazy(typeof(ISomeInterface), () => new SomeOtherDependency()));
79-
80-
var actual = fixture.GetResolver().Resolve(typeof(ISomeInterface));
81-
82-
actual.ShouldNotBeNull();
83-
actual.ShouldBeOfType<SomeOtherDependency>();
84-
}
85-
8672
[Fact(Skip = "Doesn't work at the moment")]
8773
public void Resolver_Should_Not_Call_Lazy_Factory_If_Not_Needed()
8874
{
@@ -99,57 +85,12 @@ public void Resolver_Should_Not_Call_Lazy_Factory_If_Not_Needed()
9985
factoryCalled.ShouldBeFalse();
10086
}
10187

102-
[Fact]
103-
public void Resolver_Registering_Multiple_Does_Not_Throw()
104-
{
105-
var fixture = new RegistrarFixture();
106-
fixture.GivenMultiRegistrationTypes(typeof(ISomeInterface));
107-
fixture.GivenOnRegistrar(r => r.Register(typeof(ISomeInterface), typeof(SomeDependency)));
108-
fixture.GivenOnRegistrar(r => r.Register(typeof(ISomeInterface), typeof(SomeOtherDependency)));
109-
110-
var actual = fixture.GetResolver().Resolve(typeof(ISomeInterface));
111-
112-
actual.ShouldNotBeNull();
113-
}
114-
115-
[Fact]
116-
public void Resolver_Resolving_From_Multiple_Returns_The_Last_Registration()
117-
{
118-
var fixture = new RegistrarFixture();
119-
fixture.GivenMultiRegistrationTypes(typeof(ISomeInterface));
120-
fixture.GivenOnRegistrar(r => r.Register(typeof(ISomeInterface), typeof(SomeDependency)));
121-
fixture.GivenOnRegistrar(r => r.Register(typeof(ISomeInterface), typeof(SomeOtherDependency)));
122-
123-
var actual = fixture.GetResolver().Resolve(typeof(ISomeInterface));
124-
125-
actual.ShouldNotBeNull();
126-
actual.ShouldBeOfType<SomeOtherDependency>();
127-
}
128-
129-
[Fact]
130-
public void Resolver_Resolving_From_Multiple_Instances_Returns_The_Last_Registration()
131-
{
132-
var fixture = new RegistrarFixture();
133-
fixture.GivenMultiRegistrationTypes(typeof(ISomeInterface));
134-
fixture.GivenOnRegistrar(r => r.RegisterInstance(typeof(ISomeInterface), new SomeDependency()));
135-
fixture.GivenOnRegistrar(r => r.RegisterInstance(typeof(ISomeInterface), new SomeOtherDependency()));
136-
137-
var actual = fixture.GetResolver().Resolve(typeof(ISomeInterface));
138-
139-
actual.ShouldNotBeNull();
140-
actual.ShouldBeOfType<SomeOtherDependency>();
141-
}
142-
14388
private interface ISomeInterface
14489
{
14590
}
14691

14792
private class SomeDependency : ISomeInterface
14893
{
14994
}
150-
151-
private class SomeOtherDependency : ISomeInterface
152-
{
153-
}
15495
}
15596
}

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

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
2-
using System.Linq;
32
using SimpleInjector;
43
using Spectre.Console.Cli;
54

6-
// ReSharper disable once CheckNamespace
75
namespace Spectre.Console
86
{
97
/// <summary>
@@ -13,90 +11,53 @@ public class SimpleInjectorRegistrar : ITypeRegistrar
1311
{
1412
private readonly Container container;
1513
private readonly Lifestyle lifestyle;
16-
private readonly Type[] multiRegistrationTypes;
1714

1815
/// <summary>
1916
/// Constructs a new instance using the the given <see cref="Container" />.
20-
/// <para>
21-
/// Since SimpleInjector inherently
22-
/// <see href="https://docs.simpleinjector.org/en/latest/decisions.html#separate-collections">differentiates
23-
/// the registration of collections from registering a single implementation</see> the types that are to be
24-
/// registered as collections need to be known before registering them. A list of types that are registered
25-
/// as collections can be set using the <paramref name="multiRegistrationTypes"/> param.
26-
/// </para>
2717
/// </summary>
2818
/// <param name="container">The <see cref="Container"/> to build the <see cref="ITypeRegistrar"/> around.</param>
2919
/// <param name="lifestyle">The <see cref="Lifestyle"/> to use during registrations. Default is <see cref="Lifestyle.Singleton"/>.</param>
30-
/// <param name="multiRegistrationTypes">List of types that are to be registered multiple times.
31-
/// Default is [ <see cref="ICommand{TSettings}"/>, <see cref="ICommand"/> ].</param>
3220
/// <exception cref="ArgumentNullException"></exception>
3321
public SimpleInjectorRegistrar(Container container,
34-
Lifestyle lifestyle = null,
35-
Type[] multiRegistrationTypes = null)
22+
Lifestyle lifestyle = null)
3623
{
3724
this.container = container ?? throw new ArgumentNullException(nameof(container));
3825
this.lifestyle = lifestyle ?? Lifestyle.Singleton;
39-
this.multiRegistrationTypes = multiRegistrationTypes ?? new[]
40-
{
41-
typeof(ICommand),
42-
typeof(ICommand<>)
43-
};
4426
}
4527

4628
/// <inheritdoc cref="ITypeRegistrar.Register"/>
4729
public void Register(Type service, Type implementation)
4830
{
49-
if (multiRegistrationTypes.Contains(service))
50-
{
51-
container.Collection.Append(service, implementation, lifestyle);
52-
return;
53-
}
54-
5531
container.Register(service, implementation, lifestyle);
5632
}
5733

5834
/// <inheritdoc cref="ITypeRegistrar.RegisterInstance"/>
5935
public void RegisterInstance(Type service, object implementation)
6036
{
61-
if (multiRegistrationTypes.Contains(service))
62-
{
63-
container.Collection.AppendInstance(service, implementation);
64-
return;
65-
}
66-
6737
container.RegisterInstance(service, implementation);
6838
}
6939

7040
/// <inheritdoc cref="ITypeRegistrar.RegisterLazy"/>
7141
public void RegisterLazy(Type service, Func<object> factory)
7242
{
7343
// todo: non of these code-paths are lazy!!
74-
if (multiRegistrationTypes.Contains(service))
75-
{
76-
var registration = lifestyle.CreateRegistration(service, factory, container);
77-
container.Collection.Append(service, registration);
78-
return;
79-
}
80-
8144
container.Register(service, factory, lifestyle);
8245
}
8346

8447
/// <inheritdoc cref="ITypeRegistrar.Build"/>
8548
public ITypeResolver Build()
8649
{
8750
container.Verify();
88-
return new SimpleInjectorTypeResolver(container, multiRegistrationTypes);
51+
return new SimpleInjectorTypeResolver(container);
8952
}
9053

9154
private class SimpleInjectorTypeResolver : ITypeResolver
9255
{
9356
private readonly Container container;
94-
private readonly Type[] multiRegistrationTypes;
9557

96-
public SimpleInjectorTypeResolver(Container container, Type[] multiRegistrationTypes)
58+
public SimpleInjectorTypeResolver(Container container)
9759
{
9860
this.container = container;
99-
this.multiRegistrationTypes = multiRegistrationTypes;
10061
}
10162

10263
public object Resolve(Type type)
@@ -106,9 +67,7 @@ public object Resolve(Type type)
10667
throw new ArgumentNullException(nameof(type));
10768
}
10869

109-
var implementation = multiRegistrationTypes.Contains(type)
110-
? container.GetAllInstances(type).LastOrDefault()
111-
: container.GetInstance(type);
70+
var implementation = container.GetInstance(type);
11271

11372
return implementation;
11473
}

src/Spectre.Console.Registrars.SimpleInjector/Spectre.Console.Registrars.SimpleInjector.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net5.0;net6.0</TargetFrameworks>
5+
<RootNamespace>Spectre.Console</RootNamespace>
56
</PropertyGroup>
67

78
<ItemGroup>

0 commit comments

Comments
 (0)