Skip to content
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

Enable Selection middleware for single item results #1536

Closed
TheJayMann opened this issue Mar 9, 2020 · 2 comments
Closed

Enable Selection middleware for single item results #1536

TheJayMann opened this issue Mar 9, 2020 · 2 comments
Assignees
Milestone

Comments

@TheJayMann
Copy link

So far, the selection middleware is working well for most of my needs. One exception is where a query is designed to return a single item, but still wanting to use the selection middleware to limit the data retrieved, which is especially useful when the item contains multiple list properties.

I can use the following inline middleware immediately before the UseSelection middleware to make this work with any IQueryable as well as query providers supporting IAsyncEnumerable without taking a dependency on EntityFrameworkCore. This is based on the idea of SingleOrDefault, but a FirstOrDefault version can easily be derived.

                .Use(next => async ctx => {
                    await next(ctx);
                    if (ctx.Result is IQueryable<Profile> profilesQuery) {
                        if (profilesQuery.Take(2) is IAsyncEnumerable<Profile> profiles) {
                            var foundProfile = false;
                            Profile? currentProfile = default;
                            await foreach (var profile in profiles) {
                                if (foundProfile) throw new InvalidOperationException("Sequence contains more than one element");
                                foundProfile = true;
                                currentProfile = profile;
                            }
                            ctx.Result = currentProfile;
                        }
                        else {
                            ctx.Result = profilesQuery.SingleOrDefault();
                        }
                    }
                })

The above code can be simplified if willing to take a dependency on System.Linq.Async. I'd be hesitant in a generic sense of taking a dependency on EntityFrameworkCore, as I believe this should be designed to work with any IQueryable.

@michaelstaib michaelstaib added the 🔍 investigate Indicates that an issue or pull request needs more information. label Mar 9, 2020
@michaelstaib michaelstaib added this to the HC-10.4.0 milestone Mar 14, 2020
@michaelstaib michaelstaib self-assigned this Mar 14, 2020
@michaelstaib michaelstaib added enhancement and removed 🔍 investigate Indicates that an issue or pull request needs more information. labels Mar 14, 2020
PascalSenn added a commit that referenced this issue Mar 14, 2020
@PascalSenn
Copy link
Member

@TheJayMann
Copy link
Author

Everything appears to be working with regards to this new feature.

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

No branches or pull requests

3 participants