This repository has been archived by the owner on Apr 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
NinjectNancyBootstrapper.cs
229 lines (210 loc) · 10.5 KB
/
NinjectNancyBootstrapper.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
namespace Nancy.Bootstrappers.Ninject
{
using System;
using System.Collections.Generic;
using global::Ninject;
using global::Ninject.Extensions.ChildKernel;
using global::Ninject.Infrastructure;
using Bootstrapper;
using Configuration;
using Diagnostics;
/// <summary>
/// Nancy bootstrapper for the Ninject container.
/// </summary>
public abstract class NinjectNancyBootstrapper : NancyBootstrapperWithRequestContainerBase<IKernel>
{
/// <summary>
/// Gets the diagnostics for intialisation
/// </summary>
/// <returns>An <see cref="IDiagnostics"/> implementation</returns>
protected override IDiagnostics GetDiagnostics()
{
return this.ApplicationContainer.Get<IDiagnostics>();
}
/// <summary>
/// Gets all registered application startup tasks
/// </summary>
/// <returns>An <see cref="System.Collections.Generic.IEnumerable{T}"/> instance containing <see cref="IApplicationStartup"/> instances. </returns>
protected override IEnumerable<IApplicationStartup> GetApplicationStartupTasks()
{
return this.ApplicationContainer.GetAll<IApplicationStartup>();
}
/// <summary>
/// Gets all registered request startup tasks
/// </summary>
/// <returns>An <see cref="IEnumerable{T}"/> instance containing <see cref="IRequestStartup"/> instances.</returns>
protected override IEnumerable<IRequestStartup> RegisterAndGetRequestStartupTasks(IKernel container, Type[] requestStartupTypes)
{
foreach (var requestStartupType in requestStartupTypes)
{
container.Bind(typeof(IRequestStartup)).To(requestStartupType).InSingletonScope();
}
return container.GetAll<IRequestStartup>();
}
/// <summary>
/// Gets all registered application registration tasks
/// </summary>
/// <returns>An <see cref="System.Collections.Generic.IEnumerable{T}"/> instance containing <see cref="IRegistrations"/> instances.</returns>
protected override IEnumerable<IRegistrations> GetRegistrationTasks()
{
return this.ApplicationContainer.GetAll<IRegistrations>();
}
/// <summary>
/// Get INancyEngine
/// </summary>
/// <returns>An <see cref="INancyEngine"/> implementation</returns>
protected override sealed INancyEngine GetEngineInternal()
{
return this.ApplicationContainer.Get<INancyEngine>();
}
/// <summary>
/// Gets the <see cref="INancyEnvironmentConfigurator"/> used by th.
/// </summary>
/// <returns>An <see cref="INancyEnvironmentConfigurator"/> instance.</returns>
protected override INancyEnvironmentConfigurator GetEnvironmentConfigurator()
{
return this.ApplicationContainer.Get<INancyEnvironmentConfigurator>();
}
/// <summary>
/// Get the <see cref="INancyEnvironment" /> instance.
/// </summary>
/// <returns>An configured <see cref="INancyEnvironment" /> instance.</returns>
/// <remarks>The boostrapper must be initialised (<see cref="INancyBootstrapper.Initialise" />) prior to calling this.</remarks>
public override INancyEnvironment GetEnvironment()
{
return this.ApplicationContainer.Get<INancyEnvironment>();
}
/// <summary>
/// Registers an <see cref="INancyEnvironment"/> instance in the container.
/// </summary>
/// <param name="container">The container to register into.</param>
/// <param name="environment">The <see cref="INancyEnvironment"/> instance to register.</param>
protected override void RegisterNancyEnvironment(IKernel container, INancyEnvironment environment)
{
container.Bind<INancyEnvironment>().ToConstant(environment);
}
/// <summary>
/// Create a default, unconfigured, container
/// </summary>
/// <returns>Container instance</returns>
protected override IKernel GetApplicationContainer()
{
return new StandardKernel(new[] { new FactoryModule() });
}
/// <summary>
/// Bind 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 <see cref="INancyModuleCatalog"/> manually.
/// </summary>
/// <param name="applicationContainer">Application container to register into</param>
protected override sealed void RegisterBootstrapperTypes(IKernel applicationContainer)
{
applicationContainer.Bind<INancyModuleCatalog>().ToConstant(this);
}
/// <summary>
/// Bind 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 sealed void RegisterTypes(IKernel container, IEnumerable<TypeRegistration> typeRegistrations)
{
foreach (var typeRegistration in typeRegistrations)
{
switch (typeRegistration.Lifetime)
{
case Lifetime.Transient:
container.Bind(typeRegistration.RegistrationType).To(typeRegistration.ImplementationType).InTransientScope();
break;
case Lifetime.Singleton:
container.Bind(typeRegistration.RegistrationType).To(typeRegistration.ImplementationType).InSingletonScope();
break;
case Lifetime.PerRequest:
throw new InvalidOperationException("Unable to directly register a per request lifetime.");
default:
throw new ArgumentOutOfRangeException();
}
}
}
/// <summary>
/// Bind 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 sealed void RegisterCollectionTypes(IKernel container, IEnumerable<CollectionTypeRegistration> collectionTypeRegistrations)
{
foreach (var collectionTypeRegistration in collectionTypeRegistrations)
{
foreach (var implementationType in collectionTypeRegistration.ImplementationTypes)
{
switch (collectionTypeRegistration.Lifetime)
{
case Lifetime.Transient:
container.Bind(collectionTypeRegistration.RegistrationType).To(implementationType).InTransientScope();
break;
case Lifetime.Singleton:
container.Bind(collectionTypeRegistration.RegistrationType).To(implementationType).InSingletonScope();
break;
case Lifetime.PerRequest:
throw new InvalidOperationException("Unable to directly register a per request lifetime.");
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
/// <summary>
/// Bind the given module types into the container
/// </summary>
/// <param name="container">Container to register into</param>
/// <param name="moduleRegistrationTypes"><see cref="INancyModule"/> types</param>
protected override sealed void RegisterRequestContainerModules(IKernel container, IEnumerable<ModuleRegistration> moduleRegistrationTypes)
{
foreach (var moduleRegistrationType in moduleRegistrationTypes)
{
container.Bind(typeof(INancyModule)).To(moduleRegistrationType.ModuleType).Named(moduleRegistrationType.ModuleType.FullName);
}
}
/// <summary>
/// Bind 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(IKernel container, IEnumerable<InstanceRegistration> instanceRegistrations)
{
foreach (var instanceRegistration in instanceRegistrations)
{
container.Bind(instanceRegistration.RegistrationType).ToConstant(instanceRegistration.Implementation);
}
}
/// <summary>
/// Creates a per request child/nested container
/// </summary>
/// <param name="context">Current context</param>
/// <returns>Request container instance</returns>
protected override IKernel CreateRequestContainer(NancyContext context)
{
return new ChildKernel(this.ApplicationContainer, new NinjectSettings { DefaultScopeCallback = StandardScopeCallbacks.Singleton });
}
/// <summary>
/// Retrieve all module instances from the container
/// </summary>
/// <param name="container">Container to use</param>
/// <returns>Collection of <see cref="INancyModule"/> instances</returns>
protected override sealed IEnumerable<INancyModule> GetAllModules(IKernel container)
{
return container.GetAll<INancyModule>();
}
/// <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>An <see cref="INancyModule"/> instance</returns>
protected override INancyModule GetModule(IKernel container, Type moduleType)
{
container.Bind<INancyModule>().To(moduleType);
return container.Get(moduleType) as INancyModule;
}
}
}