Make `ModelBinderAttribute.BindingSource` setter `protected`

- #3428
- the `public` setter was not useful when this class is used as an attribute
- make property `virtual` to support other override patterns
- update existing test to use both override patterns
This commit is contained in:
Doug Bunting 2015-11-20 16:29:26 -08:00
parent a420af67b7
commit 45ff0a269c
3 changed files with 20 additions and 9 deletions

View File

@ -32,7 +32,7 @@ namespace Microsoft.AspNet.Mvc
public Type BinderType { get; set; } public Type BinderType { get; set; }
/// <inheritdoc /> /// <inheritdoc />
public BindingSource BindingSource public virtual BindingSource BindingSource
{ {
get get
{ {
@ -43,7 +43,7 @@ namespace Microsoft.AspNet.Mvc
return _bindingSource; return _bindingSource;
} }
set protected set
{ {
_bindingSource = value; _bindingSource = value;
} }

View File

@ -108,8 +108,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
// Arrange // Arrange
var attributes = new object[] var attributes = new object[]
{ {
new ModelBinderAttribute() { BindingSource = BindingSource.Body }, new BindingSourceModelBinderAttribute(BindingSource.Body),
new ModelBinderAttribute() { BindingSource = BindingSource.Query }, new BindingSourceModelBinderAttribute(BindingSource.Query),
}; };
var context = new BindingMetadataProviderContext( var context = new BindingMetadataProviderContext(
@ -132,8 +132,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
var attributes = new object[] var attributes = new object[]
{ {
new ModelBinderAttribute(), new ModelBinderAttribute(),
new ModelBinderAttribute() { BindingSource = BindingSource.Body }, new BindingSourceModelBinderAttribute(BindingSource.Body),
new ModelBinderAttribute() { BindingSource = BindingSource.Query }, new BindingSourceModelBinderAttribute(BindingSource.Query),
}; };
var context = new BindingMetadataProviderContext( var context = new BindingMetadataProviderContext(
@ -545,5 +545,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
private class BindRequiredOverridesInheritedBindNever : BindNeverOnClass private class BindRequiredOverridesInheritedBindNever : BindNeverOnClass
{ {
} }
private class BindingSourceModelBinderAttribute : ModelBinderAttribute
{
public BindingSourceModelBinderAttribute(BindingSource bindingSource)
{
BindingSource = bindingSource;
}
}
} }
} }

View File

@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Xunit; using Xunit;
namespace Microsoft.AspNet.Mvc.ModelBinding namespace Microsoft.AspNet.Mvc.ModelBinding
@ -39,8 +38,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void BinderType_SettingBindingSource_OverridesDefaultCustomBindingSource() public void BinderType_SettingBindingSource_OverridesDefaultCustomBindingSource()
{ {
// Arrange // Arrange
var attribute = new ModelBinderAttribute(); var attribute = new FromQueryModelBinderAttribute();
attribute.BindingSource = BindingSource.Query;
attribute.BinderType = typeof(ByteArrayModelBinder); attribute.BinderType = typeof(ByteArrayModelBinder);
// Act // Act
@ -49,5 +47,10 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
// Assert // Assert
Assert.Equal(BindingSource.Query, source); Assert.Equal(BindingSource.Query, source);
} }
private class FromQueryModelBinderAttribute : ModelBinderAttribute
{
public override BindingSource BindingSource => BindingSource.Query;
}
} }
} }