1
1
using System ;
2
2
using Autofac ;
3
+ using Autofac . Builder ;
3
4
4
5
namespace LazyProxy . Autofac
5
6
{
@@ -8,59 +9,32 @@ namespace LazyProxy.Autofac
8
9
/// </summary>
9
10
public static class AutofacExtensions
10
11
{
11
- /// <summary>
12
- /// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
13
- /// The real class To will be instantiated only after first method execution.
14
- /// </summary>
15
- /// <param name="builder">The instance of the Autofac container builder.</param>
16
- /// <typeparam name="TFrom">The binded interface.</typeparam>
17
- /// <typeparam name="TTo">The binded class.</typeparam>
18
- /// <returns>The instance of the Autofac container builder.</returns>
19
- public static ContainerBuilder RegisterLazy < TFrom , TTo > ( this ContainerBuilder builder )
20
- where TTo : TFrom where TFrom : class =>
21
- builder . RegisterLazy < TFrom , TTo > ( null , ServiceLifetime . InstancePerDependency ) ;
22
-
23
- /// <summary>
24
- /// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
25
- /// The real class To will be instantiated only after first method or property execution.
26
- /// </summary>
27
- /// <param name="builder">The instance of the Autofac container builder.</param>
28
- /// <param name="name">The registration name.</param>
29
- /// <typeparam name="TFrom">The binded interface.</typeparam>
30
- /// <typeparam name="TTo">The binded class.</typeparam>
31
- /// <returns>The instance of the Autofac container builder.</returns>
32
- public static ContainerBuilder RegisterLazy < TFrom , TTo > ( this ContainerBuilder builder , string name )
33
- where TTo : TFrom where TFrom : class =>
34
- builder . RegisterLazy < TFrom , TTo > ( name , ServiceLifetime . InstancePerDependency ) ;
35
-
36
12
/// <summary>
37
13
/// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
38
14
/// The real class To will be instantiated only after first method or property execution.
39
15
/// </summary>
40
16
/// <param name="builder">The instance of the Autofac container builder.</param>
41
- /// <param name="serviceLifetime">The instance lifetime.</param>
42
17
/// <typeparam name="TFrom">The binded interface.</typeparam>
43
18
/// <typeparam name="TTo">The binded class.</typeparam>
44
- /// <returns>The instance of the Autofac container builder.</returns>
45
- public static ContainerBuilder RegisterLazy < TFrom , TTo > (
46
- this ContainerBuilder builder , ServiceLifetime serviceLifetime )
19
+ /// <returns>The instance of the Autofac registration builder.</returns>
20
+ public static IRegistrationBuilder < object , SimpleActivatorData , SingleRegistrationStyle >
21
+ RegisterLazy < TFrom , TTo > ( this ContainerBuilder builder )
47
22
where TTo : TFrom where TFrom : class =>
48
- builder . RegisterLazy < TFrom , TTo > ( null , serviceLifetime ) ;
23
+ builder . RegisterLazy ( typeof ( TFrom ) , typeof ( TTo ) , null ) ;
49
24
50
25
/// <summary>
51
26
/// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
52
27
/// The real class To will be instantiated only after first method or property execution.
53
28
/// </summary>
54
29
/// <param name="builder">The instance of the Autofac container builder.</param>
55
- /// <param name="name">The registration name.</param>
56
- /// <param name="serviceLifetime">The instance lifetime.</param>
30
+ /// <param name="name">The registration name. Null if named registration is not required.</param>
57
31
/// <typeparam name="TFrom">The binded interface.</typeparam>
58
32
/// <typeparam name="TTo">The binded class.</typeparam>
59
- /// <returns>The instance of the Autofac container builder.</returns>
60
- public static ContainerBuilder RegisterLazy < TFrom , TTo > (
61
- this ContainerBuilder builder , string name , ServiceLifetime serviceLifetime )
33
+ /// <returns>The instance of the Autofac registration builder.</returns>
34
+ public static IRegistrationBuilder < object , SimpleActivatorData , SingleRegistrationStyle >
35
+ RegisterLazy < TFrom , TTo > ( this ContainerBuilder builder , string name )
62
36
where TTo : TFrom where TFrom : class =>
63
- builder . RegisterLazy ( typeof ( TFrom ) , typeof ( TTo ) , name , serviceLifetime ) ;
37
+ builder . RegisterLazy ( typeof ( TFrom ) , typeof ( TTo ) , name ) ;
64
38
65
39
/// <summary>
66
40
/// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
@@ -69,36 +43,10 @@ public static ContainerBuilder RegisterLazy<TFrom, TTo>(
69
43
/// <param name="typeFrom">The binded interface.</param>
70
44
/// <param name="typeTo">The binded class.</param>
71
45
/// <param name="builder">The instance of the Autofac container builder.</param>
72
- /// <returns>The instance of the Autofac container builder.</returns>
73
- public static ContainerBuilder RegisterLazy (
74
- this ContainerBuilder builder , Type typeFrom , Type typeTo ) =>
75
- builder . RegisterLazy ( typeFrom , typeTo , null , ServiceLifetime . InstancePerDependency ) ;
76
-
77
- /// <summary>
78
- /// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
79
- /// The real class To will be instantiated only after first method or property execution.
80
- /// </summary>
81
- /// <param name="typeFrom">The binded interface.</param>
82
- /// <param name="typeTo">The binded class.</param>
83
- /// <param name="builder">The instance of the Autofac container builder.</param>
84
- /// <param name="name">The registration name.</param>
85
- /// <returns>The instance of the Autofac container builder.</returns>
86
- public static ContainerBuilder RegisterLazy (
87
- this ContainerBuilder builder , Type typeFrom , Type typeTo , string name ) =>
88
- builder . RegisterLazy ( typeFrom , typeTo , name , ServiceLifetime . InstancePerDependency ) ;
89
-
90
- /// <summary>
91
- /// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
92
- /// The real class To will be instantiated only after first method or property execution.
93
- /// </summary>
94
- /// <param name="typeFrom">The binded interface.</param>
95
- /// <param name="typeTo">The binded class.</param>
96
- /// <param name="builder">The instance of the Autofac container builder.</param>
97
- /// <param name="serviceLifetime">The instance lifetime.</param>
98
- /// <returns>The instance of the Autofac container builder.</returns>
99
- public static ContainerBuilder RegisterLazy (
100
- this ContainerBuilder builder , Type typeFrom , Type typeTo , ServiceLifetime serviceLifetime ) =>
101
- builder . RegisterLazy ( typeFrom , typeTo , null , serviceLifetime ) ;
46
+ /// <returns>The instance of the Autofac registration builder.</returns>
47
+ public static IRegistrationBuilder < object , SimpleActivatorData , SingleRegistrationStyle >
48
+ RegisterLazy ( this ContainerBuilder builder , Type typeFrom , Type typeTo ) =>
49
+ builder . RegisterLazy ( typeFrom , typeTo , null ) ;
102
50
103
51
/// <summary>
104
52
/// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
@@ -107,11 +55,10 @@ public static ContainerBuilder RegisterLazy(
107
55
/// <param name="typeFrom">The binded interface.</param>
108
56
/// <param name="typeTo">The binded class.</param>
109
57
/// <param name="builder">The instance of the Autofac container builder.</param>
110
- /// <param name="name">The registration name.</param>
111
- /// <param name="serviceLifetime">The instance lifetime.</param>
112
- /// <returns>The instance of the Autofac container builder.</returns>
113
- public static ContainerBuilder RegisterLazy (
114
- this ContainerBuilder builder , Type typeFrom , Type typeTo , string name , ServiceLifetime serviceLifetime )
58
+ /// <param name="name">The registration name. Null if named registration is not required.</param>
59
+ /// <returns>The instance of the Autofac registration builder.</returns>
60
+ public static IRegistrationBuilder < object , SimpleActivatorData , SingleRegistrationStyle >
61
+ RegisterLazy ( this ContainerBuilder builder , Type typeFrom , Type typeTo , string name )
115
62
{
116
63
// There is no way to constraint it on the compilation step.
117
64
if ( ! typeFrom . IsInterface )
@@ -132,52 +79,9 @@ public static ContainerBuilder RegisterLazy(
132
79
) ;
133
80
} ) ;
134
81
135
- // Variant 2 (Overrides dont work
136
- // builder.RegisterType(typeTo).Named(registrationName, typeTo);
137
- //
138
- // var funcType = typeof(Func<>);
139
- // var factoryType = funcType.MakeGenericType(typeTo);
140
- //
141
- // var registration = builder.Register((c, p) =>
142
- // LazyProxyBuilder.CreateInstance(typeFrom, (Func<object>)c.ResolveNamed(registrationName, factoryType)));
143
-
144
- if ( name == null )
145
- {
146
- registration . As ( typeFrom ) ;
147
- }
148
- else
149
- {
150
- registration . Named ( name , typeFrom ) ;
151
- }
152
-
153
- switch ( serviceLifetime )
154
- {
155
- case ServiceLifetime . Unknown :
156
- case ServiceLifetime . InstancePerDependency :
157
- registration . InstancePerDependency ( ) . ExternallyOwned ( ) ;
158
- break ;
159
- case ServiceLifetime . SingleInstance :
160
- registration . SingleInstance ( ) ;
161
- break ;
162
- case ServiceLifetime . InstancePerLifetimeScope :
163
- registration . InstancePerLifetimeScope ( ) ;
164
- break ;
165
- default :
166
- throw new ArgumentOutOfRangeException ( nameof ( serviceLifetime ) , serviceLifetime , null ) ;
167
- }
168
-
169
- return builder ;
82
+ return name == null
83
+ ? registration . As ( typeFrom )
84
+ : registration . Named ( name , typeFrom ) ;
170
85
}
171
86
}
172
-
173
- /// <summary>
174
- /// Service lifetime provided by the IoC container.
175
- /// </summary>
176
- public enum ServiceLifetime
177
- {
178
- Unknown = 0 ,
179
- SingleInstance = 1 ,
180
- InstancePerLifetimeScope = 2 ,
181
- InstancePerDependency = 3
182
- }
183
87
}
0 commit comments