Fixes 2307. MutableObjectBinder now relies on ModelMetadata to decide if a type is a complex type.

This avoids reflection at multiple places and allows caching.
This commit is contained in:
Harsh Gupta 2015-04-03 10:44:17 -07:00
parent 9330789fe3
commit 0c0fa10ed0
2 changed files with 16 additions and 6 deletions

View File

@ -18,7 +18,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public virtual async Task<ModelBindingResult> BindModelAsync(ModelBindingContext bindingContext) public virtual async Task<ModelBindingResult> BindModelAsync(ModelBindingContext bindingContext)
{ {
ModelBindingHelper.ValidateBindingContext(bindingContext); ModelBindingHelper.ValidateBindingContext(bindingContext);
if (!CanBindType(bindingContext.ModelType)) if (!CanBindType(bindingContext.ModelMetadata))
{ {
return null; return null;
} }
@ -188,20 +188,20 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
return false; return false;
} }
private static bool CanBindType(Type modelType) private static bool CanBindType(ModelMetadata modelMetadata)
{ {
// Simple types cannot use this binder // Simple types cannot use this binder
var isComplexType = !TypeHelper.HasStringConverter(modelType); if (!modelMetadata.IsComplexType)
if (!isComplexType)
{ {
return false; return false;
} }
if (modelType == typeof(ComplexModelDto)) if (modelMetadata.ModelType == typeof(ComplexModelDto))
{ {
// forbidden type - will cause a stack overflow if we try binding this type // forbidden type - will cause a stack overflow if we try binding this type
return false; return false;
} }
return true; return true;
} }

View File

@ -13,6 +13,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// </summary> /// </summary>
public abstract class ModelMetadata public abstract class ModelMetadata
{ {
private bool? _isComplexType;
/// <summary> /// <summary>
/// The default value of <see cref="ModelMetadata.Order"/>. /// The default value of <see cref="ModelMetadata.Order"/>.
/// </summary> /// </summary>
@ -254,7 +256,15 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// </remarks> /// </remarks>
public bool IsComplexType public bool IsComplexType
{ {
get { return !TypeHelper.HasStringConverter(ModelType); } get
{
if (_isComplexType == null)
{
_isComplexType = !TypeHelper.HasStringConverter(ModelType);
}
return _isComplexType.Value;
}
} }
/// <summary> /// <summary>