This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
KeyValuePairModelBinder.cs
142 lines (123 loc) · 6 KB
/
KeyValuePairModelBinder.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
// 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.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Mvc.ModelBinding.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
{
/// <summary>
/// An <see cref="IModelBinder"/> for <see cref="KeyValuePair{TKey, TValue}"/>.
/// </summary>
/// <typeparam name="TKey">The key type.</typeparam>
/// <typeparam name="TValue">The value type.</typeparam>
public class KeyValuePairModelBinder<TKey, TValue> : IModelBinder
{
private readonly IModelBinder _keyBinder;
private readonly IModelBinder _valueBinder;
private readonly ILogger _logger;
/// <summary>
/// <para>This constructor is obsolete and will be removed in a future version. The recommended alternative
/// is the overload that also takes an <see cref="ILoggerFactory"/>.</para>
/// <para>Creates a new <see cref="KeyValuePair{TKey, TValue}"/>.</para>
/// </summary>
/// <param name="keyBinder">The <see cref="IModelBinder"/> for <typeparamref name="TKey"/>.</param>
/// <param name="valueBinder">The <see cref="IModelBinder"/> for <typeparamref name="TValue"/>.</param>
[Obsolete("This constructor is obsolete and will be removed in a future version. The recommended alternative"
+ " is the overload that also takes an " + nameof(ILoggerFactory) + ".")]
public KeyValuePairModelBinder(IModelBinder keyBinder, IModelBinder valueBinder)
: this(keyBinder, valueBinder, NullLoggerFactory.Instance)
{
}
/// <summary>
/// Creates a new <see cref="KeyValuePair{TKey, TValue}"/>.
/// </summary>
/// <param name="keyBinder">The <see cref="IModelBinder"/> for <typeparamref name="TKey"/>.</param>
/// <param name="valueBinder">The <see cref="IModelBinder"/> for <typeparamref name="TValue"/>.</param>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
public KeyValuePairModelBinder(IModelBinder keyBinder, IModelBinder valueBinder, ILoggerFactory loggerFactory)
{
if (keyBinder == null)
{
throw new ArgumentNullException(nameof(keyBinder));
}
if (valueBinder == null)
{
throw new ArgumentNullException(nameof(valueBinder));
}
if (loggerFactory == null)
{
throw new ArgumentNullException(nameof(loggerFactory));
}
_keyBinder = keyBinder;
_valueBinder = valueBinder;
_logger = loggerFactory.CreateLogger<KeyValuePairModelBinder<TKey, TValue>>();
}
/// <inheritdoc />
public async Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
_logger.AttemptingToBindModel(bindingContext);
var keyModelName = ModelNames.CreatePropertyModelName(bindingContext.ModelName, "Key");
var keyResult = await TryBindStrongModel<TKey>(bindingContext, _keyBinder, "Key", keyModelName);
var valueModelName = ModelNames.CreatePropertyModelName(bindingContext.ModelName, "Value");
var valueResult = await TryBindStrongModel<TValue>(bindingContext, _valueBinder, "Value", valueModelName);
if (keyResult.IsModelSet && valueResult.IsModelSet)
{
var model = new KeyValuePair<TKey, TValue>(
ModelBindingHelper.CastOrDefault<TKey>(keyResult.Model),
ModelBindingHelper.CastOrDefault<TValue>(valueResult.Model));
bindingContext.Result = ModelBindingResult.Success(model);
_logger.DoneAttemptingToBindModel(bindingContext);
return;
}
if (!keyResult.IsModelSet && valueResult.IsModelSet)
{
bindingContext.ModelState.TryAddModelError(
keyModelName,
bindingContext.ModelMetadata.ModelBindingMessageProvider.MissingKeyOrValueAccessor());
_logger.DoneAttemptingToBindModel(bindingContext);
return;
}
if (keyResult.IsModelSet && !valueResult.IsModelSet)
{
bindingContext.ModelState.TryAddModelError(
valueModelName,
bindingContext.ModelMetadata.ModelBindingMessageProvider.MissingKeyOrValueAccessor());
_logger.DoneAttemptingToBindModel(bindingContext);
return;
}
// If we failed to find data for a top-level model, then generate a
// default 'empty' model and return it.
if (bindingContext.IsTopLevelObject)
{
var model = new KeyValuePair<TKey, TValue>();
bindingContext.Result = ModelBindingResult.Success(model);
}
_logger.DoneAttemptingToBindModel(bindingContext);
}
internal async Task<ModelBindingResult> TryBindStrongModel<TModel>(
ModelBindingContext bindingContext,
IModelBinder binder,
string propertyName,
string propertyModelName)
{
var propertyModelMetadata = bindingContext.ModelMetadata.Properties[propertyName];
using (bindingContext.EnterNestedScope(
modelMetadata: propertyModelMetadata,
fieldName: propertyName,
modelName: propertyModelName,
model: null))
{
await binder.BindModelAsync(bindingContext);
return bindingContext.Result;
}
}
}
}