LiveLinq Overview
LiveLinq is a class library that augments the functionality of LINQ in two related directions:
• It makes LINQ faster
LiveLinq uses indexing and other optimizations to speed up LINQ queries in memory. Speed gains can be hundreds and even thousands of times on queries with high selectivity conditions. Typical/average gains are lower but still significant; a 10-50 times speedup can be considered typical.
• It adds support for live views to LINQ
A live view is a LINQ query result that is kept constantly up-to-date without re-populating it every time its base data changes. This makes LiveLinq extremely useful in common data-binding scenarios where objects are edited and may be filtered in or out of views, have their associated subtotals updated, and so on. In old jargon, one could say that LINQ queries correspond to snapshots, while LiveLinq views correspond to dynasets. Since live views automatically react to changes, they greatly widen the sphere of declarative programming, not only in data binding and GUI but in many other programming scenarios as well.
The scope of LiveLinq
LiveLinq implements three LINQ varieties:
• LINQ to Objects
• LINQ to XML
• LINQ to DataSet
In other words, its scope is LINQ in memory. The current LiveLinq version does not offer an alternative to LINQ to databases. However, this does not prevent you from using LiveLinq with data retrieved to memory from a database. For example, you can retrieve data from a database to a dataset in memory using ADO.NET and then use LiveLinq to DataSet. You can also retrieve objects from a database using LINQ to SQL or Entity Framework and then operate on that data using LiveLinq and send changes to the database again using Entity Framework or another framework of your choice.
LiveLinq and LINQ
LiveLinq has the same syntax as the standard LINQ. The only change necessary is to wrap your data source by applying to it an extension method AsIndexed() (for indexing) or AsLive() (for live views).
How the two parts of LiveLinq are related to each other
The two areas of LiveLinq functionality, indexing and live views can interoperate but are independent. It is not necessary to define indexes if you need to create a live view. A view is populated initially by executing a query, so, if the query can be optimized using indexing, the view will be populated faster. But after its initial population, changes to the view are made using special optimization techniques (known as Incremental View Maintenance) that don't require the user to explicitly define indexes.
Faster LINQ with Indexing
LiveLinq contains an indexing framework that it uses for optimizing query performance (not available in Silverlight). For example, by defining an index by ProductID, we can dramatically speed up queries like
from p in products where p.ProductID == 100
because it will use the index to access the requested product directly instead of going through the entire large collection in search of a single product.
Same indexes can also be used programmatically, in code, even without LINQ, for various kinds of searches, including range search and others.
Declarative programming with Live Views
Live (real) data binding
LiveLinq adds the concept of view to LINQ. A view is a query with a resultset that remains live, dynamic after it is initially populated by executing the query. A standard LINQ query is a snapshot in the sense that its result list does not change when you change the underlying (base) data. So it can't be used for full-featured data binding. A live view is automatically kept in sync with base data, so it enables full data binding to LINQ queries.
Moreover, many views are updatable themselves; you can modify properties in their objects and add and delete objects directly in the view, see Updatable Views. So, a view can be modifiable in both directions: changes in base data propagate to the view and changes in the view propagate to base data (the latter pursuant to usual conditions of updatability).
This is full-featured two-way data binding.
Reactive = "View-Oriented" = Declarative Programming
Data binding, which is mostly used in GUI, is a very important part of live views functionality, but not the only one. More generally, live views enable a declarative style of programming which we tentatively call "view-oriented programming". Non-GUI, batch processing code can also be made declarative using live views.
Enabling technology: Incrementality
When a change occurs in base data, live views update themselves in a smart, fast way, not simply repopulating themselves from scratch. The changes are made locally, incrementally, calculating the delta in the view from the delta in the base data. In most cases, it allows you to propagate the change from base data to the view very fast. This is a key component of LiveLinq: its enabling technology, based on an area of Computer Science known as Incremental View Maintenance. But the user does not need to do anything to enable this optimization, as it is done entirely beneath the hood, transparent to the user.