Future Queries
Widget Connector | ||||||
---|---|---|---|---|---|---|
|
With PLINQO, the future is now! Build up a list of queries for the data that you need and the first time any of the results are accessed, PLINQO will retrieve all the data in one round trip to the database server. Reducing the number of trips to the database is a great optimization that makes for smoother, faster running applications. Using this feature is as simple as appending .Future() to the end of your queries. To use the Future Queries please make sure that you are importing the CodeSmith.Data.Linq Namespace. Here is a quick sample.
...
Queuing up for the future has never been easier!
The Details
Future queries are created with the extension methods Future(), FutureFirstOrDefault(), or FutureCount().
...
In this example, we have a common senerio where you want to page a list of tasks. In order for the GUI to setup the paging control, you need a total count. With Future, we can batch together the queries to get all the data in one database call.
Future Query Cache
Future queries can also be cached using the same syntax of FromCache extension method. Each query is cached separately to provide more flexibility.
var db = new TrackerDataContext(); // cache for 120 seconds CacheSettings cache = new CacheSettings(120); // build up queries var q1 = db.User .ByEmailAddress("one@test.com") .FutureCache(cache); var q2 = db.Task .Where(t => t.Summary == "Test") .FutureCache(cache); // this triggers the loading of all the future queries var users = q1.ToList();
How It Works
Future queries work by creating the appropriate IFutureQuery object that keeps the IQuerable. The IFutureQuery object is then stored in IFutureContext.FutureQueries list on the DataContext that created the query. The DataContext must implement IFutureContext. Then, when one of the IFutureQuery objects is enumerated, it calls back to IFutureContext.ExecuteFutureQueries() via the LoadAction delegate. ExecuteFutureQueries builds a batch query from all the stored IFutureQuery objects. Finally, all the IFutureQuery objects are updated with the results from the query.
Limitations
Does not support anonymous types.