-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathComponentRegistration.cs
1140 lines (1042 loc) · 44.7 KB
/
ComponentRegistration.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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2004-2022 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
namespace Castle.MicroKernel.Registration
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using Castle.Core;
using Castle.Core.Configuration;
using Castle.Core.Internal;
using Castle.DynamicProxy;
using Castle.MicroKernel.ComponentActivator;
using Castle.MicroKernel.Context;
using Castle.MicroKernel.Handlers;
using Castle.MicroKernel.LifecycleConcerns;
using Castle.MicroKernel.Lifestyle.Scoped;
using Castle.MicroKernel.ModelBuilder;
using Castle.MicroKernel.ModelBuilder.Descriptors;
using Castle.MicroKernel.Registration.Interceptor;
using Castle.MicroKernel.Registration.Lifestyle;
using Castle.MicroKernel.Registration.Proxy;
/// <summary>
/// Registration for a single type as a component with the kernel.
/// <para />
/// You can create a new registration with the <see cref = "Component" /> factory.
/// </summary>
/// <typeparam name = "TService"> The service type </typeparam>
public class ComponentRegistration<TService> : IRegistration
where TService : class
{
private readonly List<IComponentModelDescriptor> descriptors = new List<IComponentModelDescriptor>();
private readonly List<Type> potentialServices = new List<Type>();
private readonly HashSet<Type> potentialServicesLookup = new HashSet<Type>();
private bool ifComponentRegisteredIgnore;
private Type implementation;
private ComponentName name;
private bool overwrite;
private bool registerNewServicesOnly;
private bool registered;
/// <summary>
/// Initializes a new instance of the <see cref = "ComponentRegistration{TService}" /> class.
/// </summary>
public ComponentRegistration() : this(typeof(TService))
{
}
/// <summary>
/// Initializes a new instance of the <see cref = "ComponentRegistration{TService}" /> class.
/// </summary>
public ComponentRegistration(params Type[] services)
{
Forward(services);
}
/// <summary>
/// The concrete type that implements the service.
/// <para />
/// To set the implementation, use <see cref = "ImplementedBy(System.Type)" /> .
/// </summary>
/// <value> The implementation of the service. </value>
public Type Implementation
{
get { return implementation; }
}
/// <summary>
/// Set the lifestyle of this component. For example singleton and transient (also known as 'factory').
/// </summary>
/// <value> The with lifestyle. </value>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public LifestyleGroup<TService> LifeStyle
{
get { return new LifestyleGroup<TService>(this); }
}
/// <summary>
/// The name of the component. Will become the key for the component in the kernel.
/// <para />
/// To set the name, use <see cref = "Named" /> .
/// <para />
/// If not set, the <see cref = "Type.FullName" /> of the <see cref = "Implementation" /> will be used as the key to register the component.
/// </summary>
/// <value> The name. </value>
public String Name
{
get
{
if (name == null)
{
return null;
}
return name.Name;
}
}
/// <summary>
/// Set proxy for this component.
/// </summary>
/// <value> The proxy. </value>
public ProxyGroup<TService> Proxy
{
get { return new ProxyGroup<TService>(this); }
}
protected internal IList<Type> Services
{
get { return potentialServices; }
}
protected internal int ServicesCount
{
get { return potentialServices.Count; }
}
internal bool IsOverWrite
{
get { return overwrite; }
}
/// <summary>
/// Set a custom <see cref = "IComponentActivator" /> which creates and destroys the component.
/// </summary>
/// <returns> </returns>
public ComponentRegistration<TService> Activator<TActivator>() where TActivator : IComponentActivator
{
return AddAttributeDescriptor("componentActivatorType", typeof(TActivator).AssemblyQualifiedName);
}
/// <summary>
/// Adds the attribute descriptor.
/// </summary>
/// <param name = "key"> The key. </param>
/// <param name = "value"> The value. </param>
/// <returns> </returns>
public ComponentRegistration<TService> AddAttributeDescriptor(string key, string value)
{
AddDescriptor(new AttributeDescriptor<TService>(key, value));
return this;
}
/// <summary>
/// Adds the descriptor.
/// </summary>
/// <param name = "descriptor"> The descriptor. </param>
/// <returns> </returns>
public ComponentRegistration<TService> AddDescriptor(IComponentModelDescriptor descriptor)
{
descriptors.Add(descriptor);
var componentDescriptor = descriptor as AbstractOverwriteableDescriptor<TService>;
if (componentDescriptor != null)
{
componentDescriptor.Registration = this;
}
return this;
}
/// <summary>
/// Creates an attribute descriptor.
/// </summary>
/// <param name = "key"> The attribute key. </param>
/// <returns> </returns>
public AttributeKeyDescriptor<TService> Attribute(string key)
{
return new AttributeKeyDescriptor<TService>(this, key);
}
/// <summary>
/// Apply more complex configuration to this component registration.
/// </summary>
/// <param name = "configNodes"> The config nodes. </param>
/// <returns> </returns>
public ComponentRegistration<TService> Configuration(params Node[] configNodes)
{
return AddDescriptor(new ConfigurationDescriptor(configNodes));
}
/// <summary>
/// Apply more complex configuration to this component registration.
/// </summary>
/// <param name = "configuration"> The configuration <see cref = "MutableConfiguration" /> . </param>
/// <returns> </returns>
public ComponentRegistration<TService> Configuration(IConfiguration configuration)
{
return AddDescriptor(new ConfigurationDescriptor(configuration));
}
/// <summary>
/// Defines additional dependencies for the component. Those can be any of <see cref = "ServiceOverride" />, <see cref = "Property" /> and <see cref = "Parameter" />. Use the static methods on
/// <see cref = "Dependency" /> class to define the dependencies. See the example attached.
/// </summary>
/// <param name = "dependency"> The dependency. </param>
/// <returns> </returns>
/// <example>
/// Artificial example showing how to specify a service override. See other methods on <see cref = "Dependency" /> class for more options.
/// <code>DependsOn(Dependency.OnComponent(typeof(IRepository), typeof(IntranetRepository)));</code>
/// </example>
public ComponentRegistration<TService> DependsOn(Dependency dependency)
{
return DependsOn(new[] { dependency });
}
/// <summary>
/// Defines additional dependencies for the component. Those can be any combibation of <see cref = "ServiceOverride" />, <see cref = "Property" /> and <see cref = "Parameter" />. Use the static methods
/// on <see cref = "Dependency" /> class to define the dependencies. See the example attached.
/// </summary>
/// <param name = "dependencies"> The dependencies. </param>
/// <returns> </returns>
/// <example>
/// Artificial example showing how to specify three different dependencies. If any of the methods shown is not self explanatory consult its documentation.
/// <code>DependsOn(Dependency.OnAppSettingsValue("connectionString", "intranet-connection-string"),
/// Dependency.OnComponent(typeof(IRepository), typeof(IntranetRepository)),
/// Dependency.OnValue("applicationName", "My Application"));</code>
/// </example>
public ComponentRegistration<TService> DependsOn(params Dependency[] dependencies)
{
if (dependencies == null || dependencies.Length == 0)
{
return this;
}
var serviceOverrides = new List<ServiceOverride>(dependencies.Length);
var properties = new List<Property>(dependencies.Length);
var parameters = new List<Parameter>(dependencies.Length);
foreach (var dependency in dependencies)
{
if (dependency.Accept(properties))
{
continue;
}
if (dependency.Accept(parameters))
{
continue;
}
if (dependency.Accept(serviceOverrides))
{
continue;
}
}
if (serviceOverrides.Count > 0)
{
AddDescriptor(new ServiceOverrideDescriptor(serviceOverrides.ToArray()));
}
if (properties.Count > 0)
{
AddDescriptor(new CustomDependencyDescriptor(properties.ToArray()));
}
if (parameters.Count > 0)
{
AddDescriptor(new ParametersDescriptor(parameters.ToArray()));
}
return this;
}
/// <summary>
/// Uses a dictionary of key/value pairs, to specify custom dependencies.
/// </summary>
public ComponentRegistration<TService> DependsOn(Arguments dependencies)
{
return AddDescriptor(new CustomDependencyDescriptor(dependencies));
}
/// <summary>
/// Uses a dictionary of key/value pairs, to specify custom dependencies.
/// </summary>
public ComponentRegistration<TService> DependsOn(IDictionary dependencies)
{
var arguments = new Arguments();
foreach (DictionaryEntry item in dependencies)
{
arguments.Add(item.Key, item.Value);
}
return DependsOn(arguments);
}
/// <summary>
/// Uses an (anonymous) object as a dictionary, to specify custom dependencies.
/// </summary>
public ComponentRegistration<TService> DependsOn(object dependenciesAsAnonymousType)
{
return DependsOn(new ReflectionBasedDictionaryAdapter(dependenciesAsAnonymousType));
}
/// <summary>
/// Allows custom dependencies to by defined dyncamically. Calling this overload is synonymous to using <see cref = "DynamicParameters(Castle.MicroKernel.Registration.DynamicParametersDelegate)" />
/// </summary>
/// <param name = "resolve"> The delegate used for providing dynamic parameters. </param>
/// <returns> </returns>
public ComponentRegistration<TService> DependsOn(DynamicParametersDelegate resolve)
{
return DynamicParameters((k, c, d) =>
{
resolve(k, d);
return null;
});
}
/// <summary>
/// Allows custom dependencies to by defined dynamically with releasing capability. Calling this overload is synonymous to using
/// <see cref = "DynamicParameters(Castle.MicroKernel.Registration.DynamicParametersResolveDelegate)" />
/// </summary>
/// <param name = "resolve"> The delegate used for providing dynamic parameters. </param>
/// <returns> </returns>
public ComponentRegistration<TService> DependsOn(DynamicParametersResolveDelegate resolve)
{
return DynamicParameters((k, c, d) => resolve(k, d));
}
/// <summary>
/// Allows custom dependencies to by defined dynamically with releasing capability. Calling this overload is synonymous to using
/// <see cref = "DynamicParameters(Castle.MicroKernel.Registration.DynamicParametersWithContextResolveDelegate)" />
/// </summary>
/// <param name = "resolve"> The delegate used for providing dynamic parameters. </param>
/// <returns> </returns>
/// <remarks>
/// Use <see cref = "CreationContext" /> when resolving components from <see cref = "IKernel" /> in order to detect cycles.
/// </remarks>
public ComponentRegistration<TService> DependsOn(DynamicParametersWithContextResolveDelegate resolve)
{
AddDescriptor(new DynamicParametersDescriptor(resolve));
return this;
}
/// <summary>
/// Allows custom dependencies to by defined dynamically.
/// </summary>
/// <param name = "resolve"> The delegate used for providing dynamic parameters. </param>
/// <returns> </returns>
public ComponentRegistration<TService> DynamicParameters(DynamicParametersDelegate resolve)
{
return DynamicParameters((k, c, d) =>
{
resolve(k, d);
return null;
});
}
/// <summary>
/// Allows custom dependencies to by defined dynamically with releasing capability.
/// </summary>
/// <param name = "resolve"> The delegate used for providing dynamic parameters. </param>
/// <returns> </returns>
public ComponentRegistration<TService> DynamicParameters(DynamicParametersResolveDelegate resolve)
{
return DynamicParameters((k, c, d) => resolve(k, d));
}
/// <summary>
/// Allows custom dependencies to by defined dynamically with releasing capability.
/// </summary>
/// <param name = "resolve"> The delegate used for providing dynamic parameters. </param>
/// <returns> </returns>
/// <remarks>
/// Use <see cref = "CreationContext" /> when resolving components from <see cref = "IKernel" /> in order to detect cycles.
/// </remarks>
public ComponentRegistration<TService> DynamicParameters(DynamicParametersWithContextResolveDelegate resolve)
{
AddDescriptor(new DynamicParametersDescriptor(resolve));
return this;
}
/// <summary>
/// Sets <see cref = "ComponentModel.ExtendedProperties" /> for this component.
/// </summary>
/// <param name = "properties"> The extended properties. </param>
/// <returns> </returns>
public ComponentRegistration<TService> ExtendedProperties(params Property[] properties)
{
return AddDescriptor(new ExtendedPropertiesDescriptor(properties));
}
/// <summary>
/// Sets <see cref = "ComponentModel.ExtendedProperties" /> for this component.
/// </summary>
/// <param name = "property"> The extended properties. </param>
/// <returns> </returns>
public ComponentRegistration<TService> ExtendedProperties(Property property)
{
return ExtendedProperties(new[] { property });
}
/// <summary>
/// Sets <see cref = "ComponentModel.ExtendedProperties" /> for this component.
/// </summary>
/// <param name = "anonymous"> The extendend properties as key/value pairs. </param>
/// <returns> </returns>
public ComponentRegistration<TService> ExtendedProperties(object anonymous)
{
return AddDescriptor(new ExtendedPropertiesDescriptor(new ReflectionBasedDictionaryAdapter(anonymous)));
}
/// <summary>
/// Adds <paramref name = "types" /> as additional services to be exposed by this component.
/// </summary>
/// <param name = "types"> The types to forward. </param>
/// <returns> </returns>
public ComponentRegistration<TService> Forward(params Type[] types)
{
return Forward((IEnumerable<Type>)types);
}
/// <summary>
/// Adds <typeparamref name = "TService2" /> as additional service to be exposed by this component.
/// </summary>
/// <typeparam name = "TService2"> The forwarded type. </typeparam>
/// <returns> The component registration. </returns>
public ComponentRegistration<TService> Forward<TService2>()
{
return Forward(new[] { typeof(TService2) });
}
/// <summary>
/// Adds <typeparamref name = "TService2" /> and <typeparamref name = "TService3" /> as additional services to be exposed by this component.
/// </summary>
/// <typeparam name = "TService2"> The first forwarded type. </typeparam>
/// <typeparam name = "TService3"> The second forwarded type. </typeparam>
/// <returns> The component registration. </returns>
public ComponentRegistration<TService> Forward<TService2, TService3>()
{
return Forward(new[] { typeof(TService2), typeof(TService3) });
}
/// <summary>
/// Adds <typeparamref name = "TService2" /> , <typeparamref name = "TService3" /> and <typeparamref name = "TService4" /> as additional services to be exposed by this component.
/// </summary>
/// <typeparam name = "TService2"> The first forwarded type. </typeparam>
/// <typeparam name = "TService3"> The second forwarded type. </typeparam>
/// <typeparam name = "TService4"> The third forwarded type. </typeparam>
/// <returns> The component registration. </returns>
public ComponentRegistration<TService> Forward<TService2, TService3, TService4>()
{
return Forward(new[] { typeof(TService2), typeof(TService3), typeof(TService4) });
}
/// <summary>
/// Adds <typeparamref name = "TService2" /> , <typeparamref name = "TService3" /> , <typeparamref name = "TService4" /> and <typeparamref name = "TService5" /> as additional services to be exposed by
/// this component.
/// </summary>
/// <typeparam name = "TService2"> The first forwarded type. </typeparam>
/// <typeparam name = "TService3"> The second forwarded type. </typeparam>
/// <typeparam name = "TService4"> The third forwarded type. </typeparam>
/// <typeparam name = "TService5"> The fourth forwarded type. </typeparam>
/// <returns> The component registration. </returns>
public ComponentRegistration<TService> Forward<TService2, TService3, TService4, TService5>()
{
return Forward(new[] { typeof(TService2), typeof(TService3), typeof(TService4), typeof(TService5) });
}
/// <summary>
/// Adds <paramref name = "types" /> as additional services to be exposed by this component.
/// </summary>
/// <param name = "types"> The types to forward. </param>
/// <returns> </returns>
public ComponentRegistration<TService> Forward(IEnumerable<Type> types)
{
foreach (var type in types)
{
ComponentServicesUtil.AddService(potentialServices, potentialServicesLookup, type);
}
return this;
}
/// <summary>
/// Sets the concrete type that implements the service to <typeparamref name = "TImpl" /> .
/// <para />
/// If not set, the class service type or first registered interface will be used as the implementation for this component.
/// </summary>
/// <typeparam name = "TImpl"> The type that is the implementation for the service. </typeparam>
/// <returns> </returns>
public ComponentRegistration<TService> ImplementedBy<TImpl>() where TImpl : TService
{
return ImplementedBy(typeof(TImpl));
}
/// <summary>
/// Sets the concrete type that implements the service to <paramref name = "type" /> .
/// <para />
/// If not set, the class service type or first registered interface will be used as the implementation for this component.
/// </summary>
/// <param name = "type"> The type that is the implementation for the service. </param>
/// <returns> </returns>
public ComponentRegistration<TService> ImplementedBy(Type type)
{
return ImplementedBy(type, null, null);
}
/// <summary>
/// Sets the concrete type that implements the service to <paramref name = "type" /> .
/// <para />
/// If not set, the class service type or first registered interface will be used as the implementation for this component.
/// </summary>
/// <param name = "type"> The type that is the implementation for the service. </param>
/// <param name = "genericImplementationMatchingStrategy"> Provides ability to close open generic service. Ignored when registering closed or non-generic component. </param>
/// <returns> </returns>
public ComponentRegistration<TService> ImplementedBy(Type type, IGenericImplementationMatchingStrategy genericImplementationMatchingStrategy)
{
return ImplementedBy(type, genericImplementationMatchingStrategy, null);
}
/// <summary>
/// Sets the concrete type that implements the service to <paramref name = "type" /> .
/// <para />
/// If not set, the class service type or first registered interface will be used as the implementation for this component.
/// </summary>
/// <param name = "type"> The type that is the implementation for the service. </param>
/// <param name = "genericServiceStrategy"> Provides ability to select if open generic component supports particular closed version of a service. </param>
/// <returns> </returns>
public ComponentRegistration<TService> ImplementedBy(Type type, IGenericServiceStrategy genericServiceStrategy)
{
return ImplementedBy(type, null, genericServiceStrategy);
}
/// <summary>
/// Sets the concrete type that implements the service to <paramref name = "type" /> .
/// <para />
/// If not set, the class service type or first registered interface will be used as the implementation for this component.
/// </summary>
/// <param name = "type"> The type that is the implementation for the service. </param>
/// <param name = "genericImplementationMatchingStrategy"> Provides ability to close open generic service. Ignored when registering closed or non-generic component. </param>
/// <param name = "genericServiceStrategy"> Provides ability to select if open generic component supports particular closed version of a service. </param>
/// <returns> </returns>
public ComponentRegistration<TService> ImplementedBy(Type type, IGenericImplementationMatchingStrategy genericImplementationMatchingStrategy, IGenericServiceStrategy genericServiceStrategy)
{
if (implementation != null && implementation != typeof(LateBoundComponent))
{
var message = String.Format("This component has already been assigned implementation {0}",
implementation.FullName);
throw new ComponentRegistrationException(message);
}
implementation = type;
if (genericImplementationMatchingStrategy != null)
{
ExtendedProperties(Property.ForKey(Constants.GenericImplementationMatchingStrategy).Eq(genericImplementationMatchingStrategy));
}
if (genericServiceStrategy != null)
{
ExtendedProperties(Property.ForKey(Constants.GenericServiceStrategy).Eq(genericServiceStrategy));
}
return this;
}
/// <summary>
/// Assigns an existing instance as the component for this registration.
/// </summary>
/// <param name = "instance"> The component instance. </param>
/// <returns> </returns>
public ComponentRegistration<TService> Instance(TService instance)
{
if (instance == null)
{
throw new ArgumentNullException(nameof(instance));
}
return ImplementedBy(instance.GetType())
.Activator<ExternalInstanceActivator>()
.ExtendedProperties(Property.ForKey("instance").Eq(instance));
}
/// <summary>
/// Set the interceptors for this component.
/// </summary>
/// <param name = "interceptors"> The interceptors. </param>
/// <returns> </returns>
public InterceptorGroup<TService> Interceptors(params InterceptorReference[] interceptors)
{
return new InterceptorGroup<TService>(this, interceptors);
}
/// <summary>
/// Set the interceptors for this component.
/// </summary>
/// <param name = "interceptors"> The interceptors. </param>
/// <returns> </returns>
public ComponentRegistration<TService> Interceptors(params Type[] interceptors)
{
var references = interceptors.ConvertAll(t => new InterceptorReference(t));
return AddDescriptor(new InterceptorDescriptor(references));
}
/// <summary>
/// Set the interceptor for this component.
/// </summary>
/// <returns> </returns>
public ComponentRegistration<TService> Interceptors<TInterceptor>() where TInterceptor : IInterceptor
{
return AddDescriptor(new InterceptorDescriptor(new[] { new InterceptorReference(typeof(TInterceptor)) }));
}
/// <summary>
/// Set the interceptor for this component.
/// </summary>
/// <returns> </returns>
public ComponentRegistration<TService> Interceptors<TInterceptor1, TInterceptor2>()
where TInterceptor1 : IInterceptor
where TInterceptor2 : IInterceptor
{
return Interceptors<TInterceptor1>().Interceptors<TInterceptor2>();
}
/// <summary>
/// Set the interceptor for this component.
/// </summary>
/// <returns> </returns>
public ComponentRegistration<TService> Interceptors(params string[] keys)
{
var interceptors = keys.ConvertAll(InterceptorReference.ForKey);
return AddDescriptor(new InterceptorDescriptor(interceptors));
}
/// <summary>
/// Sets component lifestyle to specified one.
/// </summary>
/// <returns> </returns>
public ComponentRegistration<TService> LifestyleCustom(Type customLifestyleType)
{
return LifeStyle.Custom(customLifestyleType);
}
/// <summary>
/// Sets component lifestyle to specified one.
/// </summary>
/// <returns> </returns>
public ComponentRegistration<TService> LifestyleCustom<TLifestyleManager>()
where TLifestyleManager : ILifestyleManager, new()
{
return LifeStyle.Custom<TLifestyleManager>();
}
/// <summary>
/// Sets component lifestyle to per thread.
/// </summary>
/// <returns> </returns>
public ComponentRegistration<TService> LifestylePerThread()
{
return LifeStyle.PerThread;
}
/// <summary>
/// Sets component lifestyle to scoped per explicit scope. If <paramref name = "scopeAccessorType" /> is provided, it will be used to access scope for the component. Otherwise the default scope accessor
/// will be used.
/// </summary>
/// <returns> </returns>
public ComponentRegistration<TService> LifestyleScoped(Type scopeAccessorType = null)
{
return LifeStyle.Scoped(scopeAccessorType);
}
/// <summary>
/// Sets component lifestyle to scoped per explicit scope.
/// </summary>
/// <returns> </returns>
public ComponentRegistration<TService> LifestyleScoped<TScopeAccessor>() where TScopeAccessor : IScopeAccessor, new()
{
return LifestyleScoped(typeof(TScopeAccessor));
}
/// <summary>
/// Sets component lifestyle to scoped per farthest component on the resolution stack where implementation type is assignable to <typeparamref name = "TBaseForRoot" /> .
/// </summary>
/// <returns> </returns>
public ComponentRegistration<TService> LifestyleBoundTo<TBaseForRoot>() where TBaseForRoot : class
{
return LifeStyle.BoundTo<TBaseForRoot>();
}
/// <summary>
/// Sets component lifestyle to scoped per nearest component on the resolution stack where implementation type is assignable to <typeparamref name = "TBaseForRoot" /> .
/// </summary>
/// <returns> </returns>
public ComponentRegistration<TService> LifestyleBoundToNearest<TBaseForRoot>() where TBaseForRoot : class
{
return LifeStyle.BoundToNearest<TBaseForRoot>();
}
/// <summary>
/// Sets component lifestyle to scoped per scope determined by <paramref name = "scopeRootBinder" />
/// </summary>
/// <param name = "scopeRootBinder"> Custom algorithm for selection which component higher up the resolution stack should be the root of the lifetime scope for current component's instances. The delegate
/// will be invoked when current component is about to be resolved and will be passed set of handlers to components higher up the resolution stack. It ought to return one which it designages as the root
/// which shall scope the lifetime of current component's instance, or <c>null</c> </param>
/// <returns> </returns>
public ComponentRegistration<TService> LifestyleBoundTo(Func<IHandler[], IHandler> scopeRootBinder)
{
return LifeStyle.BoundTo(scopeRootBinder);
}
/// <summary>
/// Sets component lifestyle to pooled. If <paramref name = "initialSize" /> or <paramref name = "maxSize" /> are not set default values will be used.
/// </summary>
/// <returns> </returns>
public ComponentRegistration<TService> LifestylePooled(int? initialSize = null, int? maxSize = null)
{
return LifeStyle.PooledWithSize(initialSize, maxSize);
}
/// <summary>
/// Sets component lifestyle to singleton.
/// </summary>
/// <returns> </returns>
public ComponentRegistration<TService> LifestyleSingleton()
{
return LifeStyle.Singleton;
}
/// <summary>
/// Sets component lifestyle to transient.
/// </summary>
/// <returns> </returns>
public ComponentRegistration<TService> LifestyleTransient()
{
return LifeStyle.Transient;
}
/// <summary>
/// Set a name of this registration. This is required if you have multiple components for a given service and want to be able to resolve some specific ones. Then you'd provide the name so that Windsor
/// knows which one of the bunch you know. Otherwise don't bother setting the name.
/// <para />
/// If not set, the <see cref = "Type.FullName" /> of the <see cref = "Implementation" /> will be used as the key to register the component.
/// </summary>
/// <param name = "name"> The name of this registration. </param>
/// <returns> </returns>
/// <remarks>
/// Names have to be globally unique in the scope of the container.
/// </remarks>
public ComponentRegistration<TService> Named(String name)
{
if (this.name != null)
{
var message = String.Format("This component has already been assigned name '{0}'", this.name.Name);
throw new ComponentRegistrationException(message);
}
if (name == null)
{
return this;
}
this.name = new ComponentName(name, true);
return this;
}
/// <summary>
/// This method as opposed to <see cref = "Named" /> should be used by tools like facilities when the name is not provided by the user, but autogenerated and user has no interest in seing this name, for
/// example in diagnostics reports. Set a name of this registration. This is required if you have multiple components for a given service and want to be able to resolve some specific ones. Then you'd
/// provide the name so that Windsor knows which one of the bunch you know. Otherwise don't bother setting the name.
/// <para />
/// If not set, the <see cref = "Type.FullName" /> of the <see cref = "Implementation" /> will be used as the key to register the component.
/// </summary>
/// <param name = "name"> The name of this registration. </param>
/// <returns> </returns>
/// <remarks>
/// Names have to be globally unique in the scope of the container.
/// </remarks>
public ComponentRegistration<TService> NamedAutomatically(String name)
{
if (this.name != null)
{
var message = String.Format("This component has already been assigned name '{0}'", this.name);
throw new ComponentRegistrationException(message);
}
this.name = new ComponentName(name, false);
return this;
}
/// <summary>
/// Stores a set of <see cref = "LifecycleActionDelegate{T}" /> which will be invoked when the component is created and before it's returned from the container.
/// </summary>
/// <param name = "actions"> A set of actions to be executed right after the component is created and before it's returned from the container. </param>
public ComponentRegistration<TService> OnCreate(params Action<TService>[] actions)
{
if (actions != null && actions.Length != 0)
{
return OnCreate(actions.ConvertAll(a => new LifecycleActionDelegate<TService>((_, o) => a(o))));
}
return this;
}
/// <summary>
/// Stores a set of <see cref = "LifecycleActionDelegate{T}" /> which will be invoked when the component is created and before it's returned from the container.
/// </summary>
/// <param name = "actions"> A set of actions to be executed right after the component is created and before it's returned from the container. </param>
public ComponentRegistration<TService> OnCreate(params LifecycleActionDelegate<TService>[] actions)
{
if (actions != null && actions.Length != 0)
{
var action = (LifecycleActionDelegate<TService>)Delegate.Combine(actions);
AddDescriptor(new OnCreateComponentDescriptor<TService>(action));
}
return this;
}
/// <summary>
/// Stores a set of <see cref = "LifecycleActionDelegate{T}" /> which will be invoked when the component is created and before it's returned from the container.
/// </summary>
/// <param name = "actions"> A set of actions to be executed right after the component is created and before it's returned from the container. </param>
public ComponentRegistration<TService> OnDestroy(params Action<TService>[] actions)
{
if (actions != null && actions.Length != 0)
{
return OnDestroy(actions.ConvertAll(a => new LifecycleActionDelegate<TService>((_, o) => a(o))));
}
return this;
}
/// <summary>
/// Stores a set of <see cref = "LifecycleActionDelegate{T}" /> which will be invoked when the component is destroyed which means when it's released or it's lifetime scope ends. Notice that usage of this
/// method will cause instances of the component to be tracked, even if they wouldn't be otherwise.
/// </summary>
/// <param name = "actions"> A set of actions to be executed when the component is destroyed. </param>
public ComponentRegistration<TService> OnDestroy(params LifecycleActionDelegate<TService>[] actions)
{
if (actions != null && actions.Length != 0)
{
var action = (LifecycleActionDelegate<TService>)Delegate.Combine(actions);
AddDescriptor(new OnDestroyComponentDescriptor<TService>(action));
}
return this;
}
/// <summary>
/// Services that are already present in the container will be skipped. If no new service is left the registration will not happen at all.
/// </summary>
/// <returns> </returns>
public ComponentRegistration<TService> OnlyNewServices()
{
registerNewServicesOnly = true;
return this;
}
/// <summary>
/// With the overwrite.
/// </summary>
/// <returns> </returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public ComponentRegistration<TService> OverWrite()
{
overwrite = true;
return this;
}
/// <summary>
/// Sets the interceptor selector for this component.
/// </summary>
/// <param name = "selector"> </param>
/// <returns> </returns>
public ComponentRegistration<TService> SelectInterceptorsWith(IInterceptorSelector selector)
{
return SelectInterceptorsWith(s => s.Instance(selector));
}
/// <summary>
/// Sets the interceptor selector for this component.
/// </summary>
/// <param name = "selector"> </param>
/// <returns> </returns>
public ComponentRegistration<TService> SelectInterceptorsWith(Action<ItemRegistration<IInterceptorSelector>> selector)
{
var registration = new ItemRegistration<IInterceptorSelector>();
selector.Invoke(registration);
return AddDescriptor(new InterceptorSelectorDescriptor(registration.Item));
}
/// <summary>
/// Uses a factory to instantiate the component
/// </summary>
/// <typeparam name = "TFactory"> Factory type. This factory has to be registered in the kernel. </typeparam>
/// <typeparam name = "TServiceImpl"> Implementation type. </typeparam>
/// <param name = "factory"> Factory invocation </param>
/// <returns> </returns>
public ComponentRegistration<TService> UsingFactory<TFactory, TServiceImpl>(Func<TFactory, TServiceImpl> factory)
where TServiceImpl : TService
{
return UsingFactoryMethod(kernel => factory.Invoke(kernel.Resolve<TFactory>()));
}
/// <summary>
/// Uses a factory method to instantiate the component.
/// </summary>
/// <typeparam name = "TImpl"> Implementation type </typeparam>
/// <param name = "factoryMethod"> Factory method </param>
/// <param name = "managedExternally"> When set to <c>true</c> container will not assume ownership of this component, will not track it not apply and lifecycle concerns to it. </param>
/// <returns> </returns>
public ComponentRegistration<TService> UsingFactoryMethod<TImpl>(Func<TImpl> factoryMethod,
bool managedExternally = false)
where TImpl : TService
{
return UsingFactoryMethod((k, m, c) => factoryMethod(), managedExternally);
}
/// <summary>
/// Uses a factory method to instantiate the component.
/// </summary>
/// <typeparam name = "TImpl"> Implementation type </typeparam>
/// <param name = "factoryMethod"> Factory method </param>
/// <param name = "managedExternally"> When set to <c>true</c> container will not assume ownership of this component, will not track it not apply and lifecycle concerns to it. </param>
/// <returns> </returns>
public ComponentRegistration<TService> UsingFactoryMethod<TImpl>(Func<IKernel, TImpl> factoryMethod,
bool managedExternally = false)
where TImpl : TService
{
return UsingFactoryMethod((k, m, c) => factoryMethod(k), managedExternally);
}
/// <summary>
/// Uses a factory method to instantiate the component.
/// </summary>
/// <typeparam name = "TImpl"> Implementation type </typeparam>
/// <param name = "factoryMethod"> Factory method </param>
/// <param name = "managedExternally"> When set to <c>true</c> container will not assume ownership of this component, will not track it not apply and lifecycle concerns to it. </param>
/// <returns> </returns>
public ComponentRegistration<TService> UsingFactoryMethod<TImpl>(
Func<IKernel, ComponentModel, CreationContext, TImpl> factoryMethod,
bool managedExternally = false)
where TImpl : TService
{
Activator<FactoryMethodActivator<TImpl>>()
.ExtendedProperties(Property.ForKey("factoryMethodDelegate").Eq(factoryMethod));
if (managedExternally)
{
ExtendedProperties(Property.ForKey("factory.managedExternally").Eq(managedExternally));
}
if (implementation == null &&
(potentialServices.First().GetTypeInfo().IsClass == false || potentialServices.First().GetTypeInfo().IsSealed == false))
{
implementation = typeof(LateBoundComponent);
}
return this;
}
/// <summary>
/// Uses a factory method to instantiate the component.
/// </summary>
/// <typeparam name = "TImpl"> Implementation type </typeparam>
/// <param name = "factoryMethod"> Factory method </param>
/// <returns> </returns>
public ComponentRegistration<TService> UsingFactoryMethod<TImpl>(Func<IKernel, CreationContext, TImpl> factoryMethod)
where TImpl : TService
{
return UsingFactoryMethod((k, m, c) => factoryMethod(k, c));
}
internal void RegisterOptionally()
{
ifComponentRegisteredIgnore = true;
}
private Type[] FilterServices(IKernel kernel)
{
var services = new List<Type>(potentialServices);
if (registerNewServicesOnly)
{
services.RemoveAll(kernel.HasComponent);
}
return services.ToArray();
}
private IComponentModelDescriptor[] GetContributors(Type[] services)
{
var list = new List<IComponentModelDescriptor>
{
new ServicesDescriptor(services),
new DefaultsDescriptor(name, implementation),
};
list.AddRange(descriptors);
return list.ToArray();
}
private bool SkipRegistration(IKernelInternal internalKernel, ComponentModel componentModel)
{
return ifComponentRegisteredIgnore && internalKernel.HasComponent(componentModel.Name);
}
/// <summary>
/// Registers this component with the <see cref = "IKernel" /> .
/// </summary>
/// <param name = "kernel"> The kernel. </param>
void IRegistration.Register(IKernelInternal kernel)
{
if (registered)