This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
BindingInfo.cs
249 lines (215 loc) · 9.37 KB
/
BindingInfo.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.AspNetCore.Mvc.ModelBinding
{
/// <summary>
/// Binding info which represents metadata associated to an action parameter.
/// </summary>
public class BindingInfo
{
/// <summary>
/// Creates a new <see cref="BindingInfo"/>.
/// </summary>
public BindingInfo()
{
}
/// <summary>
/// Creates a copy of a <see cref="BindingInfo"/>.
/// </summary>
/// <param name="other">The <see cref="BindingInfo"/> to copy.</param>
public BindingInfo(BindingInfo other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
BindingSource = other.BindingSource;
BinderModelName = other.BinderModelName;
BinderType = other.BinderType;
PropertyFilterProvider = other.PropertyFilterProvider;
RequestPredicate = other.RequestPredicate;
}
/// <summary>
/// Gets or sets the <see cref="ModelBinding.BindingSource"/>.
/// </summary>
public BindingSource BindingSource { get; set; }
/// <summary>
/// Gets or sets the binder model name.
/// </summary>
public string BinderModelName { get; set; }
/// <summary>
/// Gets or sets the <see cref="Type"/> of the model binder used to bind the model.
/// </summary>
public Type BinderType { get; set; }
/// <summary>
/// Gets or sets the <see cref="ModelBinding.IPropertyFilterProvider"/>.
/// </summary>
public IPropertyFilterProvider PropertyFilterProvider { get; set; }
/// <summary>
/// Gets or sets a predicate which determines whether or not the model should be bound based on state
/// from the current request.
/// </summary>
public Func<ActionContext, bool> RequestPredicate { get; set; }
/// <summary>
/// Constructs a new instance of <see cref="BindingInfo"/> from the given <paramref name="attributes"/>.
/// <para>
/// This overload does not account for <see cref="BindingInfo"/> specified via <see cref="ModelMetadata"/>. Consider using
/// <see cref="GetBindingInfo(IEnumerable{object}, ModelMetadata)"/> overload, or <see cref="TryApplyBindingInfo(ModelMetadata)"/>
/// on the result of this method to get a more accurate <see cref="BindingInfo"/> instance.
/// </para>
/// </summary>
/// <param name="attributes">A collection of attributes which are used to construct <see cref="BindingInfo"/>
/// </param>
/// <returns>A new instance of <see cref="BindingInfo"/>.</returns>
public static BindingInfo GetBindingInfo(IEnumerable<object> attributes)
{
var bindingInfo = new BindingInfo();
var isBindingInfoPresent = false;
// BinderModelName
foreach (var binderModelNameAttribute in attributes.OfType<IModelNameProvider>())
{
isBindingInfoPresent = true;
if (binderModelNameAttribute?.Name != null)
{
bindingInfo.BinderModelName = binderModelNameAttribute.Name;
break;
}
}
// BinderType
foreach (var binderTypeAttribute in attributes.OfType<IBinderTypeProviderMetadata>())
{
isBindingInfoPresent = true;
if (binderTypeAttribute.BinderType != null)
{
bindingInfo.BinderType = binderTypeAttribute.BinderType;
break;
}
}
// BindingSource
foreach (var bindingSourceAttribute in attributes.OfType<IBindingSourceMetadata>())
{
isBindingInfoPresent = true;
if (bindingSourceAttribute.BindingSource != null)
{
bindingInfo.BindingSource = bindingSourceAttribute.BindingSource;
break;
}
}
// PropertyFilterProvider
var propertyFilterProviders = attributes.OfType<IPropertyFilterProvider>().ToArray();
if (propertyFilterProviders.Length == 1)
{
isBindingInfoPresent = true;
bindingInfo.PropertyFilterProvider = propertyFilterProviders[0];
}
else if (propertyFilterProviders.Length > 1)
{
isBindingInfoPresent = true;
bindingInfo.PropertyFilterProvider = new CompositePropertyFilterProvider(propertyFilterProviders);
}
// RequestPredicate
foreach (var requestPredicateProvider in attributes.OfType<IRequestPredicateProvider>())
{
isBindingInfoPresent = true;
if (requestPredicateProvider.RequestPredicate != null)
{
bindingInfo.RequestPredicate = requestPredicateProvider.RequestPredicate;
break;
}
}
return isBindingInfoPresent ? bindingInfo : null;
}
/// <summary>
/// Constructs a new instance of <see cref="BindingInfo"/> from the given <paramref name="attributes"/> and <paramref name="modelMetadata"/>.
/// </summary>
/// <param name="attributes">A collection of attributes which are used to construct <see cref="BindingInfo"/>.</param>
/// <param name="modelMetadata">The <see cref="ModelMetadata"/>.</param>
/// <returns>A new instance of <see cref="BindingInfo"/> if any binding metadata was discovered; otherwise or <see langword="null"/>.</returns>
public static BindingInfo GetBindingInfo(IEnumerable<object> attributes, ModelMetadata modelMetadata)
{
if (attributes == null)
{
throw new ArgumentNullException(nameof(attributes));
}
if (modelMetadata == null)
{
throw new ArgumentNullException(nameof(modelMetadata));
}
var bindingInfo = GetBindingInfo(attributes);
var isBindingInfoPresent = bindingInfo != null;
if (bindingInfo == null)
{
bindingInfo = new BindingInfo();
}
isBindingInfoPresent |= bindingInfo.TryApplyBindingInfo(modelMetadata);
return isBindingInfoPresent ? bindingInfo : null;
}
/// <summary>
/// Applies binding metadata from the specified <paramref name="modelMetadata"/>.
/// <para>
/// Uses values from <paramref name="modelMetadata"/> if no value is already available.
/// </para>
/// </summary>
/// <param name="modelMetadata">The <see cref="ModelMetadata"/>.</param>
/// <returns><see langword="true"/> if any binding metadata from <paramref name="modelMetadata"/> was applied;
/// <see langword="false"/> otherwise.</returns>
public bool TryApplyBindingInfo(ModelMetadata modelMetadata)
{
if (modelMetadata == null)
{
throw new ArgumentNullException(nameof(modelMetadata));
}
var isBindingInfoPresent = false;
if (BinderModelName == null && modelMetadata.BinderModelName != null)
{
isBindingInfoPresent = true;
BinderModelName = modelMetadata.BinderModelName;
}
if (BinderType == null && modelMetadata.BinderType != null)
{
isBindingInfoPresent = true;
BinderType = modelMetadata.BinderType;
}
if (BindingSource == null && modelMetadata.BindingSource != null)
{
isBindingInfoPresent = true;
BindingSource = modelMetadata.BindingSource;
}
if (PropertyFilterProvider == null && modelMetadata.PropertyFilterProvider != null)
{
isBindingInfoPresent = true;
PropertyFilterProvider = modelMetadata.PropertyFilterProvider;
}
return isBindingInfoPresent;
}
private class CompositePropertyFilterProvider : IPropertyFilterProvider
{
private readonly IEnumerable<IPropertyFilterProvider> _providers;
public CompositePropertyFilterProvider(IEnumerable<IPropertyFilterProvider> providers)
{
_providers = providers;
}
public Func<ModelMetadata, bool> PropertyFilter => CreatePropertyFilter();
private Func<ModelMetadata, bool> CreatePropertyFilter()
{
var propertyFilters = _providers
.Select(p => p.PropertyFilter)
.Where(p => p != null);
return (m) =>
{
foreach (var propertyFilter in propertyFilters)
{
if (!propertyFilter(m))
{
return false;
}
}
return true;
};
}
}
}
}