Skip to content

Refactor property info handling, allow keys different from Id property #158

Closed
nseguin42 wants to merge 8 commits intoDapperLib:mainfrom
nseguin42:main
Closed

Refactor property info handling, allow keys different from Id property #158
nseguin42 wants to merge 8 commits intoDapperLib:mainfrom
nseguin42:main

Conversation

@nseguin42
Copy link

@nseguin42 nseguin42 commented Aug 3, 2023

I've moved this PR to a draft until I have the chance to investigate the test failures. In hindsight, it might be worth just changing GetSingleKey() in a minimal way.

Behavior changes:

  • In GetSingleKey<T>(), only return the property named 'Id' as a fallback if there is no Key or ExplicitKey property. (fixes Why a hard reference to the column "id" #140)
    Note: The new behavior allows manually specifying a key (or explicit key) that is different from the 'Id' property. Any usage that would previously have succeeded (not thrown an exception) should be unaffected.

Refactors/implementation details:

  • Introduce PropertyInfoWrapper to hold all the relevant information about a type's properties. This object is only mutated within a thread-safe Lazy<T>, so we shouldn't have any issues even if multiple threads grab the same instance from a ConcurrentDictionary.

  • Move logic about selecting a key into the PropertyInfoWrapper and cache the result of GetSingleKey<T>().

  • Add PropertyNamedId to distinguish properties named 'Id' from properties explicitly annotated with a KeyAttribute.

  • Add an optional parameter to KeyPropertiesCache to specify whether you want to include the Id property or not, so that the existing code didn't need to be modified as much.

- Only treat a property named 'Id' as a key if there is no Key or ExplicitKey property. (fixes #140)
  The new behavior allows manually specifying a key (or explicit key) that is different from the 'Id' property. Any usage that would previously have succeeded (not thrown an exception) is unaffected.

- Introduce PropertyInfoWrapper to hold all the relevant information about a type's properties.

- Move logic about selecting a key into the PropertyInfoWrapper so it can be cached as well.

- Use a single ConcurrentDictionary of PropertyInfoWrappers instead of one for each type of property. Thread-safety is preserved because all mutation happens inside a thread-safe Lazy<T>.

- Add `PropertyNamedId` to distinguish this property from those explicitly annotated with a `KeyAttribute`.
…ropertiesCache

Quick and dirty fix. Let's see if the tests pass.
This brings the behavior closer to current while preserving the new GetSingleKey<T> behavior. Should fix more tests.
@nseguin42 nseguin42 marked this pull request as draft August 4, 2023 13:04
@nseguin42 nseguin42 closed this by deleting the head repository Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Why a hard reference to the column "id"

2 participants