Skip to content

Commit

Permalink
Remove ViewDataEvaluator.GetPropertyValue Closure (aspnet#5325)
Browse files Browse the repository at this point in the history
- part of aspnet#3918
  • Loading branch information
benaadams authored and dougbu committed Sep 28, 2016
1 parent 0a4c06e commit a04f5cb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private static ViewDataInfo GetPropertyValue(object container, string propertyNa
return null;
}

return new ViewDataInfo(container, propertyInfo, () => propertyInfo.GetValue(container));
return new ViewDataInfo(container, propertyInfo);
}

private struct ExpressionPair
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,44 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
{
public class ViewDataInfo
{
private static readonly Func<object> _propertyInfoResolver = () => null;

private object _value;
private Func<object> _valueAccessor;

/// <summary>
/// Initializes a new instance of the <see cref="ViewDataInfo"/> class with info about a
/// <see cref="ViewDataDictionary"/> lookup which has already been evaluated.
/// </summary>
/// <param name="container">The <see cref="object"/> that <paramref name="value"/> was evaluated from.</param>
/// <param name="value">The evaluated value.</param>
public ViewDataInfo(object container, object value)
{
Container = container;
_value = value;
}

/// <summary>
/// Initializes a new instance of the <see cref="ViewDataInfo"/> class with info about a
/// <see cref="ViewDataDictionary"/> lookup which is evaluated when <see cref="Value"/> is read.
/// It uses <see cref="System.Reflection.PropertyInfo.GetValue(object)"/> on <paramref name="propertyInfo"/>
/// passing parameter <paramref name="container"/> to lazily evaluate the value.
/// </summary>
/// <param name="container">The <see cref="object"/> that <see cref="Value"/> will be evaluated from.</param>
/// <param name="propertyInfo">The <see cref="PropertyInfo"/> that will be used to evalute <see cref="Value"/>.</param>
public ViewDataInfo(object container, PropertyInfo propertyInfo)
: this(container, propertyInfo, _propertyInfoResolver)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ViewDataInfo"/> class with info about a
/// <see cref="ViewDataDictionary"/> lookup which is evaluated when <see cref="Value"/> is read.
/// It uses <paramref name="valueAccessor"/> to lazily evaluate the value.
/// </summary>
/// <param name="container">The <see cref="object"/> that has the <see cref="Value"/>.</param>
/// <param name="propertyInfo">The <see cref="PropertyInfo"/> that represents <see cref="Value"/>'s property.</param>
/// <param name="valueAccessor"></param>
public ViewDataInfo(object container, PropertyInfo propertyInfo, Func<object> valueAccessor)
{
Container = container;
Expand All @@ -42,8 +63,7 @@ public object Value
{
if (_valueAccessor != null)
{
_value = _valueAccessor();
_valueAccessor = null;
ResolveValue();
}

return _value;
Expand All @@ -54,5 +74,19 @@ public object Value
_valueAccessor = null;
}
}

private void ResolveValue()
{
if (ReferenceEquals(_valueAccessor, _propertyInfoResolver))
{
_value = PropertyInfo.GetValue(Container);
}
else
{
_value = _valueAccessor();
}

_valueAccessor = null;
}
}
}

0 comments on commit a04f5cb

Please sign in to comment.