Replies: 1 comment
-
Difficulties
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Reason
In generic code, it’s common to specify a “property” of an object as a “editable property” in a given “binding like” context written in code. The common usage is to update a sub-property of a root object based on some external trigger. For instance, toggle the
T.IsFavorite
on an object when the user trigger a genericToggleIsFavoriteCommand<T>
.The issue is that it usually requires for the user to specify both a getter and a setter, which could be verbose and confusing, eg.
Command.CreateToggleCommand(getter: x => x.IsFavorite, setter: (x, isFavorite) => x.IsFavorite = isFavorite)
.The common technique is to use an
Expression<Func<T, TProperty>>
, but this uses reflection which is known to be costly at runtime.Also when working with immutable records, we need to create a new instance of T to assign the value. This adds few levels of complexity:
setter
will beFunc<T, TProperty, T>
instead ofAction<T, Tproperty>
)t => t.A.B.C
) you will have to definesetter
for the whole tree, and will require that the user handles possible new values.Proposal
public delegate TProperty PropertySelector<T, Tproperty>(T entity)
AssemblyInitializeAttribute
) into a static registry.PropertySelectors.Get<T, TProperty>(PropertySelector<T, TProperty> selector, string name, string filePath, int lineNumber)
In order to get aIPropertyAccessor<T, Tproperty>
IPropertyAccessor
We will work with interface to allow other implementation than the generated from immutable records we are discussing here (like for instance an attached property which uses a Get and Set method).
Resolution key
We need to find a way to match a definition resolved at compile time an instance at runtime, but we don’t want to give to the user the responsibility to provide a unique cache key.
The only valid way to automatically generate that cache key will be to use the use file path and line number where the delegate is being declared.
Those can be fulfilled by the
CallerFilePathAttribute
and theCallerLineNumberAttribute
. The issue is that in a common usageMyMethod(PropertySelector selector, [CallerFilePath] string path = “”, [CallerLineNumber] int line = -1)
we will get the info for theMyMethod
usage, not theselector
itself. We might also have conflict if a method has 2KeySelector
parameter. So we will also append the parameter name.Rules
PropertySelector
must also require a cache keyKnown limitation
Current limitation that could be resolve in next versions
Beta Was this translation helpful? Give feedback.
All reactions