Working with Data Sources in Code

Up to this point, we have been setting up data sources directly on the designer surface with very little code. ComponentOne Studio for Entity Framework (SEF) has made it very easy, but sometimes you want or need to do everything in code. SEF makes this possible as well. Everything we did previously can be done at run time in code.

An obvious way to go about this would be to use the ClientViewSource object that we have, in effect, been setting up in the designer as elements of the ViewSourceCollection of a C1DataSource, given that it can be created on its own without a C1DataSource. We could, however, take a step further back and use a lower level class ClientView<T>. This would provide full control over loading data from the server and, since it is derived from C1.LiveLinq.LiveViews.View<T>, we can apply any LiveLinq operators to it. The ability to bind this to any GUI control whose datasource can be set to a View<T> also means that we’ll end up with a fully editable view of our data.

Server-side filtering is, perhaps, the most common operation, because no one usually wants entire database tables brought to the client unrestricted. Earlier we saw how SEF made it simple to perform without code, but now we’ll try it in run-time code.

To begin using SEF in run-time code without a C1DataSource, add a few lines to our project's main class to create a global client-side data cache. When we used C1DataSource, it was created for us behind the scenes. Now we can create it explicitly using the following code:

      Visual Basic

      C#

This code creates a single application-wide (static) ObjectContext and associates it with EntityClientCache. As noted previously in The Power of Client Data Cache topic, the ability to have a single context (and cache) for the entire application is a great simplification made possible by SEF.

To perform server-side filtering in run-time code, follow these steps:

1.   Add a new form using the project created to demonstrate Customizing View.

2.   Add a grid (dataGridView1), a combo box (comboBox1), and a button (btnSaveChanges) to the form.

3.   Add the following code to the form class:

      Visual Basic

      C#

4.   Save, build and run your application. You should see similar results to those you saw in the Server-Side Filtering example, the only difference being that this time we’ve implemented it all in code.

Let’s take a closer look at some of the code we’ve just written.

The private field _scope is the form’s gateway to the global data cache. It is a pattern we recommend you follow in all the forms where you do not employ a C1DataSource component directly, as that does this for you automatically. It ensures that the entities the form needs stay in the cache while the form is alive, and that they are automatically released when all forms (scopes) holding them are released.

Creating a view showing all categories for the combo box is simple:

      Visual Basic

      C#

To create the view to which the grid is bound that only provides those products associated with the chosen category in the combo box required one additional operator; AsFiltered(<predicate>).

      Visual Basic

      C#

Note that when this query is executed, the result does not necessarily require a round trip to the server to retrieve the products requested. The cache is examined first to see if it already contains the requested data, either because the required data has already been requested once before within this form or from another form in the application. Or, possibly a completely separate query run elsewhere in the application had requested that all products be returned, so the cache would already have all the product data. Again, this is a fundamental strength of SEF. By providing your application with a global cache of data, its performance is continually improved throughout its lifetime.

Here we chose to create a new view, and bind the grid to it, every time the user selects a new category in the combo box (see the combo box’s SelectedValueChanged event). However, we could have avoided the need to create new views all the time and instead created one single view using a special BindFilterKey, which we'll learn more about in the Simplifying MVVM topic.

So, in summary, we replicated in code what we did on the design surface with C1DataSource in Server-Side Filtering. We have even thrown in a little extra; we customized the fields shown in the grid columns as we did in Customizing View by adding a Select to our LiveLinq statement:

      Visual Basic

      C#

Had we just wanted the raw product data returned from the table without any special formatting, we could have simply said;

        selectp;


Send us comments about this topic.
Copyright © GrapeCity, inc. All rights reserved.