-
Notifications
You must be signed in to change notification settings - Fork 163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for expressions in keypath building #3518
Conversation
Pull Request Test Coverage Report for Build 8160379640Details
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good - one thing to consider is if we want to do anything about traversing collections. E.g. this expression could enable calls like First
, Single
, etc.:
MethodCallExpression
{
Arguments:
[
MemberExpression
{
Expression: not null,
Member: PropertyInfo argumentPi
} innerExpression
]
} when typeof(IEnumerable).IsAssignableFrom(argumentPi.PropertyType)
=> GetFullPath(innerExpression)
This would enable stuff like:
// Evaluates to "Friends.FirstName"
KeyPath.For<Person>(p => p.Friends.First().FirstName);
Might be a bit magical though and obviously doesn't include much validation.
/// var keyPath = KeyPath.For<Person>(p => p.Dog.Name); | ||
/// </code> | ||
/// </example> | ||
public static KeyPath For<T>(Expression<Func<T, object>> expression) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the For
name something from Swift/ObjC? I guess I understand where it's coming from, but it's also kind of unusual for a .NET API name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's something I thought made sense for this case 😁
I also thought it was following a similar "style" of KeyPathCollection.Of
. Do you have any other name suggestion? I would much more prefer to have a static method than using the constructor to be honest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nirinchev, do you have any suggestion here about an alternative name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd be impossible to use the ctor here, because we need the generic arg. Perhaps it's a bit verbose, but how about FromExpression
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it sounds reasonable :)
Co-authored-by: Nikola Irinchev <irinchev@me.com>
Yeah, it would work, but I think it would feel forced in a way, and could give the wrong impression that we're creating an index path for a specific collection index. For now I'd prefer to keep it as it is, and we can come back to it if there's request for more strongly typed keypaths. |
…nto fp/kp-expression # Please enter a commit message to explain why this merge is necessary, # especially if it merges an updated upstream into a topic branch. # # Lines starting with '#' will be ignored, and an empty message aborts # the commit.
Fixes #3494
TODO