-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathServiceRegistry.cs
559 lines (476 loc) · 16.8 KB
/
ServiceRegistry.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
using System;
using System.Collections.Generic;
using System.Linq;
using JasperFx.Core.Reflection;
using Lamar.IoC.Activation;
using Lamar.IoC.Instances;
using Lamar.IoC.Setters;
using Lamar.Scanning.Conventions;
using Microsoft.Extensions.DependencyInjection;
namespace Lamar;
public static class InstanceExtensions
{
public static T Named<T>(this T instance, string name) where T : Instance
{
instance.Name = name;
instance.IsExplicitlyNamed = true;
return instance;
}
public static T Scoped<T>(this T instance) where T : Instance
{
AssertNotObjectInstance(instance);
instance.Lifetime = ServiceLifetime.Scoped;
return instance;
}
private static void AssertNotObjectInstance<T>(T instance) where T : Instance
{
if (instance is ObjectInstance)
{
throw new InvalidOperationException(
"You cannot override the lifecycle of a direct object registration. Did you mean to use a Lambda registration instead?");
}
}
public static T Singleton<T>(this T instance) where T : Instance
{
instance.Lifetime = ServiceLifetime.Singleton;
return instance;
}
public static T Transient<T>(this T instance) where T : Instance
{
AssertNotObjectInstance(instance);
instance.Lifetime = ServiceLifetime.Transient;
return instance;
}
}
public partial class ServiceRegistry : List<ServiceDescriptor>, IServiceCollection
{
public ServiceRegistry()
{
}
public ServiceRegistry(IEnumerable<ServiceDescriptor> descriptors)
{
AddRange(descriptors);
}
internal IList<Type> RegistryTypes { get; set; } = new List<Type>();
/// <summary>
/// Configure Container-wide policies and conventions
/// </summary>
public PoliciesExpression Policies => new(this);
public static ServiceRegistry For(Action<ServiceRegistry> configuration)
{
var registry = new ServiceRegistry();
configuration(registry);
return registry;
}
/// <summary>
/// This method is a shortcut for specifying the default constructor and
/// setter arguments for a ImplementationType. ForConcreteType is shorthand for:
/// For[T]().Use[T].**************
/// when the ServiceType and ImplementationType are the same Type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public BuildWithExpression<T> ForConcreteType<T>() where T : class
{
var instance = For<T>().Use<T>();
return new BuildWithExpression<T>(instance);
}
public InstanceExpression<T> For<T>() where T : class
{
return new InstanceExpression<T>(this, ServiceLifetime.Transient);
}
public DescriptorExpression For(Type serviceType)
{
return new DescriptorExpression(serviceType, this);
}
/// <summary>
/// Shorthand equivalent to `For<T>().******.Singleton()`
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public InstanceExpression<T> ForSingletonOf<T>() where T : class
{
return new InstanceExpression<T>(this, ServiceLifetime.Singleton);
}
/// <summary>
/// Create an isolated type scanning registration policy
/// </summary>
/// <param name="scan"></param>
public void Scan(Action<IAssemblyScanner> scan)
{
var finder = new AssemblyScanner(this);
scan(finder);
finder.Start();
var descriptor = ServiceDescriptor.Singleton(finder);
Add(descriptor);
}
/// <summary>
/// Include the registrations from another ServiceRegistry
/// </summary>
/// <typeparam name="T"></typeparam>
public void IncludeRegistry<T>() where T : ServiceRegistry, new()
{
IncludeRegistry(new T());
}
/// <summary>
/// Include the registrations from another ServiceRegistry
/// </summary>
/// <param name="serviceRegistry"></param>
/// <typeparam name="T"></typeparam>
public void IncludeRegistry<T>(T serviceRegistry) where T : ServiceRegistry
{
Include(serviceRegistry);
}
/// <summary>
/// Include the registrations from another ServiceRegistry
/// </summary>
/// <param name="registry"></param>
/// <exception cref="ArgumentNullException"></exception>
public void Include(ServiceRegistry registry)
{
if (registry == null)
{
throw new ArgumentNullException(nameof(registry));
}
var type = registry.GetType();
if (type != typeof(ServiceRegistry))
{
if (RegistryTypes.Contains(type))
{
return;
}
AddRange(registry);
RegistryTypes.Add(type);
}
else
{
AddRange(registry);
}
}
/// <summary>
/// Tells Lamar that the service "T" will be injected into a built
/// container later. Used for framework support
/// </summary>
/// <typeparam name="T"></typeparam>
public void Injectable<T>() where T : class
{
For<T>().Use(new InjectedInstance<T>());
}
internal T[] FindAndRemovePolicies<T>() where T : ILamarPolicy
{
var policies = this
.Where(x => x.ServiceType == typeof(T) && x.ImplementationInstance != null)
.Select(x => x.ImplementationInstance)
.OfType<T>()
.ToArray();
RemoveAll(x => x.ServiceType == typeof(T));
return policies;
}
public InverseInstanceExpression<T> Use<T>() where T : class
{
return new InverseInstanceExpression<T>(this);
}
public ProvidedInstanceInverseInstanceExpression<T> Use<T>(T instance) where T : class
{
return new ProvidedInstanceInverseInstanceExpression<T>(this, instance);
}
/// <summary>
/// Define the constructor and setter arguments for the default T
/// </summary>
/// <typeparam name="T"></typeparam>
public class BuildWithExpression<T>
{
public BuildWithExpression(ConstructorInstance<T, T> instance)
{
Configure = instance;
}
public ConstructorInstance<T, T> Configure { get; }
}
public class DescriptorExpression
{
private readonly ServiceRegistry _parent;
private readonly Type _serviceType;
public DescriptorExpression(Type serviceType, ServiceRegistry parent)
{
_serviceType = serviceType;
_parent = parent;
}
public ConstructorInstance Use(Type concreteType)
{
var instance = new ConstructorInstance(_serviceType, concreteType, ServiceLifetime.Transient);
_parent.Add(instance);
return instance;
}
public ConstructorInstance Add(Type implementationType)
{
var instance = new ConstructorInstance(_serviceType, implementationType, ServiceLifetime.Transient);
_parent.Add(instance);
return instance;
}
/// <summary>
/// Register a custom Instance
/// </summary>
/// <param name="instance"></param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public void Use(Instance instance)
{
if (instance.ServiceType != _serviceType)
{
throw new ArgumentOutOfRangeException(nameof(instance),
$"The Instance.ServiceType {instance.ServiceType.FullNameInCode()} was not {_serviceType.FullNameInCode()}");
}
_parent.Add(instance);
}
/// <summary>
/// Decorate all registrations to the service type with the supplied decorator type
/// </summary>
/// <param name="decoratorType"></param>
public void DecorateAllWith(Type decoratorType)
{
var policy = new DecoratorPolicy(_serviceType, decoratorType);
_parent.Policies.DecorateWith(policy);
}
}
public class InstanceExpression<T> where T : class
{
private readonly ServiceLifetime? _lifetime;
private readonly ServiceRegistry _parent;
public InstanceExpression(ServiceRegistry parent, ServiceLifetime? lifetime)
{
_parent = parent;
_lifetime = lifetime;
}
public ConstructorInstance<TConcrete, T> Use<TConcrete>() where TConcrete : class, T
{
var instance = ConstructorInstance.For<T, TConcrete>();
if (_lifetime != null)
{
instance.Lifetime = _lifetime.Value;
}
_parent.Add(instance);
return instance;
}
/// <summary>
/// Register a custom instance
/// </summary>
/// <param name="instance"></param>
public void Use(Instance instance)
{
if (_lifetime != null)
{
instance.Lifetime = _lifetime.Value;
}
_parent.Add(instance);
}
/// <summary>
/// Fills in a default type implementation for a service type if there are no prior
/// registrations
/// </summary>
/// <typeparam name="TConcrete"></typeparam>
/// <exception cref="NotImplementedException"></exception>
public void UseIfNone<TConcrete>() where TConcrete : class, T
{
if (_parent.FindDefault<T>() == null)
{
Use<TConcrete>();
}
}
/// <summary>
/// Fills in a default type implementation for a service type if there are no prior
/// registrations
/// </summary>
/// <typeparam name="T"></typeparam>
public void UseIfNone(T service)
{
if (_parent.FindDefault<T>() == null)
{
Use(service);
}
}
/// <summary>
/// Delegates to Use<T>(), polyfill for StructureMap syntax
/// </summary>
/// <typeparam name="TConcrete"></typeparam>
public ConstructorInstance<TConcrete, T> Add<TConcrete>() where TConcrete : class, T
{
return Use<TConcrete>();
}
public ObjectInstance Use(T service)
{
var instance = new ObjectInstance(typeof(T), service);
_parent.Add(instance);
return instance;
}
public ObjectInstance Add(T instance)
{
return Use(instance);
}
public LambdaInstance<IServiceContext, T> Add(Func<IServiceContext, T> func)
{
var instance = LambdaInstance.For(func);
if (_lifetime != null)
{
instance.Lifetime = _lifetime.Value;
}
_parent.Add(instance);
return instance;
}
public LambdaInstance<IServiceContext, T> Use(Func<IServiceContext, T> func)
{
return Add(func);
}
/// <summary>
/// Decorates all instances of T with the concrete type TDecorator
/// </summary>
/// <typeparam name="TDecorator"></typeparam>
public void DecorateAllWith<TDecorator>() where TDecorator : T
{
var policy = new DecoratorPolicy(typeof(T), typeof(TDecorator));
_parent.AddSingleton<IDecoratorPolicy>(policy);
}
/// <summary>
/// Intercept the object being created and potentially replace it with a wrapped
/// version or another object. This will apply to every registration where the service
/// type is T or the implementation type could be cast to T
/// </summary>
/// <param name="interceptor"></param>
/// <returns></returns>
public void InterceptAll(Func<T, T> interceptor)
{
InterceptAll((s, x) => interceptor(x));
}
/// <summary>
/// Intercept the object being created and potentially replace it with a wrapped
/// version or another object. This will apply to every registration where the service
/// type is T or the implementation type could be cast to T
/// </summary>
/// <param name="interceptor"></param>
/// <returns></returns>
public void InterceptAll(Func<IServiceContext, T, T> interceptor)
{
var policy = new InterceptorPolicy<T>(interceptor);
_parent.Policies.Add(policy);
}
/// <summary>
/// Perform some action on the object being created at the time the object is created for the first time by Lamar.
/// This will apply to every registration where the service
/// type is T or the implementation type could be cast to T
/// </summary>
/// <param name="activator"></param>
/// <returns></returns>
public void OnCreationForAll(Action<IServiceContext, T> activator)
{
var policy = new ActivationPolicy<T>(activator);
_parent.Policies.Add(policy);
}
/// <summary>
/// Perform some action on the object being created at the time the object is created for the first time by Lamar.
/// This will apply to every registration where the service
/// type is T or the implementation type could be cast to T
/// </summary>
/// <param name="activator"></param>
/// <returns></returns>
public void OnCreationForAll(Action<T> activator)
{
OnCreationForAll((c, x) => activator(x));
}
}
public class PoliciesExpression
{
private readonly ServiceRegistry _parent;
internal PoliciesExpression(ServiceRegistry parent)
{
_parent = parent;
}
/// <summary>
/// Adds a new policy to this container
/// that can apply to every object instance created
/// by this container
/// </summary>
/// <param name="policy"></param>
public void Add(ILamarPolicy policy)
{
if (policy is IInstancePolicy ip)
{
_parent.AddSingleton(ip);
}
if (policy is IFamilyPolicy fp)
{
_parent.AddSingleton(fp);
}
if (policy is IRegistrationPolicy rp)
{
_parent.AddSingleton(rp);
}
if (policy is IDecoratorPolicy dp)
{
_parent.AddSingleton(dp);
}
if (policy is ISetterPolicy sp)
{
_parent.AddSingleton(sp);
}
}
/// <summary>
/// Adds a new policy to this container
/// that can apply to every object instance created
/// by this container
/// </summary>
public void Add<T>() where T : ILamarPolicy, new()
{
Add(new T());
}
/// <summary>
/// Register a strategy for automatically resolving "missing" families
/// when an unknown ServiceType is first encountered
/// </summary>
/// <param name="policy"></param>
public void OnMissingFamily(IFamilyPolicy policy)
{
_parent.AddSingleton(policy);
}
public void OnMissingFamily<T>() where T : IFamilyPolicy, new()
{
OnMissingFamily(new T());
}
/// <summary>
/// Register a strategy for applying decorators on existing registrations
/// </summary>
/// <typeparam name="T"></typeparam>
public void DecorateWith<T>() where T : IDecoratorPolicy, new()
{
DecorateWith(new T());
}
public void DecorateWith(IDecoratorPolicy policy)
{
_parent.AddSingleton(policy);
}
/// <summary>
/// Creates automatic "policies" for which public setters are considered mandatory
/// properties by StructureMap that will be "setter injected" as part of the
/// construction process.
/// </summary>
/// <param name="action"></param>
public void SetAllProperties(Action<SetterConvention> action)
{
var convention = new SetterConvention(this);
action(convention);
}
/// <summary>
/// Directs StructureMap to always inject dependencies into any and all public Setter properties
/// of the type TServiceType.
/// </summary>
/// <typeparam name="TType"></typeparam>
/// <returns></returns>
public InstanceExpression<TType> FillAllPropertiesOfType<TType>() where TType : class
{
Add(new LambdaSetterPolicy(prop => prop.PropertyType == typeof(TType)));
return _parent.For<TType>();
}
}
}
public enum DynamicAssemblySharing
{
Shared,
Individual
}