DapperLib / DapperLib/DapperAOT

Support reading of custom types

Open
#115 0 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
C#
Stars
472
Forks
43
Avg merge
1d 4h
Merged PRs (30d)
18

Description

Would you accept a PR to allow reading of custom types?

Background

In my app I often wrap primitive types in dedicated types to express exactly what they represent and prevent the wrong values being passed around. I spotted there's a TypeHandler class that looks promising, but currently unused.

Proposal

Add a method to TypeHandler that accepts the reader, default implemented to match current behaviour (i.e. non breaking change)

public virtual T Read(DbDataReader reader, int fieldOffset)
        => CommandUtils.As<T>(reader.GetValue(fieldOffset));

Modify the code generator to spot where the TypeHandlerAttribute has been used and, when reading a value that matches:

  • Change the Tokenize to skip the type check when reading the custom type (it's never going to come back from the DB as that type - if it does the type handler can still do the check)
  • Change the generated Read method to create the type handler and invoke the Read method on it.
  • Implement the reading/writing of parameters that are also on the TypeHandler class (it looks like it's not yet implemented)

Alternatively, as it looks like TypeHandler isn't yet used, we could make it an interface with static virtual methods, which would be a micro-optimization that avoids needing to create an instance of the handler when reading the values (i.e. we could write CustomTypeHandler.Read instead of new CustomTypeHandler().Read(), saving an allocation)

I'd be happy to do the work myself and submit a PR (with appropriate tests and some documentation on how to use it), just wanted confirmation this is the right approach to take or if there were any other plans to bring across the SqlMapper type functionality across.

Thanks

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 with src/Dapper.AOT/TypeHandlerT.cs and trace how the code generator currently handles TypeHandlerAttribute, value reads, and parameter writing. Compare the existing behavior with the proposed reader and parameter support; done means custom types can be read and written without breaking current behavior, with appropriate tests and usage documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
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.