Source

AccessorTest / Tester.cs

Full commit
using System;
using System.Diagnostics;

namespace AccessorTest
{
  public class Tester
  {
    public Tester(string name, IPropertyAccessor propertyAccessor)
    {
      Name = name;
      PropertyAccessor = propertyAccessor;
      SetTimer = new Stopwatch();
      GetTimer = new Stopwatch();
    }

    private Entity values;
    private Entity target;

    /// <summary>
    /// Runs the get and set performance tests.
    /// </summary>
    public void Run()
    {
      RunSet();
      RunGet();
    }

    /// <summary>
    /// Sets property values of the target entity using the property 
    /// accessor and records the time taken to complete the operation.
    /// </summary>
    private void RunSet()
    {
      var guid = Guid.NewGuid();
      values = new Entity { Id = guid, Name = guid.ToString() };
      target = new Entity();
      if (PropertyAccessor == null)
      {
        SetTimer.Start();
        target.Id = values.Id;
        target.Name = values.Name;
        SetTimer.Stop();
        return;
      }
      SetTimer.Start();
      PropertyAccessor.SetPropertyValue(target, values.Id, "Id");
      PropertyAccessor.SetPropertyValue(target, values.Name, "Name");
      SetTimer.Stop();
    }

    /// <summary>
    /// Gets property values of the target entity using the property 
    /// accessor and records the time taken to complete the operation.
    /// </summary>
    private void RunGet()
    {
      if (PropertyAccessor == null)
      {
        GetTimer.Start();
        if (target.Id == values.Id && target.Name == values.Name)
          GetTimer.Stop();
        else
          throw new Exception("Accessor returned invalid property values");
        return;
      }
      GetTimer.Start();
      if (PropertyAccessor.GetPropertyValue(target, "Id").Equals(values.Id) && PropertyAccessor.GetPropertyValue(target, "Name").Equals(values.Name))
        GetTimer.Stop();
      else
        throw new Exception("Accessor returned invalid property values");
    }

    public string Name { get; private set; }

    public Stopwatch SetTimer { get; private set; }

    public Stopwatch GetTimer { get; private set; }

    public IPropertyAccessor PropertyAccessor { get; private set; }
  }
}