Getting access to query internals at runtime

A QuerySpec query's internal objects are hidden and only available to fetch logic. This is done to prevent pollution of the fluent interface. It can however sometimes be necessary to manipulate some elements in the query right before execution, e.g. you need to add a filter on a table based on the where / from clause specified.

To be able to do this, a couple of interfaces is available to you as well as a callback mechanism to be able to access the internals of a query object right before execution, after it has been prepared and setup for execution so all internal objects are finalized and ready.

The interfaces are IQuerySpec, implemented on QuerySpec, IDynamicQuery, implemented on DynamicQuery, and IEntityQuery, implemented on EntityQuery. The interfaces are implemented explicitly which means you have to cast to these interfaces to access their properties.

It's enough to cast to IDynamicQuery for dynamic queries and IEntityQuery for entity queries to access the IQuerySpec properties as well.

To be able to read the properties at the right moment, right before execution, a lambda (Action<IQuerySpec>) has to be assigned to the QuerySpec property OnPrepareForExecutionCallBack.

Example:

q.OnPrepareForExecutionCallBack = o =>
{
    // code here is executed when the query is about to be executed, 
    // after the query has been prepared.
    // 'o' is of type IQuerySpec, the base interface for IDynamicQuery and IEntityQuery.
    // E.g. if 'q' is a DynamicQuery, 'o' is of type IDynamicQuery
}
// execute 'q' here.