1
1
using System ;
2
- using System . Linq ;
3
2
using SimpleInjector ;
4
3
using Spectre . Console . Cli ;
5
4
6
- // ReSharper disable once CheckNamespace
7
5
namespace Spectre . Console
8
6
{
9
7
/// <summary>
@@ -13,90 +11,53 @@ public class SimpleInjectorRegistrar : ITypeRegistrar
13
11
{
14
12
private readonly Container container ;
15
13
private readonly Lifestyle lifestyle ;
16
- private readonly Type [ ] multiRegistrationTypes ;
17
14
18
15
/// <summary>
19
16
/// 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>
27
17
/// </summary>
28
18
/// <param name="container">The <see cref="Container"/> to build the <see cref="ITypeRegistrar"/> around.</param>
29
19
/// <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>
32
20
/// <exception cref="ArgumentNullException"></exception>
33
21
public SimpleInjectorRegistrar ( Container container ,
34
- Lifestyle lifestyle = null ,
35
- Type [ ] multiRegistrationTypes = null )
22
+ Lifestyle lifestyle = null )
36
23
{
37
24
this . container = container ?? throw new ArgumentNullException ( nameof ( container ) ) ;
38
25
this . lifestyle = lifestyle ?? Lifestyle . Singleton ;
39
- this . multiRegistrationTypes = multiRegistrationTypes ?? new [ ]
40
- {
41
- typeof ( ICommand ) ,
42
- typeof ( ICommand < > )
43
- } ;
44
26
}
45
27
46
28
/// <inheritdoc cref="ITypeRegistrar.Register"/>
47
29
public void Register ( Type service , Type implementation )
48
30
{
49
- if ( multiRegistrationTypes . Contains ( service ) )
50
- {
51
- container . Collection . Append ( service , implementation , lifestyle ) ;
52
- return ;
53
- }
54
-
55
31
container . Register ( service , implementation , lifestyle ) ;
56
32
}
57
33
58
34
/// <inheritdoc cref="ITypeRegistrar.RegisterInstance"/>
59
35
public void RegisterInstance ( Type service , object implementation )
60
36
{
61
- if ( multiRegistrationTypes . Contains ( service ) )
62
- {
63
- container . Collection . AppendInstance ( service , implementation ) ;
64
- return ;
65
- }
66
-
67
37
container . RegisterInstance ( service , implementation ) ;
68
38
}
69
39
70
40
/// <inheritdoc cref="ITypeRegistrar.RegisterLazy"/>
71
41
public void RegisterLazy ( Type service , Func < object > factory )
72
42
{
73
43
// 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
-
81
44
container . Register ( service , factory , lifestyle ) ;
82
45
}
83
46
84
47
/// <inheritdoc cref="ITypeRegistrar.Build"/>
85
48
public ITypeResolver Build ( )
86
49
{
87
50
container . Verify ( ) ;
88
- return new SimpleInjectorTypeResolver ( container , multiRegistrationTypes ) ;
51
+ return new SimpleInjectorTypeResolver ( container ) ;
89
52
}
90
53
91
54
private class SimpleInjectorTypeResolver : ITypeResolver
92
55
{
93
56
private readonly Container container ;
94
- private readonly Type [ ] multiRegistrationTypes ;
95
57
96
- public SimpleInjectorTypeResolver ( Container container , Type [ ] multiRegistrationTypes )
58
+ public SimpleInjectorTypeResolver ( Container container )
97
59
{
98
60
this . container = container ;
99
- this . multiRegistrationTypes = multiRegistrationTypes ;
100
61
}
101
62
102
63
public object Resolve ( Type type )
@@ -106,9 +67,7 @@ public object Resolve(Type type)
106
67
throw new ArgumentNullException ( nameof ( type ) ) ;
107
68
}
108
69
109
- var implementation = multiRegistrationTypes . Contains ( type )
110
- ? container . GetAllInstances ( type ) . LastOrDefault ( )
111
- : container . GetInstance ( type ) ;
70
+ var implementation = container . GetInstance ( type ) ;
112
71
113
72
return implementation ;
114
73
}
0 commit comments