DataSource for Entity Framework in WPF
DataSource for Entity Framework in WPF / The Power of Client Data Cache
In This Topic
    The Power of Client Data Cache
    In This Topic

    The Server-Side Filtering|document=WordDocuments\C1DataStudio-WPF.docx;topic=Server-Side Filtering example shows how the Entity Framework DataSource (EF DataSource) improves and simplifies working with the Entity Framework in applications, made possible by its client-side data cache, a key feature of EF DataSource. 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|document=WordDocuments\C1DataStudio-WPF.docx;topic=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. Why? Because the data is being retrieved from the memory 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 EF DataSource 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 the EF DataSource 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 control, 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 project in Server-Side Filtering|document=WordDocuments\C1DataStudio-WPF.docx;topic=Server-Side Filtering.

    1. Simply add a button to the form and add a handler for the code:
    Visual Basic
    Copy Code
    Private Sub button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
         c1DataSource1.ClientCache.SaveChanges()
    End Sub
    

     

    C#
    Copy Code
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        c1DataSource1.ClientCache.SaveChanges();
    

     

    1. 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. EF DataSource 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 writing a lot of code, 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. EF DataSource simplifies all of this for you while optimizing memory usage and performance.

    Client-side caching also makes possible other important features of EF DataSource, such as client-side queries and, especially, live views. Live Views|document=WordDocuments\C1DataStudio-WPF.docx;topic=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.