DapperLib / DapperAOT

Build time tools in the flavor of Dapper
Other
349 stars 19 forks source link

Support reading of custom types #115

Open samcragg opened 4 months ago

samcragg commented 4 months ago

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:

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