DataSource for Entity Framework in WPF > 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. Entity Framework DataSource (EF DataSource) has made it very easy, but sometimes you want or need to do everything in code. EF DataSource 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. 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 EF DataSource made it simple to perform without code, but now we’ll try it in run-time code.
To begin using EF DataSource 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 create it explicitly using the following code:
Visual Basic |
Copy Code
|
---|---|
Imports C1.Data.Entities Class Application Public Shared ClientCache As EntityClientCache Private Sub Application_Startup(sender As Object, e As System.Windows.StartupEventArgs) Handles Me.Startup ClientCache = EntityClientCache.GetDefault(GetType(NORTHWNDEntities)) End Sub End Class |
C# |
Copy Code
|
---|---|
using C1.Data.Entities; public partial class App : Application { public static EntityClientCache ClientCache; public static NORTHWNDEntities ObjectContext; protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); ObjectContext = new NORTHWNDEntities(); ClientCache = new EntityClientCache(ObjectContext); } } |
This code creates a single application-wide (static) ObjectContext and associates it with EntityClientCache. As noted previously in The Power of Client Data Cache|document=WordDocuments\C1DataStudio-WPF.docx;topic=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 EF DataSource.
To perform server-side filtering in run-time code, follow these steps:
Visual Basic Copy Code Imports C1.Data.Entities Imports C1.Data Public Class DataSourcesInCode Private _scope As EntityClientScope Public Sub New() ' This call is required by the designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. _scope = Application.ClientCache.CreateScope() Dim viewCategories As ClientView(Of Category) = _scope.GetItems(Of Category)() comboBox1.DisplayMemberPath = "CategoryName" comboBox1.ItemsSource = viewCategories BindGrid(viewCategories.First().CategoryID) End Sub Private Sub BindGrid(categoryID As Integer) dataGrid1.ItemsSource = (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 comboBox1_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles comboBox1.SelectionChanged If comboBox1.SelectedValue IsNot Nothing Then BindGrid(CType(comboBox1.SelectedValue, Category).CategoryID) End If End Sub Private Sub btnSaveChanges_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnSaveChanges.Click Application.ClientCache.SaveChanges() End Sub End Class
C# Copy Code using C1.Data.Entities; using C1.Data; public partial class DataSourcesInCode : Window { private EntityClientScope _scope; public DataSourcesInCode() { InitializeComponent(); _scope = App.ClientCache.CreateScope(); ClientView<Category> viewCategories = _scope.GetItems<Category>(); comboBox1.DisplayMemberPath = "CategoryName"; comboBox1.ItemsSource = viewCategories; BindGrid(viewCategories.First().CategoryID); } private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (comboBox1.SelectedValue != null) BindGrid(((Category)comboBox1.SelectedValue).CategoryID); } private void BindGrid(int categoryID) { dataGrid1.ItemsSource = (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, RoutedEventArgs e) { App.ClientCache.SaveChanges(); } }
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 |
Copy Code
|
---|---|
Dim viewCategories As ClientView(Of Category) = _scope.GetItems(Of Category)() |
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>).
Visual Basic |
Copy Code
|
---|---|
From p In _scope.GetItems(Of Product)().AsFiltered(Function(p As Product) p.CategoryID.Value = categoryID) |
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 EF DataSource. 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|document=WordDocuments\C1DataStudio-WPF.docx;topic=Simplifying MVVM topic.
So, in summary, we replicated in code what we did on the design surface with C1DataSource in Server-Side Filtering|document=WordDocuments\C1DataStudio-WPF.docx;topic=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|document=WordDocuments\C1DataStudio-WPF.docx;topic=Customizing View by adding a Select to our LiveLinq statement:
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 ; |
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;