1
- using System ;
2
- using Lamar . IoC . Instances ;
1
+ using Lamar . IoC . Instances ;
3
2
using Microsoft . Extensions . DependencyInjection ;
3
+ using System ;
4
4
5
5
namespace Lamar
6
6
{
7
7
public partial class ServiceRegistry
8
8
{
9
- public class InverseInstanceExpression < TImpl > where TImpl : class
9
+ public class ProvidedInstanceInverseInstanceExpression < TImpl > : BaseInverseInstanceExpression < TImpl >
10
+ where TImpl : class
10
11
{
11
- public InverseInstanceExpression ( ServiceRegistry parent )
12
+ public ProvidedInstanceInverseInstanceExpression ( ServiceRegistry parent , TImpl instance )
13
+ : base ( parent )
12
14
{
13
- _parent = parent ;
15
+ _instance = instance ;
14
16
}
15
17
16
- private readonly ServiceRegistry _parent ;
18
+ private readonly TImpl _instance ;
17
19
18
- public ScopedInverseInstanceExpression < TImpl > Named ( string name )
20
+ public override ScopedInverseInstanceExpression < TImpl > Named ( string name )
19
21
{
20
- return new ScopedInverseInstanceExpression < TImpl > ( _parent , name ) ;
22
+ return new ScopedInverseInstanceExpression < TImpl > ( Parent , _instance , name ) ;
21
23
}
22
24
23
- public ScopedInverseInstanceExpression < TImpl > Scoped ( )
25
+ public override ScopedInverseInstanceExpression < TImpl > Singleton ( )
24
26
{
25
- return new ScopedInverseInstanceExpression < TImpl > ( _parent , ServiceLifetime . Scoped ) ;
27
+ return new ScopedInverseInstanceExpression < TImpl > ( Parent , _instance ) ;
26
28
}
29
+ }
27
30
28
- public ScopedInverseInstanceExpression < TImpl > Singleton ( )
31
+ public class InverseInstanceExpression < TImpl > : BaseInverseInstanceExpression < TImpl >
32
+ where TImpl : class
33
+ {
34
+ public InverseInstanceExpression ( ServiceRegistry parent )
35
+ : base ( parent )
29
36
{
30
- return new ScopedInverseInstanceExpression < TImpl > ( _parent , ServiceLifetime . Singleton ) ;
31
37
}
32
38
33
39
public ScopedInverseInstanceExpression < TImpl > Transient ( )
34
40
{
35
- return new ScopedInverseInstanceExpression < TImpl > ( _parent , ServiceLifetime . Transient ) ;
41
+ return new ScopedInverseInstanceExpression < TImpl > ( Parent , ServiceLifetime . Transient ) ;
42
+ }
43
+
44
+ public ScopedInverseInstanceExpression < TImpl > Scoped ( )
45
+ {
46
+ return new ScopedInverseInstanceExpression < TImpl > ( Parent , ServiceLifetime . Scoped ) ;
47
+ }
48
+ }
49
+
50
+ public class BaseInverseInstanceExpression < TImpl > where TImpl : class
51
+ {
52
+ public BaseInverseInstanceExpression ( ServiceRegistry parent )
53
+ {
54
+ Parent = parent ;
55
+ }
56
+
57
+ protected ServiceRegistry Parent { get ; }
58
+
59
+ public virtual ScopedInverseInstanceExpression < TImpl > Named ( string name )
60
+ {
61
+ return new ScopedInverseInstanceExpression < TImpl > ( Parent , name ) ;
62
+ }
63
+
64
+ public virtual ScopedInverseInstanceExpression < TImpl > Singleton ( )
65
+ {
66
+ return new ScopedInverseInstanceExpression < TImpl > ( Parent , ServiceLifetime . Singleton ) ;
36
67
}
37
68
}
38
69
@@ -50,11 +81,26 @@ public ScopedInverseInstanceExpression(ServiceRegistry parent, ServiceLifetime l
50
81
_lifetime = lifetime ;
51
82
}
52
83
53
- private ServiceRegistry _parent ;
84
+ public ScopedInverseInstanceExpression ( ServiceRegistry parent , TImpl instance , string name )
85
+ {
86
+ _parent = parent ;
87
+ _providedInstance = instance ;
88
+ _name = name ;
89
+ }
90
+
91
+ public ScopedInverseInstanceExpression ( ServiceRegistry parent , TImpl instance )
92
+ {
93
+ _parent = parent ;
94
+ _providedInstance = instance ;
54
95
96
+ _lifetime = ServiceLifetime . Singleton ;
97
+ }
98
+
99
+ private readonly TImpl _providedInstance ;
100
+ private ServiceRegistry _parent ;
55
101
private string _name ;
56
102
private ServiceLifetime _lifetime ;
57
- private ConstructorInstance < TImpl > _rootInstance ;
103
+ private Instance _rootInstance ;
58
104
59
105
public ScopedInverseInstanceExpression < TImpl > For < TService > ( )
60
106
where TService : class
@@ -87,7 +133,14 @@ private void AddRootRegistration()
87
133
{
88
134
if ( _rootInstance is null )
89
135
{
90
- _rootInstance = _parent . For < TImpl > ( ) . Use < TImpl > ( ) ;
136
+ if ( _providedInstance is null )
137
+ {
138
+ _rootInstance = _parent . For < TImpl > ( ) . Use < TImpl > ( ) ;
139
+ }
140
+ else
141
+ {
142
+ _rootInstance = _parent . For < TImpl > ( ) . Use ( _providedInstance ) ;
143
+ }
91
144
92
145
_rootInstance . Lifetime = _lifetime ;
93
146
if ( ! String . IsNullOrEmpty ( _name ) )
0 commit comments