Add TypeHandlerFactory for open-generic type handler registration#2211
Open
PauloHMattos wants to merge 1 commit into
Open
Add TypeHandlerFactory for open-generic type handler registration#2211PauloHMattos wants to merge 1 commit into
PauloHMattos wants to merge 1 commit into
Conversation
97f5549 to
02bede9
Compare
02bede9 to
b828bb7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Dapper's
AddTypeHandlerAPI requires a concrete closed type and a concrete handler instance. There was no way to register a single handler that covers all closed variants of an open generic type (e.g.,HashSet<T>,List<T>) without registering each element type individually.Proposed solution
Introduces
SqlMapper.TypeHandlerFactory, an abstract class modelled afterSystem.Text.Json.Serialization.JsonConverterFactory, that lets users register a single factory covering an entire family of types.Register a factory with the new
AddTypeHandlerFactorymethod:Implementation details
typeHandlers, it queries each registered factory in registration order. The first factory whoseCanHandlereturnstruewins.Createis called and the resulting handler is immediately registered viaAddTypeHandlerCore, which updates both thetypeHandlersdictionary andTypeHandlerCache<T>(used by IL emitted code paths). Subsequent lookups for the same type are O(1) dictionary reads so the factory is only consulted once per type.HasTypeHandleralso checks factories, so callers can introspect before executing queries.ResetTypeHandlers()clears all registered factories.typeHandlers.New API surface
public static class SqlMapper { + public static void AddTypeHandlerFactory(SqlMapper.TypeHandlerFactory factory); + public abstract class TypeHandlerFactory + { + public abstract bool CanHandle(System.Type type); + public abstract ITypeHandler Create(System.Type type); + } }Closes #2190.