Dapper icon indicating copy to clipboard operation
Dapper copied to clipboard

Issue #1754 resolution - Allow INullTypeHandler to allow parsing of nulls on custom type handlers

Open jonnymuir opened this issue 3 years ago • 1 comments

This change resolves #1754

It allows a developer to implement an additional interface (INullTypeHandler) on a custom type handler if they want to be able to handle the parsing of nulls (as opposed to the default behaviour of ignore nulls).

This has been implemented as an interface rather than extending the behaviour of SqlMapper.Settings.ApplyNullValues. This is to make it fully backwards compatible for existing code and ensure it doesn't impact performance.

There is an example of existing behaviour: Existing behaviour example

Example of how to implement desired behaviour is in the supplied unit tests.

Apologies - I can't see where to update documentation, if someone could point me in the right direction I will happily contribute to the documentation too.

Changes:

  • Unit tests to show desired behaviour and ensure backwards compatible behaviour
  • Create INullTypeHandler interface
  • Change LoadReaderValueOrBranchToDBNullLabel to check for implementation of INullTypeHandler
  • Alter GenerateDeserializerFromMap to produce IL which will use the null handler if required

Performance assessment:

  • Performance overhead = 1 check for INullTypeHandler per mapped ITypeHandler type on generation of IL.
  • No performance overhead at runtime of IL (because the generated IL is the same if the interface isn't implemented)
  • If Interface is implemented the same pattern as ITypeHandlerParse is used

Impact assessment:

  • Developer is required to implement a new interface. No change to any existing behaviours. Unit test supplied to uphold this.

jonnymuir avatar Feb 12 '22 10:02 jonnymuir

Hi good dapper people - we have been using this now in development and UAT since March and are about to put it into live. It is a really useful addition for us. Dapper is by far the best fit for our projects and this addition is critical for our solution.

It would be great it this addition could be a candidate to be a pull requested, I think there may be others that find it useful and I think it fits with the ethos of the project (performance, simplicity and quality!)

Is there anything I can do to make it more attractive? Contribute to documentation etc?

Ta.

jonnymuir avatar Jul 05 '22 22:07 jonnymuir