Every time an immediate method like ToList or FirstOrDefault is invoked on a query, a database round trip is made to retrieve data. While the most application doesn’t have performance issues with making multiple round trips, batching multiple queries into one can be critical for some heavy traffic applications for scalability. Major ORM like NHibernate had this feature for a long time but, unfortunately for Entity Framework users, batching queries is only available through third party libraries.
EF+ Query Future opens up all batching future queries features for Entity Framework users.
To batch multiples queries, simply append Future or FutureValue method to the query. All future queries will be stored in a pending list, and when the first future query requires a database round trip, all queries will be resolved in the same SQL command.
EF+ Query Future
Query Future delays the execution of a query returning an IEnumerable.
EF+ Query FutureValue
Query FutureValue delays the execution of the query returning a result.
EF+ Query FutureValue Deferred
Immediate resolution methods like Count() and FirstOrDefault() cannot use future methods since it executes the query immediately.
EF+ Query Deferred has been created to resolve this issue. The resolution is now deferred instead of being immediate which lets you use FutureValue and get the expected result.
Real Life Scenarios
- Multi tables information scenario: Client and all related information (order, invoice, etc.) must be loaded.
- Paging Scenario: The first ten posts must be returned but you also need to know the total numbers of posts
Behind the code
- All queries from a context using query future are added to a batch list.
- When the first database round trip is required:
- All sql commands are combined into one sql command.
- The sql command is executed and a data reader is returned.
- For every result in the data reader, the result is set to the corresponding query future.
- Provider not supported are added on demand
- Provider which doesn’t support multiple statements like SQL Compact cannot be supported
- EF+ Query Future: Full version or Standalone version
- Database Provider: SQL Server
- Entity Framework Version: EF5, EF6, EF Core
- Minimum Framework Version: .NET Framework 4
As we saw, EF+ Query Future follows a good architecture principle:
- Flexible: Future, FutureValue and FutureValue deferred make it possible to use it in any kind of scenario.
- Maintainable: The easy to use API, documentation and available source code allows new developers to quickly understand this feature.
- Scalable: Query Future gets only better as the number of user/traffic grows by drastically reducing database round trips.
Need help getting started? firstname.lastname@example.org
We welcome all comments, ideas and suggestions to improve our library.