forked from autofac/Autofac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IRegistrationBuilder.cs
332 lines (297 loc) · 19.2 KB
/
IRegistrationBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// This software is part of the Autofac IoC container
// Copyright © 2011 Autofac Contributors
// https://autofac.org
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using Autofac.Core;
using Autofac.Core.Resolving.Pipeline;
namespace Autofac.Builder
{
/// <summary>
/// Data structure used to construct registrations.
/// </summary>
/// <typeparam name="TLimit">The most specific type to which instances of the registration
/// can be cast.</typeparam>
/// <typeparam name="TActivatorData">Activator builder type.</typeparam>
/// <typeparam name="TRegistrationStyle">Registration style type.</typeparam>
public interface IRegistrationBuilder<out TLimit, out TActivatorData, out TRegistrationStyle>
{
/// <summary>
/// Gets the registration data.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
RegistrationData RegistrationData { get; }
/// <summary>
/// Gets the activator data.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
TActivatorData ActivatorData { get; }
/// <summary>
/// Gets the registration style.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
TRegistrationStyle RegistrationStyle { get; }
/// <summary>
/// Gets the resolve pipeline for this registration.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
IResolvePipelineBuilder ResolvePipeline { get; }
/// <summary>
/// Configure the component so that instances are never disposed by the container.
/// </summary>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> ExternallyOwned();
/// <summary>
/// Configure the component so that instances that support IDisposable are
/// disposed by the container (default).
/// </summary>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OwnedByLifetimeScope();
/// <summary>
/// Configure the component so that every dependent component or call to Resolve()
/// gets a new, unique instance (default).
/// </summary>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> InstancePerDependency();
/// <summary>
/// Configure the component so that every dependent component or call to Resolve()
/// gets the same, shared instance.
/// </summary>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> SingleInstance();
/// <summary>
/// Configure the component so that every dependent component or call to Resolve()
/// within a single ILifetimeScope gets the same, shared instance. Dependent components in
/// different lifetime scopes will get different instances.
/// </summary>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> InstancePerLifetimeScope();
/// <summary>
/// Configure the component so that every dependent component or call to Resolve() within
/// a ILifetimeScope tagged with any of the provided tags value gets the same, shared instance.
/// Dependent components in lifetime scopes that are children of the tagged scope will
/// share the parent's instance. If no appropriately tagged scope can be found in the
/// hierarchy an <see cref="DependencyResolutionException"/> is thrown.
/// </summary>
/// <param name="lifetimeScopeTag">Tag applied to matching lifetime scopes.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> InstancePerMatchingLifetimeScope(params object[] lifetimeScopeTag);
/// <summary>
/// Configure the component so that every dependent component or call to Resolve()
/// within a ILifetimeScope created by an owned instance gets the same, shared instance.
/// Dependent components in lifetime scopes that are children of the owned instance scope will
/// share the parent's instance. If no appropriate owned instance scope can be found in the
/// hierarchy an <see cref="DependencyResolutionException"/> is thrown.
/// </summary>
/// <typeparam name="TService">Service type.</typeparam>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> InstancePerOwned<TService>();
/// <summary>
/// Configure the component so that every dependent component or call to Resolve()
/// within a ILifetimeScope created by an owned instance gets the same, shared instance.
/// Dependent components in lifetime scopes that are children of the owned instance scope will
/// share the parent's instance. If no appropriate owned instance scope can be found in the
/// hierarchy an <see cref="DependencyResolutionException"/> is thrown.
/// </summary>
/// <param name="serviceType">Service type.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> InstancePerOwned(Type serviceType);
/// <summary>
/// Configure the component so that every dependent component or call to Resolve()
/// within a ILifetimeScope created by an owned instance gets the same, shared instance.
/// Dependent components in lifetime scopes that are children of the owned instance scope will
/// share the parent's instance. If no appropriate owned instance scope can be found in the
/// hierarchy an <see cref="DependencyResolutionException"/> is thrown.
/// </summary>
/// <typeparam name="TService">The service type provided by the component.</typeparam>
/// <param name="serviceKey">Key to associate with the component.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> InstancePerOwned<TService>(object serviceKey);
/// <summary>
/// Configure the component so that every dependent component or call to Resolve()
/// within a ILifetimeScope created by an owned instance gets the same, shared instance.
/// Dependent components in lifetime scopes that are children of the owned instance scope will
/// share the parent's instance. If no appropriate owned instance scope can be found in the
/// hierarchy an <see cref="DependencyResolutionException"/> is thrown.
/// </summary>
/// <typeparam name="TService">The service type provided by the component.</typeparam>
/// <param name="serviceKeys">Keys to associate with the component.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> InstancePerOwned<TService>(params object[] serviceKeys);
/// <summary>
/// Configure the component so that every dependent component or call to Resolve()
/// within a ILifetimeScope created by an owned instance gets the same, shared instance.
/// Dependent components in lifetime scopes that are children of the owned instance scope will
/// share the parent's instance. If no appropriate owned instance scope can be found in the
/// hierarchy an <see cref="DependencyResolutionException"/> is thrown.
/// </summary>
/// <param name="serviceKey">Key to associate with the component.</param>
/// <param name="serviceType">The service type provided by the component.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> InstancePerOwned(object serviceKey, Type serviceType);
/// <summary>
/// Configure the services that the component will provide. The generic parameter(s) to As()
/// will be exposed as TypedService instances.
/// </summary>
/// <typeparam name="TService">Service type.</typeparam>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> As<TService>()
where TService : notnull;
/// <summary>
/// Configure the services that the component will provide. The generic parameter(s) to As()
/// will be exposed as TypedService instances.
/// </summary>
/// <typeparam name="TService1">Service type.</typeparam>
/// <typeparam name="TService2">Service type.</typeparam>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> As<TService1, TService2>()
where TService1 : notnull
where TService2 : notnull;
/// <summary>
/// Configure the services that the component will provide. The generic parameter(s) to As()
/// will be exposed as TypedService instances.
/// </summary>
/// <typeparam name="TService1">Service type.</typeparam>
/// <typeparam name="TService2">Service type.</typeparam>
/// <typeparam name="TService3">Service type.</typeparam>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> As<TService1, TService2, TService3>()
where TService1 : notnull
where TService2 : notnull
where TService3 : notnull;
/// <summary>
/// Configure the services that the component will provide.
/// </summary>
/// <param name="services">Service types to expose.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> As(params Type[] services);
/// <summary>
/// Configure the services that the component will provide.
/// </summary>
/// <param name="services">Services to expose.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> As(params Service[] services);
/// <summary>
/// Provide a textual name that can be used to retrieve the component.
/// </summary>
/// <param name="serviceName">Named service to associate with the component.</param>
/// <param name="serviceType">The service type provided by the component.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> Named(string serviceName, Type serviceType);
/// <summary>
/// Provide a textual name that can be used to retrieve the component.
/// </summary>
/// <param name="serviceName">Named service to associate with the component.</param>
/// <typeparam name="TService">The service type provided by the component.</typeparam>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> Named<TService>(string serviceName)
where TService : notnull;
/// <summary>
/// Provide a key that can be used to retrieve the component.
/// </summary>
/// <param name="serviceKey">Key to associate with the component.</param>
/// <param name="serviceType">The service type provided by the component.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> Keyed(object serviceKey, Type serviceType);
/// <summary>
/// Provide a key that can be used to retrieve the component.
/// </summary>
/// <param name="serviceKey">Key to associate with the component.</param>
/// <typeparam name="TService">The service type provided by the component.</typeparam>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> Keyed<TService>(object serviceKey)
where TService : notnull;
/// <summary>
/// Add a handler for the Preparing event. This event allows manipulating of the parameters
/// that will be provided to the component.
/// </summary>
/// <param name="handler">The event handler.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OnPreparing(Action<PreparingEventArgs> handler);
/// <summary>
/// Add an async handler for the Preparing event. This event allows manipulating of the parameters
/// that will be provided to the component.
/// </summary>
/// <param name="handler">An event handler; the resolve process will not continue until the returned task completes.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OnPreparing(Func<PreparingEventArgs, ValueTask> handler);
/// <summary>
/// Add a handler for the Activating event.
/// </summary>
/// <param name="handler">The event handler.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OnActivating(Action<IActivatingEventArgs<TLimit>> handler);
/// <summary>
/// Add an async handler for the Activating event.
/// </summary>
/// <param name="handler">An event handler; the resolve process will not continue until the returned task completes.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OnActivating(Func<IActivatingEventArgs<TLimit>, ValueTask> handler);
/// <summary>
/// Add a handler for the Activated event.
/// </summary>
/// <param name="handler">The event handler.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OnActivated(Action<IActivatedEventArgs<TLimit>> handler);
/// <summary>
/// Add a handler for the Activated event.
/// </summary>
/// <param name="handler">An event handler; the resolve process will not continue until the returned task completes.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OnActivated(Func<IActivatedEventArgs<TLimit>, ValueTask> handler);
/// <summary>
/// Configure the component so that any properties whose types are registered in the
/// container and follow specific criteria will be wired to instances of the appropriate service.
/// </summary>
/// <param name="propertySelector">Selector to determine which properties should be injected.</param>
/// <param name="allowCircularDependencies">Determine if circular dependencies should be allowed or not.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> PropertiesAutowired(IPropertySelector propertySelector, bool allowCircularDependencies = false);
/// <summary>
/// Associates data with the component.
/// </summary>
/// <param name="key">Key by which the data can be located.</param>
/// <param name="value">The data value.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> WithMetadata(string key, object? value);
/// <summary>
/// Associates data with the component.
/// </summary>
/// <param name="properties">The extended properties to associate with the component.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> WithMetadata(IEnumerable<KeyValuePair<string, object?>> properties);
/// <summary>
/// Associates data with the component.
/// </summary>
/// <typeparam name="TMetadata">A type with properties whose names correspond to the
/// property names to configure.</typeparam>
/// <param name="configurationAction">
/// The action used to configure the metadata.
/// </param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> WithMetadata<TMetadata>(Action<MetadataConfiguration<TMetadata>> configurationAction);
}
}