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 https://github.com/DapperLib/Dapper.Contrib/issues/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.
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:
GetSingleKey<T>()
, only return the property named 'Id' as a fallback if there is noKey
orExplicitKey
property. (fixes https://github.com/DapperLib/Dapper.Contrib/issues/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-safeLazy<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 ofGetSingleKey<T>()
.Add
PropertyNamedId
to distinguish properties named 'Id' from properties explicitly annotated with aKeyAttribute
.Add an optional parameter to
KeyPropertiesCache
to specify whether you want to include theId
property or not, so that the existing code didn't need to be modified as much.