Skip to content

Can you support delegated queries? #29

@tanshanli

Description

@tanshanli

Hi,
First of all, thank you for your great work on this project! I have a suggestion that could significantly improve the developer experience for common scenarios.
Background
In real-world development, it is very common to write queries like:

await _repository.FirstOrDefaultAsync(s => s.Name == "tome");

However, in the current design, methods such as FirstOrDefaultAsync, SingleOrDefaultAsync, etc., only accept an IQuery parameter. This means that for even the simplest queries, I have to write extra code to initialize a Query object, like this:

var query = QueryBuilder.SingleResultQuery<T>.New().AndFilter(s => s.Name == "tome");
await _repository.FirstOrDefaultAsync(query);

This adds unnecessary boilerplate and reduces code readability and efficiency, especially since simple predicate-based queries are extremely common in most applications.
Suggestion
Could you please consider adding overloads for these methods that accept Expression<Func<T, bool>> as a parameter?
For example:

Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default);

Internally, this overload could simply create a Query and delegate to the existing implementation, ensuring backward compatibility and code reuse.
Benefits
Improved developer experience: Enables concise and readable code for simple queries.
Consistency: Aligns with the usage patterns of popular ORMs and repository frameworks.
No breaking changes: Existing functionality remains intact; this is purely an additive improvement.
Example
With this change, both of the following would be supported:

// Simple query
await _repository.FirstOrDefaultAsync(s => s.Name == "tome");

// Complex query
var query = QueryBuilder.SingleResultQuery<T>.New()
    .AndFilter(s => s.Name == "tome")
    .OrderBy(s => s.Id)
    .Include(...);
await _repository.FirstOrDefaultAsync(query);

Conclusion
I believe this enhancement would make the repository much more ergonomic and developer-friendly for everyday use.
Thank you for considering this suggestion!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions