ComponentOne Data Source for Entity Framework
Design-Time Features / Client-Side Caching
In This Topic
    Client-Side Caching
    In This Topic

    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.

    1. Simply add a button, btnSaveChanges,  to the form and add a handler for the code:

      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();
       }
    2. Save, build and run the application.
      • Select a category and then make some changes to the product information in the grid. 
      • Select another category (and possibly a third) and again makes changes to the product details in the grid.
      • Click the button you added, close the application, reopen it and select the same categories as before.
      • Observe how the changes you made have been saved. C1DataSource has provided, via the EntityDataCache, a way to alter the product details of several categories' products without the need to save those changes each time different category is selected. To achieve this effect without C1DataSource, you would either need to write a lot of code, or you’d need to keep the entities (the product details) from different categories in the same context. This would waste memory without releasing it, creating a memory leak. C1DataSource simplifies all of this for you while optimizing memory usage and performance.

    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.