Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reading of custom types #115

Open
samcragg opened this issue Mar 18, 2024 · 0 comments
Open

Support reading of custom types #115

samcragg opened this issue Mar 18, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@samcragg
Copy link

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

@samcragg samcragg added the enhancement New feature or request label Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant