-
Notifications
You must be signed in to change notification settings - Fork 530
/
BindingBuilder.cs
279 lines (248 loc) · 13.7 KB
/
BindingBuilder.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
// -------------------------------------------------------------------------------------------------
// <copyright file="BindingBuilder.cs" company="Ninject Project Contributors">
// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved.
// Copyright (c) 2010-2020 Ninject Project Contributors. All rights reserved.
//
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
// You may not use this file except in compliance with one of the Licenses.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// or
// http://www.microsoft.com/opensource/licenses.mspx
//
// 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.
// </copyright>
// -------------------------------------------------------------------------------------------------
namespace Ninject.Planning.Bindings
{
using System;
using System.Linq.Expressions;
using Ninject.Activation;
using Ninject.Activation.Providers;
using Ninject.Infrastructure;
using Ninject.Parameters;
using Ninject.Selection.Heuristics;
using Ninject.Syntax;
/// <summary>
/// Provides a root for the fluent syntax associated with an <see cref="BindingConfiguration"/>.
/// </summary>
public class BindingBuilder
{
/// <summary>
/// Initializes a new instance of the <see cref="BindingBuilder"/> class.
/// </summary>
/// <param name="bindingConfiguration">The binding to build.</param>
/// <param name="planner">The <see cref="IPlanner"/> component.</param>
/// <param name="constructorScorer">The <see cref="IConstructorScorer"/> component.</param>
/// <param name="serviceNames">The names of the services.</param>
/// <exception cref="ArgumentNullException"><paramref name="bindingConfiguration"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="planner"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="constructorScorer"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="serviceNames"/> is <see langword="null"/>.</exception>
public BindingBuilder(
IBindingConfiguration bindingConfiguration,
IPlanner planner,
IConstructorScorer constructorScorer,
string serviceNames)
{
Ensure.ArgumentNotNull(bindingConfiguration, nameof(bindingConfiguration));
Ensure.ArgumentNotNull(planner, nameof(planner));
Ensure.ArgumentNotNull(constructorScorer, nameof(constructorScorer));
Ensure.ArgumentNotNull(serviceNames, nameof(serviceNames));
this.BindingConfiguration = bindingConfiguration;
this.Planner = planner;
this.ConstructorScorer = constructorScorer;
this.ServiceNames = serviceNames;
}
/// <summary>
/// Gets the binding being built.
/// </summary>
public IBindingConfiguration BindingConfiguration { get; private set; }
/// <summary>
/// Gets the <see cref="IPlanner"/> component.
/// </summary>
protected IPlanner Planner { get; private set; }
/// <summary>
/// Gets the <see cref="IConstructorScorer"/> component.
/// </summary>
protected IConstructorScorer ConstructorScorer { get; private set; }
/// <summary>
/// Gets the names of the services.
/// </summary>
/// <value>The names of the services.</value>
protected string ServiceNames { get; private set; }
/// <summary>
/// Indicates that the service should be bound to the specified implementation type.
/// </summary>
/// <typeparam name="TImplementation">The implementation type.</typeparam>
/// <returns>The fluent syntax.</returns>
protected IBindingWhenInNamedWithOrOnSyntax<TImplementation> InternalTo<TImplementation>()
{
return this.InternalTo<TImplementation>(typeof(TImplementation));
}
/// <summary>
/// Indicates that the service should be bound to the specified implementation type.
/// </summary>
/// <typeparam name="T">The type of the returned syntax.</typeparam>
/// <param name="implementation">The implementation type.</param>
/// <returns>The fluent syntax.</returns>
protected IBindingWhenInNamedWithOrOnSyntax<T> InternalTo<T>(Type implementation)
{
this.BindingConfiguration.ProviderCallback = ctx => new StandardProvider(implementation, this.Planner, this.ConstructorScorer);
this.BindingConfiguration.Target = BindingTarget.Type;
return new BindingConfigurationBuilder<T>(this.BindingConfiguration, this.ServiceNames);
}
/// <summary>
/// Indicates that the service should be bound to the specified constant value.
/// </summary>
/// <typeparam name="TImplementation">The type of the implementation.</typeparam>
/// <param name="value">The constant value.</param>
/// <returns>The fluent syntax.</returns>
protected IBindingWhenInNamedWithOrOnSyntax<TImplementation> InternalToConfiguration<TImplementation>(TImplementation value)
{
this.BindingConfiguration.ProviderCallback = ctx => new ConstantProvider<TImplementation>(value);
this.BindingConfiguration.Target = BindingTarget.Constant;
this.BindingConfiguration.ScopeCallback = StandardScopeCallbacks.Singleton;
return new BindingConfigurationBuilder<TImplementation>(this.BindingConfiguration, this.ServiceNames);
}
/// <summary>
/// Indicates that the service should be bound to the specified callback method.
/// </summary>
/// <typeparam name="TImplementation">The type of the implementation.</typeparam>
/// <param name="method">The method.</param>
/// <returns>The fluent syntax.</returns>
protected IBindingWhenInNamedWithOrOnSyntax<TImplementation> InternalToMethod<TImplementation>(Func<IContext, TImplementation> method)
{
this.BindingConfiguration.ProviderCallback = ctx => new CallbackProvider<TImplementation>(method);
this.BindingConfiguration.Target = BindingTarget.Method;
return new BindingConfigurationBuilder<TImplementation>(this.BindingConfiguration, this.ServiceNames);
}
/// <summary>
/// Indicates that the service should be bound to the specified provider.
/// </summary>
/// <typeparam name="TImplementation">The type of the implementation.</typeparam>
/// <param name="provider">The provider.</param>
/// <returns>The fluent syntax.</returns>
protected IBindingWhenInNamedWithOrOnSyntax<TImplementation> InternalToProvider<TImplementation>(IProvider<TImplementation> provider)
{
this.BindingConfiguration.ProviderCallback = ctx => provider;
this.BindingConfiguration.Target = BindingTarget.Provider;
return new BindingConfigurationBuilder<TImplementation>(this.BindingConfiguration, this.ServiceNames);
}
/// <summary>
/// Indicates that the service should be bound to an instance of the specified provider type.
/// The instance will be activated via the kernel when an instance of the service is activated.
/// </summary>
/// <typeparam name="TProvider">The type of provider to activate.</typeparam>
/// <typeparam name="TImplementation">The type of the implementation.</typeparam>
/// <returns>The fluent syntax.</returns>
protected IBindingWhenInNamedWithOrOnSyntax<TImplementation> ToProviderInternal<TProvider, TImplementation>()
where TProvider : IProvider
{
this.BindingConfiguration.ProviderCallback = ctx => ctx.Kernel.Get<TProvider>();
this.BindingConfiguration.Target = BindingTarget.Provider;
return new BindingConfigurationBuilder<TImplementation>(this.BindingConfiguration, this.ServiceNames);
}
/// <summary>
/// Indicates that the service should be bound to an instance of the specified provider type.
/// The instance will be activated via the kernel when an instance of the service is activated.
/// </summary>
/// <typeparam name="T">The type of the returned fluent syntax.</typeparam>
/// <param name="providerType">The type of provider to activate.</param>
/// <returns>The fluent syntax.</returns>
protected IBindingWhenInNamedWithOrOnSyntax<T> ToProviderInternal<T>(Type providerType)
{
this.BindingConfiguration.ProviderCallback = ctx => ctx.Kernel.Get(providerType) as IProvider;
this.BindingConfiguration.Target = BindingTarget.Provider;
return new BindingConfigurationBuilder<T>(this.BindingConfiguration, this.ServiceNames);
}
/// <summary>
/// Indicates that the service should be bound to the specified constructor.
/// </summary>
/// <typeparam name="TImplementation">The type of the implementation.</typeparam>
/// <param name="newExpression">The expression that specifies the constructor.</param>
/// <returns>The fluent syntax.</returns>
protected IBindingWhenInNamedWithOrOnSyntax<TImplementation> InternalToConstructor<TImplementation>(
Expression<Func<IConstructorArgumentSyntax, TImplementation>> newExpression)
{
if (!(newExpression.Body is NewExpression ctorExpression))
{
throw new ArgumentException("The expression must be a constructor call.", nameof(newExpression));
}
this.BindingConfiguration.ProviderCallback = ctx => new StandardProvider(ctorExpression.Type, this.Planner, new SpecificConstructorSelector(ctorExpression.Constructor));
this.BindingConfiguration.Target = BindingTarget.Type;
this.AddConstructorArguments(ctorExpression, newExpression.Parameters[0]);
return new BindingConfigurationBuilder<TImplementation>(this.BindingConfiguration, this.ServiceNames);
}
/// <summary>
/// Adds the constructor arguments for the specified constructor expression.
/// </summary>
/// <param name="ctorExpression">The ctor expression.</param>
/// <param name="constructorArgumentSyntaxParameterExpression">The constructor argument syntax parameter expression.</param>
private void AddConstructorArguments(NewExpression ctorExpression, ParameterExpression constructorArgumentSyntaxParameterExpression)
{
var parameters = ctorExpression.Constructor.GetParameters();
for (var i = 0; i < ctorExpression.Arguments.Count; i++)
{
var argument = ctorExpression.Arguments[i];
var argumentName = parameters[i].Name;
this.AddConstructorArgument(argument, argumentName, constructorArgumentSyntaxParameterExpression);
}
}
/// <summary>
/// Adds a constructor argument for the specified argument expression.
/// </summary>
/// <param name="argument">The argument.</param>
/// <param name="argumentName">Name of the argument.</param>
/// <param name="constructorArgumentSyntaxParameterExpression">The constructor argument syntax parameter expression.</param>
private void AddConstructorArgument(Expression argument, string argumentName, ParameterExpression constructorArgumentSyntaxParameterExpression)
{
if (!(argument is MethodCallExpression methodCall) ||
!methodCall.Method.IsGenericMethod ||
methodCall.Method.GetGenericMethodDefinition().DeclaringType != typeof(IConstructorArgumentSyntax))
{
var compiledExpression = Expression.Lambda(argument, constructorArgumentSyntaxParameterExpression).Compile();
this.BindingConfiguration.Parameters.Add(new ConstructorArgument(
argumentName,
ctx => compiledExpression.DynamicInvoke(new ConstructorArgumentSyntax(ctx))));
}
}
/// <summary>
/// Passed to ToConstructor to specify that a constructor value is Injected.
/// </summary>
private class ConstructorArgumentSyntax : IConstructorArgumentSyntax
{
/// <summary>
/// Initializes a new instance of the <see cref="ConstructorArgumentSyntax"/> class.
/// </summary>
/// <param name="context">The context.</param>
public ConstructorArgumentSyntax(IContext context)
{
this.Context = context;
}
/// <summary>
/// Gets the context.
/// </summary>
/// <value>The context.</value>
public IContext Context
{
get;
private set;
}
/// <summary>
/// Specifies that the argument is injected.
/// </summary>
/// <typeparam name="T1">The type of the parameter.</typeparam>
/// <returns>Not used. This interface has no implementation.</returns>
public T1 Inject<T1>()
{
throw new InvalidOperationException("This method is for declaration that a parameter shall be injected only! Never call it directly.");
}
}
}
}