title: Querying abstract models in dapper
body: <p>I'm using Table Per Hierarchy database inheritance where columns for all derived types are in a single table. Each derived table is identified using a string Discriminator field that holds the name of the derived class:</p>
  
  <pre><code>---------------------
  | tanimal           |
  ---------------------
  | animalid          |
  | discriminator     |
  | furcolour         |
  | feathercolour     |
  ---------------------
  
  public abstract class Animal
  {
      public int AnimalId { get; set; }
      public string Discriminator { get { return GetType().Name; } }
  }
  
  public class Bird : Animal
  {
      public string FeatherColour { get; set; }
  }
  
  public class Dog : Animal
  {
      public string FurColour { get; set; }
  }
  </code></pre>
  
  <p>As expected, when retrieving this via Dapper's query method I receive <code>Instances of abstract classes cannot be created</code>. I would hope that this would return a list of Animal with their values being the respective derived types.</p>
  
  <pre><code>var animals = Connection.Query&lt;Animal&gt;("SELECT * FROM tanimal")
  </code></pre>
  
  <p>My attempts to add support for this have been unsuccessful. Before SqlMapper.cs::GetTypeDeserializer() is called if the type being passed in is an abstract class then I replace the type with the one returned in the following method:</p>
  
  <pre><code>static Type GetDerivedType(Type abstractType, IDataReader reader)
  {
      var discriminator = abstractType.GetProperty("Discriminator");
      if (discriminator == null)
          throw new InvalidOperationException("Cannot create instance of abstract class " + abstractType.FullName + ". To allow dapper to map to a derived type, add a Discriminator field that stores the name of the derived type");
  
      return Type.GetType((string)reader["Discriminator"]);
  }
  </code></pre>
  
  <p>However it looks like at this point the reader hasn't been opened so it fails with <code>Invalid attempt to read when no data is present</code>. </p>
  
  <p>Is this the correct approach to take? Has there been any effort to support this elsewhere?</p>