Customizing View

In many situations, you may want to use custom views that do not correspond directly to the tables and views provided by the database. LINQ is the perfect tool for these situations. It allows you to transform raw data using your language of choice using query statements that are flexible, concise, and efficient. ComponentOne Studio for Entity Framework (SEF) makes LINQ even more powerful by making LINQ query statements live. That’s why its LINQ implementation is called LiveLinq. We will show the power of LiveLinq in Live Views. For now suffice it to say that you can transform your view to shape it whatever way you want using LINQ operators without losing full updatability and bindability.

Let’s try, for example, one of the LINQ operators: Select (also called projection), to customize the fields (properties) of our view.

To customize a view, follow these steps:

1.   Using the project we created to demonstrate Large Datasets: Paging, add a new form with a C1DataSource component using the same DomainContextTypeName as before. Note that you can make this the startup form to save time when you run the project.

2.   Create a ViewSource in the ViewSourceCollection editor. Use the GetProducts table in our sample database.

3.   Add a DataGrid to the page designer and, as before, set its AutoGenerateColumns property to True. But this time we won’t bind the grid to C1DataSource in XAML. Instead, we will bind it in code to implement a custom view.

4.   Create a custom live view and bind the grid to that view with the following code:

      Visual Basic

      C#

Here c1DataSource1["GetProducts"] is a ClientCollectionView object. It is the view that is created by the view source that we set up in the designer (if you need to access the view source itself in code, it is also available, as c1DataSource.ViewSources[“GetProducts”]). The AsLive<Web.Product>() extension method call is needed to specify the item type of the view (Web.Product is the class generated for the Product entity in DomainService) so LiveLinq operators can be applied to it. The result, c1DataSource1["GetProducts"].AsLive<Web.Product>(), is a View<Web.Product>. The C1.LiveLinq.LiveViews.View is the main class of LiveLinq, used for client-side live views. LINQ operators applied to live views preserve their updatability and bindability. They are the same usual LINQ operators, but the fact that they are applied to a live view gives them these additional features that are critically important for data binding applications.

Note 1: AsDynamic() must be applied to this view because its result selector (the select new… code) uses an anonymous class. This is a minor LiveLinq limitation, only for anonymous classes. If you forget to add AsDynamic() to such view, you will be reminded by an exception.

Note 2: At this point we will need to add the following two attributes to the AssemblyInfo file in our project:

      Visual Basic

      C#

These attributes give LiveLinq access to non-public members of our assembly, If we don’t add them, LiveLinq will remind us at run time by throwing an exception. This is necessary only in Silverlight and only if we use anonymous or non-public types in LiveLinq queries.

5.   Since we want to show the value of p.Category.CategoryName, we need to make sure that for every product entity, its corresponding Category entity is fetched to the client. This can be done either by fetching all Category entities to the client in a separate query or by telling RIA Services that we want it to fetch Category with every Product. Let’s choose the second option and use the Include attribute in the DomainService1.cs and DomainService1.metadata.cs files as we already did before.

In DomainService1.cs, add the Include operator to the query in the GetProducts method:

      Visual Basic

      C#

6.   In DomainService1.metadata.cs, add the [Include] attribute before the corresponding property:

      Visual Basic

      C#

7.   Save, build and run the application. The grid now shows the columns we defined in the ‘select’ clause of our LiveLinq view. Note also that all columns are modifiable. It may not seem like a big deal; however, it is an important feature that would be difficult to implement on your own for this customized view, especially when adding and deleting rows, which, as you can see here, is also supported.  

8.   To add or delete rows, add two buttons to the page executing the following code:

      Visual Basic

      C#

This is needed only because Microsoft DataGrid does not provide a built-in interface for adding new rows.

As we just saw, LiveLinq operators preserve updatability. But we also said that they preserve bindability. Bindability is achieved because the views are always ‘live’; they aren’t simple snapshots of static data. The views "feel" and automatically reflect changes in their source data. We will see this remarkable feature in action in the Live Views topic.


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