Implementing a Query Handler
In Bloqs, you can implement custom query handlers to provide specialized queries for entities. This allows you to control exactly which data is returned and apply filters directly at the storage level.
Key Points
Use
[QueryHandler<Activity>]to register your query handler for theActivityentity.The handler must implement
IQueryHandler<EntityQuery, MultiQueryResult>.You can use the
QueryAsyncoverload that accepts an expression to filter data on the storage level.If you do not implement a query handler, Bloqs will perform the default query, which simply returns all data for that entity.
Sample Implementation
using Bloqs.Api.Engine.DataAccess;
using Bloqs.App.Engine;
using Bloqs.App.Engine.Queries;
using Bloqs.App.Engine.Queries.Query;
using Templates.SampleApp.Models.Data;
namespace Templates.SampleApp.Api.Queries;
[QueryHandler<Activity>]
public class ActivityQueryHandler(IDataServiceCreator dataServiceCreator)
: IQueryHandler<EntityQuery, MultiQueryResult>
{
public async Task<MultiQueryResult> HandleAsync(
EntityQuery query,
CancellationToken cancellationToken = default
)
{
var dataService = dataServiceCreator.GetDataService<Activity>(
DataServiceCreatorArgs.FromQuery<Activity>(query)
);
// Simple filter: only activities with Prio > 5
var activities = await dataService.QueryAsync(a => a.Prio > 5);
return new MultiQueryResult { Entities = activities.ToList() };
}
}Notes
[QueryHandler<Activity>]registers this class as the query handler forActivity.Using
QueryAsyncwith an expression filters the data directly in the storage, improving performance.This query handler runs in the API project, and the results can be consumed by the UI or other services.
If no custom query handler is implemented, the default query returns all entities without filtering.
Last updated