Implementing a GetQuery Handler for a Single Entity
Bloqs allows you to implement a GetQuery handler to retrieve a single entity from the system. This is useful if you want to control or customize how an entity is fetched.
Key Points
Use
[QueryHandler<Activity>]to register your handler for theActivityentity.The handler must implement
IQueryHandler<EntityGetQuery, SingleQueryResult>.If no GetQuery handler is implemented, the default behavior retrieves the entity by its ID without filtering.
Sample Implementation
using Bloqs.Api.Engine.DataAccess;
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 ActivityGetQueryHandler(IDataServiceCreator dataServiceCreator)
: IQueryHandler<EntityGetQuery, SingleQueryResult>
{
public async Task<SingleQueryResult> HandleAsync(
EntityGetQuery query,
CancellationToken cancellationToken = default
)
{
var dataService = dataServiceCreator.GetDataService<Activity>(
DataServiceCreatorArgs.FromQuery<Activity>(query)
);
var entity = await dataService.GetAsync(query.EntityId);
return new SingleQueryResult { Entity = entity };
}
}Notes
[QueryHandler<Activity>]registers this class as the handler for singleActivityqueries.GetAsyncfetches the entity by its ID directly from the data service.You can customize this method if you need additional filtering, access control, or transformations before returning the entity.
This handler runs in the API project, serving queries from the UI or other services.
Last updated