ComponentOne Data Source for Entity Framework
Design-Time Features / Working with DataSources in Code
In This Topic
    Working with DataSources in Code
    In This Topic

    Up to this point, we have been setting up data sources directly on the designer surface with very little code. DataSource for Entity Framework has made it very easy, but sometimes you want or need to do everything in code.  C1DataSource 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 C1DataSource made it simple to perform without code, but now we’ll try it in run-time code.

    To begin using C1DataSource 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:

    To write code in Visual Basic

    Visual Basic
    Copy Code

    Imports C1.Data.Entities

     Public Class Program
         Public Shared ClientCache As EntityClientCache
         Public Shared ObjectContext As NORTHWNDEntities

         <STAThread()> _
         Shared Sub Main()

             ObjectContext = New NORTHWNDEntities
             ClientCache = New EntityClientCache(ObjectContext)

             Application.EnableVisualStyles()
             Application.SetCompatibleTextRenderingDefault(False)
             Application.Run(New MainForm())
         End Sub
     End Class

       

    To write code in C#

    C#
    Copy Code

    using C1.Data.Entities;

     static class Program
     {
         public static EntityClientCache ClientCache;
         public static NORTHWNDEntities ObjectContext;

         [STAThread]
         static void Main()
         {
             ObjectContext = new NORTHWNDEntities();
             ClientCache = new EntityClientCache(ObjectContext);

             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
             Application.Run(new MainForm());
     }

    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 C1DataSource.

    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:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Imports C1.Data.Entities
       Imports C1.Data

       Public Class DataSourcesInCode

           Private _scope As EntityClientScope

           Public Sub New()

               InitializeComponent()

               _scope = Program.ClientCache.CreateScope()

               Dim viewCategories As ClientView(Of Category) = _scope.GetItems(Of Category)()

               comboBox1.DisplayMember = "CategoryName"
               comboBox1.ValueMember = "CategoryID"
               comboBox1.DataSource = viewCategories

               BindGrid(viewCategories.First().CategoryID)
           End Sub

           Private Sub BindGrid(categoryID As Integer)
               dataGridView1.DataSource =
                       (From p In _scope.GetItems(Of Product)().AsFiltered(Function(p As Product) p.CategoryID.Value = categoryID)
                        Select New With
                        {
                            p.ProductID,
                            p.ProductName,
                            p.CategoryID,
                            p.Category.CategoryName,
                            p.SupplierID,
                            .Supplier = p.Supplier.CompanyName,
                            p.UnitPrice,
                            p.QuantityPerUnit,
                            p.UnitsInStock,
                            p.UnitsOnOrder
                        }).AsDynamic()
           End Sub


           Private Sub btnSaveChanges_Click(sender As System.Object, e As System.EventArgs) Handles btnSaveChanges.Click
               Program.ClientCache.SaveChanges()
           End Sub

           Private Sub comboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles comboBox1.SelectedIndexChanged
               If comboBox1.SelectedValue IsNot Nothing Then
                   BindGrid(CType(comboBox1.SelectedValue, Integer))
               End If
           End Sub
       End Class

       

      To write code in C#

      C#
      Copy Code
      namespaceTutorialsWinForms
       {
           using C1.Data.Entities;
           using C1.Data;

           public partial class DataSourcesInCode : Form
           {
               private EntityClientScope _scope;

               public DataSourcesInCode()
               {
                   InitializeComponent();

                   _scope = Program.ClientCache.CreateScope();

                   ClientView<Category> viewCategories =_scope.GetItems<Category>();

                   comboBox1.DisplayMember = "CategoryName";
                   comboBox1.ValueMember = "CategoryID";
                   comboBox1.DataSource = viewCategories;

                   BindGrid(viewCategories.First().CategoryID);
               }

               private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
               {
                   if (comboBox1.SelectedValue != null)
                       BindGrid((int)comboBox1.SelectedValue);
               }

               private void BindGrid(int categoryID)
               {
                   dataGridView1.DataSource =
                       (from p in _scope.GetItems<Product>().AsFiltered(
                            p => p.CategoryID == categoryID)
                        select new
                        {
                            p.ProductID,
                            p.ProductName,
                            p.CategoryID,
                            CategoryName = p.Category.CategoryName,
                            p.SupplierID,
                            Supplier = p.Supplier.CompanyName,
                            p.UnitPrice,
                            p.QuantityPerUnit,
                            p.UnitsInStock,
                            p.UnitsOnOrder
                        }).AsDynamic();
               }

               private void btnSaveChanges_Click(object sender, EventArgs e)
               {
                   Program.ClientCache.SaveChanges();
               }

           }
       }

             
    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:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Dim viewCategories As ClientView(Of Category) = _scope.GetItems(Of Category)()
       

    To write code in C#

    C#
    Copy Code
    ClientView<Category> viewCategories = _scope.GetItems<Category>();
       

    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>).

    To write code in Visual Basic

    Visual Basic
    Copy Code
    From p In _scope.GetItems(Of Product)().AsFiltered(Function(p As Product) p.CategoryID.Value = categoryID)
       

    To write code in C#

    C#
    Copy Code
    from p in _scope.GetItems<Product>().AsFiltered(p => p.CategoryID == categoryID)

    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 C1DataSource. 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:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Select New With
     {
         p.ProductID,
         p.ProductName,
         p.CategoryID,
         p.Category.CategoryName,
         p.SupplierID,
         Supplier = p.Supplier.CompanyName,
         p.UnitPrice,
         p.QuantityPerUnit,
         p.UnitsInStock,
         p.UnitsOnOrder
     }
       

    To write code in C#

    C#
    Copy Code
    select new
    {
         p.ProductID,
         p.ProductName,
         p.CategoryID,
         CategoryName = p.Category.CategoryName,
         p.SupplierID,

        Supplier = p.Supplier.CompanyName,

        p.UnitPrice,
         p.QuantityPerUnit,
         p.UnitsInStock,
         p.UnitsOnOrder
     };

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

            select p;