Skip to content

Commit efe27b0

Browse files
committed
Changes per PR comments
* Cleanup unused exception code path, fix doc comments * Clean up usage of variables * Adjust logging to be consistent
1 parent 559ad78 commit efe27b0

File tree

8 files changed

+17
-34
lines changed

8 files changed

+17
-34
lines changed

src/Mvc/Mvc.Core/src/ModelBinding/Binders/DateTimeModelBinder.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Globalization;
6-
using System.Runtime.ExceptionServices;
76
using System.Threading.Tasks;
87
using Microsoft.Extensions.Logging;
98

@@ -18,9 +17,9 @@ public class DateTimeModelBinder : IModelBinder
1817
private readonly ILogger _logger;
1918

2019
/// <summary>
21-
/// Initializes a new instance of <see cref="DecimalModelBinder"/>.
20+
/// Initializes a new instance of <see cref="DateTimeModelBinder"/>.
2221
/// </summary>
23-
/// <param name="supportedStyles">The <see cref="NumberStyles"/>.</param>
22+
/// <param name="supportedStyles">The <see cref="DateTimeStyles"/>.</param>
2423
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
2524
public DateTimeModelBinder(DateTimeStyles supportedStyles, ILoggerFactory loggerFactory)
2625
{
@@ -61,8 +60,8 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
6160
var type = metadata.UnderlyingOrModelType;
6261
try
6362
{
63+
6464
var value = valueProviderResult.FirstValue;
65-
var culture = valueProviderResult.Culture;
6665

6766
object model;
6867
if (string.IsNullOrWhiteSpace(value))
@@ -72,7 +71,7 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
7271
}
7372
else if (type == typeof(DateTime))
7473
{
75-
model = DateTime.Parse(value, culture, _supportedStyles);
74+
model = DateTime.Parse(value, valueProviderResult.Culture, _supportedStyles);
7675
}
7776
else
7877
{
@@ -97,17 +96,8 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
9796
}
9897
catch (Exception exception)
9998
{
100-
var isFormatException = exception is FormatException;
101-
if (!isFormatException && exception.InnerException != null)
102-
{
103-
// Unlike TypeConverters, floating point types do not seem to wrap FormatExceptions. Preserve
104-
// this code in case a cursory review of the CoreFx code missed something.
105-
exception = ExceptionDispatchInfo.Capture(exception.InnerException).SourceException;
106-
}
107-
108-
modelState.TryAddModelError(modelName, exception, metadata);
109-
11099
// Conversion failed.
100+
modelState.TryAddModelError(modelName, exception, metadata);
111101
}
112102

113103
_logger.DoneAttemptingToBindModel(bindingContext);

src/Mvc/Mvc.Core/src/ModelBinding/Binders/DateTimeModelBinderProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public IModelBinder GetBinder(ModelBinderProviderContext context)
2424
}
2525

2626
var modelType = context.Metadata.UnderlyingOrModelType;
27-
var loggerFactory = context.Services.GetRequiredService<ILoggerFactory>();
2827
if (modelType == typeof(DateTime))
2928
{
29+
var loggerFactory = context.Services.GetRequiredService<ILoggerFactory>();
3030
return new DateTimeModelBinder(SupportedStyles, loggerFactory);
3131
}
3232

