forked from NancyFx/Nancy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurableBootstrapper.cs
More file actions
1885 lines (1638 loc) · 95.7 KB
/
Copy pathConfigurableBootstrapper.cs
File metadata and controls
1885 lines (1638 loc) · 95.7 KB
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
namespace Nancy.Testing
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Localization;
using Nancy.Bootstrapper;
using Nancy.Conventions;
using Nancy.Culture;
using Nancy.Diagnostics;
using Nancy.ErrorHandling;
using Nancy.ModelBinding;
using Nancy.Routing;
using Nancy.Routing.Constraints;
using Nancy.Routing.Trie;
using Nancy.Security;
using Nancy.TinyIoc;
using Nancy.ViewEngines;
using Responses.Negotiation;
using Nancy.Validation;
/// <summary>
/// A Nancy bootstrapper that can be configured with either Type or Instance overrides for all Nancy types.
/// </summary>
public class ConfigurableBootstrapper : NancyBootstrapperWithRequestContainerBase<TinyIoCContainer>, IPipelines, INancyModuleCatalog
{
private readonly List<object> registeredTypes;
private readonly List<InstanceRegistration> registeredInstances;
private readonly NancyInternalConfiguration configuration;
private readonly ConfigurableModuleCatalog catalog;
private bool enableAutoRegistration;
private DiagnosticsConfiguration diagnosticConfiguration;
private readonly List<Action<TinyIoCContainer, IPipelines>> applicationStartupActions;
private readonly List<Action<TinyIoCContainer, IPipelines, NancyContext>> requestStartupActions;
private readonly Assembly nancyAssembly = typeof(NancyEngine).Assembly;
/// <summary>
/// Test project name suffixes that will be stripped from the test name project
/// in order to try and resolve the name of the assembly that is under test so
/// that all of its references can be loaded into the application domain.
/// </summary>
public static IList<string> TestAssemblySuffixes = new[] { "test", "tests", "unittests", "specs", "specifications" };
private bool allDiscoveredModules;
private bool autoRegistrations = true;
/// <summary>
/// Initializes a new instance of the <see cref="ConfigurableBootstrapper"/> class.
/// </summary>
public ConfigurableBootstrapper()
: this(null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ConfigurableBootstrapper"/> class.
/// </summary>
/// <param name="configuration">The configuration that should be used by the bootstrapper.</param>
public ConfigurableBootstrapper(Action<ConfigurableBootstrapperConfigurator> configuration)
{
this.catalog = new ConfigurableModuleCatalog();
this.configuration = NancyInternalConfiguration.Default;
this.registeredTypes = new List<object>();
this.registeredInstances = new List<InstanceRegistration>();
this.applicationStartupActions = new List<Action<TinyIoCContainer, IPipelines>>();
this.requestStartupActions = new List<Action<TinyIoCContainer, IPipelines, NancyContext>>();
var testAssembly =
Assembly.GetCallingAssembly();
PerformConventionBasedAssemblyLoading(testAssembly);
if (configuration != null)
{
var configurator =
new ConfigurableBootstrapperConfigurator(this);
configurator.StatusCodeHandler<PassThroughStatusCodeHandler>();
configuration.Invoke(configurator);
}
}
private static void PerformConventionBasedAssemblyLoading(Assembly testAssembly)
{
var testAssemblyName =
testAssembly.GetName().Name;
LoadReferencesForAssemblyUnderTest(testAssemblyName);
}
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
base.ApplicationStartup(container, pipelines);
foreach (var action in this.applicationStartupActions)
{
action.Invoke(container, pipelines);
}
}
protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
{
base.RequestStartup(container, pipelines, context);
foreach (var action in this.requestStartupActions)
{
action.Invoke(container, pipelines, context);
}
}
/// <summary>
/// Get all NancyModule implementation instances
/// </summary>
/// <param name="context">The current context</param>
/// <returns>An <see cref="IEnumerable{T}"/> instance containing <see cref="INancyModule"/> instances.</returns>
public new IEnumerable<INancyModule> GetAllModules(NancyContext context)
{
return base.GetAllModules(context).Union(this.catalog.GetAllModules(context));
}
/// <summary>
/// Retrieve a specific module instance from the container
/// </summary>
/// <param name="container">Container to use</param>
/// <param name="moduleType">Type of the module</param>
/// <returns>INancyModule instance</returns>
protected override INancyModule GetModule(TinyIoCContainer container, Type moduleType)
{
var module =
this.catalog.GetModule(moduleType, null);
if (module != null)
{
return module;
}
container.Register(typeof(INancyModule), moduleType);
return container.Resolve<INancyModule>();
}
private IEnumerable<ModuleRegistration> GetModuleRegistrations()
{
return this.registeredTypes.Where(x => x is ModuleRegistration).Cast<ModuleRegistration>();
}
private IEnumerable<TypeRegistration> GetTypeRegistrations()
{
return this.registeredTypes.Where(x => x is TypeRegistration).Cast<TypeRegistration>();
}
private IEnumerable<CollectionTypeRegistration> GetCollectionTypeRegistrations()
{
return this.registeredTypes.Where(x => x.GetType() == typeof(CollectionTypeRegistration)).Cast<CollectionTypeRegistration>();
}
private static void LoadReferencesForAssemblyUnderTest(string testAssemblyName)
{
if (!TestAssemblySuffixes.Any(x => GetSafePathExtension(testAssemblyName).Equals("." + x, StringComparison.OrdinalIgnoreCase)))
{
return;
}
var testAssemblyNameWithoutExtension =
Path.GetFileNameWithoutExtension(testAssemblyName);
var testAssemblyPath =
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Concat(testAssemblyNameWithoutExtension, ".dll"));
if (File.Exists(testAssemblyPath))
{
AppDomainAssemblyTypeScanner.LoadAssemblies(AppDomain.CurrentDomain.BaseDirectory, string.Concat(testAssemblyNameWithoutExtension, ".dll"));
var assemblyUnderTest = AppDomain.CurrentDomain
.GetAssemblies()
.FirstOrDefault(x => x.GetName().Name.Equals(testAssemblyNameWithoutExtension, StringComparison.OrdinalIgnoreCase));
if (assemblyUnderTest != null)
{
foreach (var referencedAssembly in assemblyUnderTest.GetReferencedAssemblies())
{
AppDomainAssemblyTypeScanner.LoadAssemblies(AppDomain.CurrentDomain.BaseDirectory, string.Concat(referencedAssembly.Name, ".dll"));
}
}
}
}
private static string GetSafePathExtension(string name)
{
return Path.GetExtension(name) ?? String.Empty;
}
private IEnumerable<Type> Resolve<T>()
{
var types = this.GetTypeRegistrations()
.Where(x => x.RegistrationType == typeof(T))
.Select(x => x.ImplementationType)
.ToList();
return (types.Any()) ? types : null;
}
/// <summary>
/// Nancy internal configuration
/// </summary>
protected override sealed NancyInternalConfiguration InternalConfiguration
{
get { return this.configuration; }
}
/// <summary>
/// Nancy conventions
/// </summary>
protected override NancyConventions Conventions
{
get
{
var conventions = this.registeredInstances
.Where(x => x.RegistrationType == typeof(NancyConventions))
.Select(x => x.Implementation)
.Cast<NancyConventions>()
.FirstOrDefault();
return conventions ?? base.Conventions;
}
}
/// <summary>
/// Gets all available module types
/// </summary>
protected override IEnumerable<ModuleRegistration> Modules
{
get
{
var moduleRegistrations =
this.GetModuleRegistrations().ToList();
if (moduleRegistrations.Any())
{
return moduleRegistrations;
}
return this.allDiscoveredModules ? base.Modules : new ModuleRegistration[] { };
}
}
/// <summary>
/// Gets the available view engine types
/// </summary>
protected override IEnumerable<Type> ViewEngines
{
get { return this.Resolve<IViewEngine>() ?? base.ViewEngines; }
}
/// <summary>
/// Gets the available custom model binders
/// </summary>
protected override IEnumerable<Type> ModelBinders
{
get { return this.Resolve<IModelBinder>() ?? base.ModelBinders; }
}
/// <summary>
/// Gets the available custom type converters
/// </summary>
protected override IEnumerable<Type> TypeConverters
{
get { return this.Resolve<ITypeConverter>() ?? base.TypeConverters; }
}
/// <summary>
/// Gets the available custom body deserializers
/// </summary>
protected override IEnumerable<Type> BodyDeserializers
{
get { return this.Resolve<IBodyDeserializer>() ?? base.BodyDeserializers; }
}
/// <summary>
/// Gets all startup tasks
/// </summary>
protected override IEnumerable<Type> ApplicationStartupTasks
{
get { return this.Resolve<IApplicationStartup>() ?? base.ApplicationStartupTasks; }
}
protected override DiagnosticsConfiguration DiagnosticsConfiguration
{
get
{
return this.diagnosticConfiguration ?? base.DiagnosticsConfiguration;
}
}
/// <summary>
/// Gets the root path provider
/// </summary>
protected override IRootPathProvider RootPathProvider
{
get { return new DefaultRootPathProvider(); }
}
/// <summary>
/// Configures the container using AutoRegister followed by registration
/// of default INancyModuleCatalog and IRouteResolver.
/// </summary>
/// <param name="container">Container instance</param>
protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
if (this.enableAutoRegistration)
{
container.AutoRegister();
this.RegisterBootstrapperTypes(container);
}
RegisterTypesInternal(this.ApplicationContainer, this.GetTypeRegistrations());
RegisterCollectionTypesInternal(this.ApplicationContainer, this.GetCollectionTypeRegistrations());
RegisterInstancesInternal(this.ApplicationContainer, this.registeredInstances);
}
/// <summary>
/// Creates a per request child/nested container
/// </summary>
/// <param name="context">Current context</param>
/// <returns>Request container instance</returns>
protected override TinyIoCContainer CreateRequestContainer(NancyContext context)
{
return this.ApplicationContainer.GetChildContainer();
}
/// <summary>
/// Retrieve all module instances from the container
/// </summary>
/// <param name="container">Container to use</param>
/// <returns>Collection of INancyModule instances</returns>
protected override IEnumerable<INancyModule> GetAllModules(TinyIoCContainer container)
{
return container.ResolveAll<INancyModule>(false);
}
/// <summary>
/// Gets the application level container
/// </summary>
/// <returns>Container instance</returns>
protected override TinyIoCContainer GetApplicationContainer()
{
return new TinyIoCContainer();
}
/// <summary>
/// Resolve INancyEngine
/// </summary>
/// <returns>INancyEngine implementation</returns>
protected override INancyEngine GetEngineInternal()
{
try
{
return this.ApplicationContainer.Resolve<INancyEngine>();
}
catch (InvalidOperationException ex)
{
throw new InvalidOperationException(
"Something went wrong when trying to satisfy one of the dependencies during composition, make sure that you've registered all new dependencies in the container and specified either a module to test, or set AllDiscoveredModules in the ConfigurableBootstrapper. Inspect the innerexception for more details.",
ex.InnerException);
}
}
/// <summary>
/// Gets the diagnostics for initialisation
/// </summary>
/// <returns>IDiagnostics implementation</returns>
protected override IDiagnostics GetDiagnostics()
{
return this.ApplicationContainer.Resolve<IDiagnostics>();
}
/// <summary>
/// Gets all registered startup tasks
/// </summary>
/// <returns>An <see cref="IEnumerable{T}"/> instance containing <see cref="IApplicationStartup"/> instances. </returns>
protected override IEnumerable<IApplicationStartup> GetApplicationStartupTasks()
{
return this.ApplicationContainer.ResolveAll<IApplicationStartup>(false);
}
/// <summary>
/// Gets all registered request startup tasks
/// </summary>
/// <returns>An <see cref="System.Collections.Generic.IEnumerable{T}"/> instance containing <see cref="IRequestStartup"/> instances.</returns>
protected override IEnumerable<IRequestStartup> RegisterAndGetRequestStartupTasks(TinyIoCContainer container, Type[] requestStartupTypes)
{
container.RegisterMultiple(typeof(IRequestStartup), requestStartupTypes);
return container.ResolveAll<IRequestStartup>(false);
}
/// <summary>
/// Gets all registered application registration tasks
/// </summary>
/// <returns>An <see cref="IEnumerable{T}"/> instance containing <see cref="IRegistrations"/> instances.</returns>
protected override IEnumerable<IRegistrations> GetRegistrationTasks()
{
if (this.autoRegistrations)
{
return this.ApplicationContainer.ResolveAll<IRegistrations>(false);
}
return this.ApplicationContainer.ResolveAll<IRegistrations>(false)
.Where(x => x.GetType().Assembly == nancyAssembly);
}
/// <summary>
/// Register the bootstrapper's implemented types into the container.
/// This is necessary so a user can pass in a populated container but not have
/// to take the responsibility of registering things like INancyModuleCatalog manually.
/// </summary>
/// <param name="applicationContainer">Application container to register into</param>
protected override void RegisterBootstrapperTypes(TinyIoCContainer applicationContainer)
{
var moduleCatalog = this.registeredInstances
.Where(x => x.RegistrationType == typeof(INancyModuleCatalog))
.Select(x => x.Implementation)
.Cast<INancyModuleCatalog>()
.FirstOrDefault() ?? this;
applicationContainer.Register<INancyModuleCatalog>(moduleCatalog);
}
/// <summary>
/// Register the default implementations of internally used types into the container as singletons
/// </summary>
/// <param name="container">Container to register into</param>
/// <param name="typeRegistrations">Type registrations to register</param>
protected override void RegisterTypes(TinyIoCContainer container, IEnumerable<TypeRegistration> typeRegistrations)
{
var configuredTypes =
this.GetTypeRegistrations().ToList();
var filtered = typeRegistrations
.Where(x => !configuredTypes.Any(y => y.RegistrationType == x.RegistrationType))
.Where(x => !this.registeredInstances.Any(y => y.RegistrationType == x.RegistrationType));
RegisterTypesInternal(container, filtered);
}
private static void RegisterTypesInternal(TinyIoCContainer container, IEnumerable<TypeRegistration> filtered)
{
foreach (var typeRegistration in filtered)
{
container.Register(typeRegistration.RegistrationType, typeRegistration.ImplementationType).AsSingleton();
}
}
/// <summary>
/// Register the various collections into the container as singletons to later be resolved
/// by IEnumerable{Type} constructor dependencies.
/// </summary>
/// <param name="container">Container to register into</param>
/// <param name="collectionTypeRegistrations">Collection type registrations to register</param>
protected override void RegisterCollectionTypes(TinyIoCContainer container, IEnumerable<CollectionTypeRegistration> collectionTypeRegistrations)
{
var configuredCollectionTypes =
this.GetCollectionTypeRegistrations().ToList();
var filtered = collectionTypeRegistrations
.Where(x => !configuredCollectionTypes.Any(y => y.RegistrationType == x.RegistrationType));
RegisterCollectionTypesInternal(container, filtered);
}
private static void RegisterCollectionTypesInternal(TinyIoCContainer container, IEnumerable<CollectionTypeRegistration> filtered)
{
foreach (var collectionTypeRegistration in filtered)
{
container.RegisterMultiple(collectionTypeRegistration.RegistrationType,
collectionTypeRegistration.ImplementationTypes);
}
}
/// <summary>
/// Register the given instances into the container
/// </summary>
/// <param name="container">Container to register into</param>
/// <param name="instanceRegistrations">Instance registration types</param>
protected override void RegisterInstances(TinyIoCContainer container, IEnumerable<InstanceRegistration> instanceRegistrations)
{
var configuredInstanceRegistrations = this.GetTypeRegistrations();
var fileteredInstanceRegistrations = instanceRegistrations
.Where(x => !this.registeredInstances.Any(y => y.RegistrationType == x.RegistrationType))
.Where(x => !configuredInstanceRegistrations.Any(y => y.RegistrationType == x.RegistrationType))
.ToList();
RegisterInstancesInternal(container, fileteredInstanceRegistrations);
}
private static void RegisterInstancesInternal(TinyIoCContainer container, IEnumerable<InstanceRegistration> fileteredInstanceRegistrations)
{
foreach (var instanceRegistration in fileteredInstanceRegistrations)
{
container.Register(
instanceRegistration.RegistrationType,
instanceRegistration.Implementation);
}
}
/// <summary>
/// Register the given module types into the request container
/// </summary>
/// <param name="container">Container to register into</param>
/// <param name="moduleRegistrationTypes">NancyModule types</param>
protected override void RegisterRequestContainerModules(TinyIoCContainer container, IEnumerable<ModuleRegistration> moduleRegistrationTypes)
{
foreach (var moduleRegistrationType in moduleRegistrationTypes)
{
container.Register(
typeof(INancyModule),
moduleRegistrationType.ModuleType,
moduleRegistrationType.ModuleType.FullName).
AsSingleton();
}
}
/// <summary>
/// <para>
/// The pre-request hook
/// </para>
/// <para>
/// The PreRequest hook is called prior to processing a request. If a hook returns
/// a non-null response then processing is aborted and the response provided is
/// returned.
/// </para>
/// </summary>
public BeforePipeline BeforeRequest
{
get { return this.ApplicationPipelines.BeforeRequest; }
set { this.ApplicationPipelines.BeforeRequest = value; }
}
/// <summary>
/// <para>
/// The post-request hook
/// </para>
/// <para>
/// The post-request hook is called after the response is created. It can be used
/// to rewrite the response or add/remove items from the context.
/// </para>
/// </summary>
public AfterPipeline AfterRequest
{
get { return this.ApplicationPipelines.AfterRequest; }
set { this.ApplicationPipelines.AfterRequest = value; }
}
/// <summary>
/// <para>
/// The error hook
/// </para>
/// <para>
/// The error hook is called if an exception is thrown at any time during the pipeline.
/// If no error hook exists a standard InternalServerError response is returned
/// </para>
/// </summary>
public ErrorPipeline OnError
{
get { return this.ApplicationPipelines.OnError; }
set { this.ApplicationPipelines.OnError = value; }
}
/// <summary>
/// Provides an API for configuring a <see cref="ConfigurableBootstrapper"/> instance.
/// </summary>
public class ConfigurableBootstrapperConfigurator
{
private readonly ConfigurableBootstrapper bootstrapper;
/// <summary>
/// Initializes a new instance of the <see cref="ConfigurableBootstrapperConfigurator"/> class.
/// </summary>
/// <param name="bootstrapper">The bootstrapper that should be configured.</param>
public ConfigurableBootstrapperConfigurator(ConfigurableBootstrapper bootstrapper)
{
this.bootstrapper = bootstrapper;
this.Diagnostics<DisabledDiagnostics>();
}
public ConfigurableBootstrapperConfigurator AllDiscoveredModules()
{
this.bootstrapper.allDiscoveredModules = true;
return this;
}
public ConfigurableBootstrapperConfigurator Binder(IBinder binder)
{
this.bootstrapper.registeredInstances.Add(
new InstanceRegistration(typeof(IBinder), binder));
return this;
}
public ConfigurableBootstrapperConfigurator Assembly(string pattern)
{
AppDomainAssemblyTypeScanner.LoadAssemblies(AppDomain.CurrentDomain.BaseDirectory, pattern);
return this;
}
/// <summary>
/// Configures the bootstrapper to create an <see cref="IBinder"/> instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of the <see cref="IBinder"/> that the bootstrapper should use.</typeparam>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator Binder<T>() where T : IBinder
{
this.bootstrapper.configuration.Binder = typeof(T);
return this;
}
/// <summary>
/// Configures the bootstrapper to use the provided instance of <see cref="INancyContextFactory"/>.
/// </summary>
/// <param name="contextFactory">The <see cref="INancyContextFactory"/> instance that should be used by the bootstrapper.</param>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator ContextFactory(INancyContextFactory contextFactory)
{
this.bootstrapper.registeredInstances.Add(
new InstanceRegistration(typeof(INancyContextFactory), contextFactory));
return this;
}
/// <summary>
/// Configures the bootstrapper to create an <see cref="INancyContextFactory"/> instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of the <see cref="INancyContextFactory"/> that the bootstrapper should use.</typeparam>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator ContextFactory<T>() where T : INancyContextFactory
{
this.bootstrapper.configuration.ContextFactory = typeof(T);
return this;
}
/// <summary>
/// Configures the bootstrapper to use the provided type as a dependency.
/// </summary>
/// <param name="type">The type of the dependency that should be used registered with the bootstrapper.</param>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator Dependency<T>(Type type)
{
this.bootstrapper.registeredTypes.Add(new TypeRegistration(typeof(T), type));
return this;
}
/// <summary>
/// Configures the bootstrapper to register the specified type as a dependency.
/// </summary>
/// <typeparam name="T">The type of the dependency that should be registered with the bootstrapper.</typeparam>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
/// <remarks>This method will register the type for all the interfaces it implements and the type itself.</remarks>
public ConfigurableBootstrapperConfigurator Dependency<T>()
{
this.bootstrapper.registeredTypes.Add(new TypeRegistration(typeof(T), typeof(T)));
foreach (var interfaceType in GetSafeInterfaces(typeof(T)))
{
this.bootstrapper.registeredTypes.Add(new TypeRegistration(interfaceType, typeof(T)));
}
return this;
}
/// <summary>
/// Configures the bootstrapper to use the provided instance as a dependency.
/// </summary>
/// <param name="instance">The dependency instance that should be used registered with the bootstrapper.</param>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
/// <remarks>This method will register the instance for all the interfaces it implements and the type itself.</remarks>
public ConfigurableBootstrapperConfigurator Dependency<T>(T instance)
{
this.bootstrapper.registeredInstances.Add(new InstanceRegistration(typeof(T), instance));
var interfacesToRegisterBy = GetSafeInterfaces(instance.GetType()).Where(i => !i.Equals(typeof(T)));
foreach (var interfaceType in interfacesToRegisterBy)
{
this.bootstrapper.registeredInstances.Add(new InstanceRegistration(interfaceType, instance));
}
return this;
}
private static IEnumerable<Type> GetSafeInterfaces(Type type)
{
return type.GetInterfaces().Where(x => x != typeof(IDisposable));
}
/// <summary>
/// Configures the bootstrapper to register the specified type as a dependency.
/// </summary>
/// <typeparam name="T">The type that the dependencies should be registered as.</typeparam>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator Dependency<T>(object instance)
{
this.bootstrapper.registeredInstances.Add(new InstanceRegistration(typeof(T), instance));
return this;
}
/// <summary>
/// Configures the bootstrapper to register the specified instances as a dependencies.
/// </summary>
/// <param name="dependencies">The instances of the dependencies that should be registered with the bootstrapper.</param>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator Dependencies(params object[] dependencies)
{
foreach (var dependency in dependencies)
{
this.Dependency(dependency);
}
return this;
}
/// <summary>
/// Configures the bootstrapper to register the specified types and instances as a dependencies.
/// </summary>
/// <param name="dependencies">An array of maps between the interfaces and instances that should be registered with the bootstrapper.</param>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator MappedDependencies<T, K>(IEnumerable<Tuple<T, K>> dependencies)
where T : Type
where K : class
{
foreach (var dependency in dependencies)
{
this.bootstrapper.registeredInstances.Add(
new InstanceRegistration(dependency.Item1, dependency.Item2));
}
return this;
}
/// <summary>
/// Configures the bootstrapper to register the specified instances as a dependencies.
/// </summary>
/// <param name="dependencies">The instances of the dependencies that should be registered with the bootstrapper.</param>
/// <typeparam name="T">The type that the dependencies should be registered as.</typeparam>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator Dependencies<T>(params object[] dependencies)
{
foreach (var dependency in dependencies)
{
this.Dependency<T>(dependency);
}
return this;
}
/// <summary>
/// Configures the bootstrapper to use the provided types as a dependency.
/// </summary>
/// <param name="dependencies">The types that should be used registered as dependencies with the bootstrapper.</param>
/// <typeparam name="T">The type that the dependencies should be registered as.</typeparam>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator Dependencies<T>(params Type[] dependencies)
{
foreach (var dependency in dependencies)
{
this.Dependency<T>(dependency);
}
return this;
}
/// <summary>
/// Enables the auto registration behavior of the bootstrapper
/// </summary>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator EnableAutoRegistration()
{
this.bootstrapper.enableAutoRegistration = true;
return this;
}
/// <summary>
/// Configures the bootstrapper to use the provided instance of <see cref="IStatusCodeHandler"/>.
/// </summary>
/// <param name="statusCodeHandlers">The <see cref="IStatusCodeHandler"/> types that should be used by the bootstrapper.</param>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator StatusCodeHandlers(params Type[] statusCodeHandlers)
{
this.bootstrapper.configuration.StatusCodeHandlers = new List<Type>(statusCodeHandlers);
return this;
}
/// <summary>
/// Configures the bootstrapper to create an <see cref="IStatusCodeHandler"/> instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of the <see cref="IStatusCodeHandler"/> that the bootstrapper should use.</typeparam>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator StatusCodeHandler<T>() where T : IStatusCodeHandler
{
this.bootstrapper.configuration.StatusCodeHandlers = new List<Type>(new[] { typeof(T) });
return this;
}
/// <summary>
/// Configures the bootstrapper to use the provided instance of <see cref="IFieldNameConverter"/>.
/// </summary>
/// <param name="fieldNameConverter">The <see cref="IFieldNameConverter"/> instance that should be used by the bootstrapper.</param>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator FieldNameConverter(IFieldNameConverter fieldNameConverter)
{
this.bootstrapper.registeredInstances.Add(
new InstanceRegistration(typeof(IFieldNameConverter), fieldNameConverter));
return this;
}
/// <summary>
/// Configures the bootstrapper to create an <see cref="IFieldNameConverter"/> instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of the <see cref="IFieldNameConverter"/> that the bootstrapper should use.</typeparam>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator FieldNameConverter<T>() where T : IFieldNameConverter
{
this.bootstrapper.configuration.FieldNameConverter = typeof(T);
return this;
}
/// <summary>
/// Configures the bootstrapper to use the provided instance of <see cref="IModelBinderLocator"/>.
/// </summary>
/// <param name="modelBinderLocator">The <see cref="IModelBinderLocator"/> instance that should be used by the bootstrapper.</param>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator ModelBinderLocator(IModelBinderLocator modelBinderLocator)
{
this.bootstrapper.registeredInstances.Add(
new InstanceRegistration(typeof(IModelBinderLocator), modelBinderLocator));
return this;
}
/// <summary>
/// Configures the bootstrapper to create an <see cref="IModelBinderLocator"/> instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of the <see cref="IModelBinderLocator"/> that the bootstrapper should use.</typeparam>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator ModelBinderLocator<T>() where T : IModelBinderLocator
{
this.bootstrapper.configuration.ModelBinderLocator = typeof(T);
return this;
}
/// <summary>
/// Configures the bootstrapper to create a <see cref="INancyModule"/> instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of the <see cref="INancyModule"/> that the bootstrapper should use.</typeparam>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator Module<T>() where T : INancyModule
{
return this.Modules(typeof(T));
}
/// <summary>
/// Configures the bootstrapper to register the provided <see cref="INancyModule"/> instance.
/// </summary>
/// <param name="module">The <see cref="INancyModule"/> instance to register.</param>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator Module(INancyModule module)
{
this.bootstrapper.catalog.RegisterModuleInstance(module);
return this;
}
/// <summary>
/// Configures the bootstrapper to create <see cref="INancyModule"/> instances of the specified types.
/// </summary>
/// <param name="modules">The types of the <see cref="INancyModule"/> that the bootstrapper should use.</param>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator Modules(params Type[] modules)
{
var moduleRegistrations =
from module in modules
select new ModuleRegistration(module);
this.bootstrapper.registeredTypes.AddRange(moduleRegistrations);
return this;
}
/// <summary>
/// Configures the bootstrapper to use the provided instance of <see cref="INancyEngine"/>.
/// </summary>
/// <param name="engine">The <see cref="INancyEngine"/> instance that should be used by the bootstrapper.</param>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator NancyEngine(INancyEngine engine)
{
this.bootstrapper.registeredInstances.Add(
new InstanceRegistration(typeof(INancyEngine), engine));
return this;
}
/// <summary>
/// Configures the bootstrapper to create an <see cref="INancyEngine"/> instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of the <see cref="INancyEngine"/> that the bootstrapper should use.</typeparam>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator NancyEngine<T>() where T : INancyEngine
{
this.bootstrapper.configuration.NancyEngine = typeof(T);
return this;
}
/// <summary>
/// Configures the bootstrapper to use the provided instance of <see cref="INancyModuleBuilder"/>.
/// </summary>
/// <param name="nancyModuleBuilder">The <see cref="INancyModuleBuilder"/> instance that should be used by the bootstrapper.</param>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator NancyModuleBuilder(INancyModuleBuilder nancyModuleBuilder)
{
this.bootstrapper.registeredInstances.Add(
new InstanceRegistration(typeof(INancyModuleBuilder), nancyModuleBuilder));
return this;
}
/// <summary>
/// Configures the bootstrapper to create an <see cref="INancyModuleBuilder"/> instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of the <see cref="INancyModuleBuilder"/> that the bootstrapper should use.</typeparam>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator NancyModuleBuilder<T>() where T : INancyModuleBuilder
{
this.bootstrapper.configuration.NancyModuleBuilder = typeof(T);
return this;
}
/// <summary>
/// Configures the bootstrapper to use the provided instance of <see cref="IRenderContextFactory"/>.
/// </summary>
/// <param name="renderContextFactory">The <see cref="IRenderContextFactory"/> instance that should be used by the bootstrapper.</param>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator RenderContextFactory(IRenderContextFactory renderContextFactory)
{
this.bootstrapper.registeredInstances.Add(
new InstanceRegistration(typeof(IRenderContextFactory), renderContextFactory));
return this;
}
/// <summary>
/// Configures the bootstrapper to create an <see cref="IRenderContextFactory"/> instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of the <see cref="IRenderContextFactory"/> that the bootstrapper should use.</typeparam>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator RenderContextFactory<T>() where T : IRenderContextFactory
{
this.bootstrapper.configuration.RenderContextFactory = typeof(T);
return this;
}
/// <summary>
/// Configures the bootstrapper to use the provided instance of <see cref="IRequestTraceFactory"/>.
/// </summary>
/// <param name="requestTraceFactory">The <see cref="IRequestTraceFactory"/> instance that should be used by the bootstrapper.</param>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator RequestTraceFactory(IRequestTraceFactory requestTraceFactory)
{
this.bootstrapper.registeredInstances.Add(
new InstanceRegistration(typeof(IRequestTraceFactory), requestTraceFactory));
return this;
}
/// <summary>
/// Configures the bootstrapper to create an <see cref="IRequestTraceFactory"/> instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of the <see cref="IRequestTraceFactory"/> that the bootstrapper should use.</typeparam>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator RequestTraceFactory<T>() where T : IRequestTraceFactory
{
this.bootstrapper.configuration.RequestTraceFactory = typeof(T);
return this;
}
/// <summary>
/// Configures the bootstrapper to use the provided instance of <see cref="IResponseFormatterFactory"/>.
/// </summary>
/// <param name="responseFormatterFactory">The <see cref="IResponseFormatterFactory"/> instance that should be used by the bootstrapper.</param>
/// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
public ConfigurableBootstrapperConfigurator ResponseFormatterFactory(IResponseFormatterFactory responseFormatterFactory)
{
this.bootstrapper.registeredInstances.Add(
new InstanceRegistration(typeof(IResponseFormatterFactory), responseFormatterFactory));
return this;
}