DapperLib / DapperLib/Dapper

Proposal: Allow Query() to support polymorphic loads

Open
#976 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

Dapper has documented support for Type Switching Per Row, however, this is somewhat cumbersome to use. What I'd really like is the ability to use the standard Query<>() api to load a list of polymorphic objects. This would also allow me to use these types with things like multi mapping. The code I'd like to write would look like:

IEnumerable <Shape> shapes = connection.Query<Shape>("SELECT * FROM Shapes"); 

We've hacked a fork of Dapper to allow this -- much of the code can be done outside the core dapper classes, however, it requires 2 changes to SqlMapper:

  1. Support for "custom deserializers" -- The ability to register a custom Func<IDataReader, object> for a specific base type. This Func would look at the row and produce the deserializer for the concrete type. This requires a change in GetDeserializer to check for the existance of the custom deserializer.
  2. Stop using CommandBehavior.SequentialAccess -- because the custom deserializers need to examine the discriminator column out of order to determine which subclass to deserialize. Not sure the impact this change has, but if it's significant, perhaps this could be made configurable.

Here's an example of custom deserializer which looks at a discriminator column to produce a deserializer for the proper subclass:

internal Func<IDataReader, object> GetShapeDeserializer(Type type, IDataReader reader, int startBound, int length, bool returnNullIfFirstMissing)
{
    var typeMap = new Dictionary<object, Type> {
        [ShapeType.Circle] = typeof(Circle),
        [ShapeType.Square] = typeof(Square),
        [ShapeType.Triangle] = typeof(Triangle)
    };
    var deserializers = typeMap.Values.ToDictionary(
        t => t,
        t => SqlMapper.GetTypeDeserializer(t, reader, startBound, length, returnNullIfFirstMissing));

    if (length == -1)
        length = reader.FieldCount - startBound;

    return r => {
        int idx = r.GetOrdinal("Type");
        object val = r.GetValue(idx);
        if (val == DBNull.Value)
            return null;

        Type childType = typeMap[val];
        return deserializers[childType](r);
    };
}

Anyway, happy to submit a PR and work on a better API, but wanted to make sure you were open to the idea before doing so.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Dapper/SqlMapper.cs around GetDeserializer, then review the documented Type Switching Per Row and multi-mapping examples in the repository README. Define an API for custom deserializers and determine how Query should select concrete types without SequentialAccess; done means polymorphic Query loads work and remain compatible with multi-mapping.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sql
Domain
backend-api-design, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.