Simple MVVM Walkthrough – Refactored–Part 2

In the previous post I showed an attempt to get rid of the magical strings. It worked great for a string property, but failed for the integer one as JR has reported. I decided to look into that, but this time around through TDD and this is what I found (besides the fact that going TDD way would definitely catch it).

When a string property is passed in, returned object is expected therefore there’s nothing complicated, it works. Though, when an integer (or any other type that is not a string) is passed in, .NET implicitly applies Convert() method.

imageimage

Therefore the code should be update:

 

class PropertyOf<T>
  {
    public static string Resolve(Expression<Func<T, object>> expression)
    {
      Expression candidate = null;
  if (expression.Body is UnaryExpression)
    candidate = (expression.Body as UnaryExpression).Operand;

  if (expression.Body is MemberExpression)
    candidate = expression.Body;

  return (candidate as MemberExpression).Member.Name;
}

}

Tests are quite simple:

    [Observation]
    public void Should_return_integer_property_name_as_is_within_the_containing_class()
    {
      PropertyOf<Dummy>.Resolve(x => x.IntegerProperty).Should_Be_Equal_To("IntegerProperty");
    }
[Observation]
public void Should_return_string_property_name_as_is_within_the_containing_class()
{
  PropertyOf&lt;Dummy&gt;.Resolve(x =&gt; x.StringProperty).Should_Be_Equal_To("StringProperty");
}

private class Dummy
{
  public int IntegerProperty { get; set; }
  public string StringProperty { get; set; }
}</pre></div>

No Comments