Design-Time Features > Client-Side Caching |
The Server-Side Filtering example shows how the C1DataSource improves and simplifies working with the Entity Framework in applications, made possible by its client-side data cache, a key feature of C1DataSource. It enables several important enhancements and makes writing application code much easier.
Let’s start with a performance enhancement that can be seen in the Server-Side Filtering example. When the user switches between categories, the grid loads products associated with them. The first time a category is chosen, there is a slight delay when the relevant data is retrieved. On subsequent occasions, when the same category is chosen, data retrieval is virtually instantaneous. This is because the data is being retrieved from the client memory data cache (EntityDataCache) rather than the server.
Note: The EntityDataCache is actually smarter still, determining that a return trip to the server can be avoided, even in more complex cases where the queries may not be identical but can be fulfilled from the results of other queries already contained therein. |
This performance enhancement may not be obvious if you are working on a single machine where no network interaction is required, but in real world applications it can make all the difference between a sluggish application that calls the server on every user action and a crisp interface with no delays.
The second important enhancement is in memory management. You might think based on what you’ve read and observed to date that the EntityDataCache continually accumulates data throughout its existence. If this were the case you would very quickly witness severe performance degradation. In reality the EntityDataCache is keeping track of what is stored within it and as data is no longer required is releasing it performing a self-cleansing operation at the same time. All of this is being done without the need for you to add extra code, and more importantly still it is ensuring that data integrity is being preserved at all times. Data won’t be released if required by other data, nor will data that has been altered in some way without those alterations having being saved. This also relates to performance, because getting rid of unnecessary objects in memory improves performance; and vice versa, keeping large numbers of obsolete objects in memory leads to performance degradation.
We mentioned before how C1DataSource simplifies context management by eliminating the need to create multiple data contexts thanks to the client cache. Now we should explain what the EntityDataCache actually is and how we can further use this information to our advantage.
The EntityDataCache is essentially the context. In terms of C1DataSource namespaces, the cache is the C1.Data.Entities.EntityClientCache class, and it is in one-to-one correspondence with ObjectContext through its ObjectContext property. Both the cache and its underlying ObjectContext are created for you automatically if you use the C1DataSource component ; however, you can create them explicitly and set the C1DataSource.ClientCache property in code, if necessary.
To see how simple application code becomes thanks to the client-side cache, let’s add the functionality of saving modified data to the Server-Side Filtering project.
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub btnSaveChanges_Click(sender As System.Object, e As System.EventArgs) C1DataSource1.ClientCache.SaveChanges() End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
private void btnSaveChanges_Click(object sender, EventArgs e) { c1DataSource1.ClientCache.SaveChanges(); } |
Client-side caching also makes possible other important features of C1DataSource, such as client-side queries and, especially, live views. Live Views is a feature that allows you to replace much of the complex application coding with simple data binding, which we'll learn more about later.