src/Mvc/Mvc.Core/src/ModelBinding/Binders/DecimalModelBinder.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
6363
try
6464
{
6565
var value = valueProviderResult.FirstValue;
66-
var culture = valueProviderResult.Culture;
6766

6867
object model;
6968
if (string.IsNullOrWhiteSpace(value))
@@ -73,7 +72,7 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
7372
}
7473
else if (type == typeof(decimal))
7574
{
76-
model = decimal.Parse(value, _supportedStyles, culture);
75+
model = decimal.Parse(value, _supportedStyles, valueProviderResult.Culture);
7776
}
7877
else
7978
{

src/Mvc/Mvc.Core/src/ModelBinding/Binders/DoubleModelBinder.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
6363
try
6464
{
6565
var value = valueProviderResult.FirstValue;
66-
var culture = valueProviderResult.Culture;
6766

6867
object model;
6968
if (string.IsNullOrWhiteSpace(value))
@@ -73,7 +72,7 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
7372
}
7473
else if (type == typeof(double))
7574
{
76-
model = double.Parse(value, _supportedStyles, culture);
75+
model = double.Parse(value, _supportedStyles, valueProviderResult.Culture);
7776
}
7877
else
7978
{

src/Mvc/Mvc.Core/src/ModelBinding/Binders/FloatModelBinder.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
6363
try
6464
{
6565
var value = valueProviderResult.FirstValue;
66-
var culture = valueProviderResult.Culture;
6766

6867
object model;
6968
if (string.IsNullOrWhiteSpace(value))
@@ -73,7 +72,7 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
7372
}
7473
else if (type == typeof(float))
7574
{
76-
model = float.Parse(value, _supportedStyles, culture);
75+
model = float.Parse(value, _supportedStyles, valueProviderResult.Culture);
7776
}
7877
else
7978
{

src/Mvc/Mvc.Core/src/ModelBinding/Binders/SimpleTypeModelBinder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
4646
throw new ArgumentNullException(nameof(bindingContext));
4747
}
4848

49+
_logger.AttemptingToBindModel(bindingContext);
50+
4951
var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
5052
if (valueProviderResult == ValueProviderResult.None)
5153
{
@@ -56,8 +58,6 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
5658
return Task.CompletedTask;
5759
}
5860

59-
_logger.AttemptingToBindModel(bindingContext);
60-
6161
bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);
6262

6363
try

src/Mvc/Mvc.Core/test/ModelBinding/Binders/DateTimeModelBinderTest.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ public async Task BindModel_ReturnsFailure_IfAttemptedValueCannotBeParsed()
3333
public async Task BindModel_CreatesError_IfAttemptedValueCannotBeParsed()
3434
{
3535
// Arrange
36-
var message = "The value 'not a number' is not valid.";
36+
var message = "The value 'not a date' is not valid.";
3737
var bindingContext = GetBindingContext();
3838
bindingContext.ValueProvider = new SimpleValueProvider
3939
{
40-
{ "theModelName", "not a number" },
40+
{ "theModelName", "not a date" },
4141
};
4242
var binder = GetBinder();
4343

@@ -164,7 +164,7 @@ public async Task BindModel_ReturnsModel_IfAttemptedValueIsValid(Type type)
164164
var bindingContext = GetBindingContext(type);
165165
bindingContext.ValueProvider = new SimpleValueProvider(new CultureInfo("fr-FR"))
166166
{
167-
{ "theModelName", "2019-06-14T02:30:04.0Z" }
167+
{ "theModelName", "2019-06-14T02:30:04.0000000Z" }
168168
};
169169
var binder = GetBinder();
170170

@@ -184,10 +184,10 @@ public async Task UsesSpecifiedStyleToParseModel()
184184
{
185185
// Arrange
186186
var bindingContext = GetBindingContext();
187-
var expected = DateTime.Parse("2019-06-14T02:30:04.0Z");
187+
var expected = DateTime.Parse("2019-06-14T02:30:04.0000000Z");
188188
bindingContext.ValueProvider = new SimpleValueProvider(new CultureInfo("fr-FR"))
189189
{
190-
{ "theModelName", "2019-06-14T02:30:04.0Z" }
190+
{ "theModelName", "2019-06-14T02:30:04.0000000Z" }
191191
};
192192
var binder = GetBinder(DateTimeStyles.AssumeLocal);
193193

@@ -218,9 +218,5 @@ private static DefaultModelBindingContext GetBindingContext(Type modelType = nul
218218
ValueProvider = new SimpleValueProvider() // empty
219219
};
220220
}
221-
222-
private sealed class TestClass
223-
{
224-
}
225221
}
226222
}

src/Mvc/Mvc.Core/test/ModelBinding/Binders/SimpleTypeModelBinderTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public async Task BindModel_EmptyValueProviderResult_ReturnsFailedAndLogsSuccess
194194
// Assert
195195
Assert.Equal(ModelBindingResult.Failed(), bindingContext.Result);
196196
Assert.Empty(bindingContext.ModelState);
197-
Assert.Equal(2, sink.Writes.Count());
197+
Assert.Equal(3, sink.Writes.Count());
198198
}
199199

200200
[Theory]

0 commit comments

Comments
 (0)