Skip to content

Commit ddd5d4a

Browse files
JMJM
authored andcommitted
Modified the sample app to show how to limit the number of fields returned by the backend to improve fetching performance by limiting the amount of data transferred over the connection. Two methods are shown:
1. Define a FieldLayout in the XamDataPresenter with the subset of Fields that should be fetched 2. Set the ODataDataSource's new DesiredFields property to an array of strings that define the subset of schema fields that should be fetched.
1 parent 69e8551 commit ddd5d4a

3 files changed

Lines changed: 85 additions & 26 deletions

File tree

DataPresenter.DataSources.OData/DataPresenter.DataSources.OData.SampleApp/MainWindow.xaml

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,36 @@
8080
</Style>
8181
</StackPanel.Resources>
8282

83-
<!-- ODATA SERVICES-->
83+
<!-- Dropdown list of publicly accessible ODATA SERVICES-->
8484
<TextBlock Text="ODATA SERVICES" Style="{StaticResource HeaderText1}"/>
8585
<ComboBox x:Name="cboOdataSources" SelectionChanged="cboOdataSources_SelectionChanged" Style="{StaticResource ComboBoxText}">
86-
<local:ODataSourceListItem BaseUri="http://services.odata.org/V4/Northwind/Northwind.svc" EntitySet="Orders" Description="Northwind Orders"/>
87-
<local:ODataSourceListItem BaseUri="http://services.odata.org/V4/Northwind/Northwind.svc" EntitySet="Employees" Description="Northwind Employees"/>
88-
<local:ODataSourceListItem BaseUri="http://services.odata.org/V4/Northwind/Northwind.svc" EntitySet="Suppliers" Description="Northwind Suppliers"/>
89-
<local:ODataSourceListItem BaseUri="http://services.odata.org/AdventureWorksV3/AdventureWorks.svc" EntitySet="CompanySales" Description="AdventureWorks Company Sales"/>
90-
<local:ODataSourceListItem BaseUri="http://services.odata.org/AdventureWorksV3/AdventureWorks.svc" EntitySet="ProductCatalog" Description="AdventureWorks Product Catalog"/>
91-
<local:ODataSourceListItem BaseUri="http://services.odata.org/AdventureWorksV3/AdventureWorks.svc" EntitySet="WorkOrderRouting" Description="AdventureWorks Workorder Routing"/>
92-
<local:ODataSourceListItem BaseUri="http://data.scottsdaleaz.gov/Planning/BuildingPermits.svc" EntitySet="BuildingPermits" Description="Scottsdale Building Permits"/>
93-
<local:ODataSourceListItem BaseUri="http://data.scottsdaleaz.gov/Planning/BuildingPermits.svc" EntitySet="BuildingInspections" Description="Scottsdale Building Inspections"/>
94-
<local:ODataSourceListItem BaseUri="http://data.scottsdaleaz.gov/MunicipalServices/CapitalImprovementProjects.svc" EntitySet="Projects" Description="Scottsdale Capital Improvement Projects"/>
95-
<local:ODataSourceListItem BaseUri="http://data.scottsdaleaz.gov/Finance/BusinessLicenses.svc" EntitySet="BusinessLicenses" Description="Scottsdale Business Licenses"/>
96-
<local:ODataSourceListItem BaseUri="http://data.scottsdaleaz.gov/Purchasing/Solicitations.svc" EntitySet="Solicitations" Description="Scottsdale Purchasing Solicitations"/>
97-
<local:ODataSourceListItem BaseUri="http://packages.nuget.org/v1/FeedService.svc" EntitySet="Packages" Description="NuGet Packages"/>
98-
<local:ODataSourceListItem BaseUri="http://odata.research.microsoft.com/odata.svc" EntitySet="Projects" Description="Microsoft Research - Projects"/>
99-
<local:ODataSourceListItem BaseUri="http://odata.research.microsoft.com/odata.svc" EntitySet="Publications" Description="Microsoft Research - Publications"/>
86+
<local:DataSourceConfigurationInfo BaseUri="http://services.odata.org/V4/Northwind/Northwind.svc" EntitySet="Orders" Description="Northwind Orders"/>
87+
<local:DataSourceConfigurationInfo BaseUri="http://services.odata.org/V4/Northwind/Northwind.svc" EntitySet="Employees" Description="Northwind Employees"/>
88+
<local:DataSourceConfigurationInfo BaseUri="http://services.odata.org/V4/Northwind/Northwind.svc" EntitySet="Suppliers" Description="Northwind Suppliers"/>
89+
<local:DataSourceConfigurationInfo BaseUri="http://services.odata.org/AdventureWorksV3/AdventureWorks.svc" EntitySet="CompanySales" Description="AdventureWorks Company Sales"/>
90+
<local:DataSourceConfigurationInfo BaseUri="http://services.odata.org/AdventureWorksV3/AdventureWorks.svc" EntitySet="ProductCatalog" Description="AdventureWorks Product Catalog"/>
91+
<local:DataSourceConfigurationInfo BaseUri="http://services.odata.org/AdventureWorksV3/AdventureWorks.svc" EntitySet="WorkOrderRouting" Description="AdventureWorks Workorder Routing"/>
92+
<local:DataSourceConfigurationInfo BaseUri="http://data.scottsdaleaz.gov/Planning/BuildingPermits.svc" EntitySet="BuildingPermits" Description="Scottsdale Building Permits">
93+
<!-- Limit the amount of data fetched from this OData service by setting the DesiredFIelds property. Refer to
94+
the cboOdataSources_SelectionChanged event handler code in MainWindow.xaml.cs to see how we apply these settings when
95+
we create a new ODataDataSource instance. -->
96+
<local:DataSourceConfigurationInfo.DesiredFields>
97+
<x:Array Type="sys:String">
98+
<sys:String>PermitNumber</sys:String>
99+
<sys:String>PermitStatus</sys:String>
100+
<sys:String>IssueDate</sys:String>
101+
<sys:String>Address</sys:String>
102+
<sys:String>Zone</sys:String>
103+
</x:Array>
104+
</local:DataSourceConfigurationInfo.DesiredFields>
105+
</local:DataSourceConfigurationInfo>
106+
<local:DataSourceConfigurationInfo BaseUri="http://data.scottsdaleaz.gov/Planning/BuildingPermits.svc" EntitySet="BuildingInspections" Description="Scottsdale Building Inspections"/>
107+
<local:DataSourceConfigurationInfo BaseUri="http://data.scottsdaleaz.gov/MunicipalServices/CapitalImprovementProjects.svc" EntitySet="Projects" Description="Scottsdale Capital Improvement Projects"/>
108+
<local:DataSourceConfigurationInfo BaseUri="http://data.scottsdaleaz.gov/Finance/BusinessLicenses.svc" EntitySet="BusinessLicenses" Description="Scottsdale Business Licenses"/>
109+
<local:DataSourceConfigurationInfo BaseUri="http://data.scottsdaleaz.gov/Purchasing/Solicitations.svc" EntitySet="Solicitations" Description="Scottsdale Purchasing Solicitations"/>
110+
<local:DataSourceConfigurationInfo BaseUri="http://packages.nuget.org/v1/FeedService.svc" EntitySet="Packages" Description="NuGet Packages"/>
111+
<local:DataSourceConfigurationInfo BaseUri="http://odata.research.microsoft.com/odata.svc" EntitySet="Projects" Description="Microsoft Research - Projects"/>
112+
<local:DataSourceConfigurationInfo BaseUri="http://odata.research.microsoft.com/odata.svc" EntitySet="Publications" Description="Microsoft Research - Publications"/>
100113
</ComboBox>
101114
<CheckBox x:Name="chkNullOutDatasource" IsChecked="True" Style="{StaticResource CheckBoxText}">Null out datasource before setting new</CheckBox>
102115

DataPresenter.DataSources.OData/DataPresenter.DataSources.OData.SampleApp/MainWindow.xaml.cs

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,60 @@ private void cboDataPresenterView_SelectionChanged(object sender, SelectionChang
159159
#region cboOdataSources_SelectionChanged
160160
private void cboOdataSources_SelectionChanged(object sender, SelectionChangedEventArgs e)
161161
{
162-
ODataSourceListItem ods = this.cboOdataSources.SelectedItem as ODataSourceListItem;
163-
if (null != ods)
162+
// Establish the DataSourceConfigurationInfo combobox item that was selected. Refer to the MainWindow.xaml file
163+
// to see how these combobox items were defined.
164+
DataSourceConfigurationInfo dataSourceConfigInfo = this.cboOdataSources.SelectedItem as DataSourceConfigurationInfo;
165+
if (null != dataSourceConfigInfo)
164166
{
165167
// If requested, null out the grid's DataSource before setting the new one.
166168
if (this.chkNullOutDatasource.IsChecked.HasValue && this.chkNullOutDatasource.IsChecked.Value == true)
167169
this.dataPresenter1.DataSource = null;
168170

169-
// Set the grid's DataSource to an instance of an ODataDataSource for the selected Uri and EntitySet..
170-
this.dataPresenter1.DataSource = new ODataDataSource { BaseUri = ods.BaseUri, EntitySet = ods.EntitySet, DesiredPageSize = this.DesiredPageSize, MaximumCachedPages = this.MaximumCachedPages };
171+
// Reset some FieldLayout related fields and settings.
172+
this.dataPresenter1.FieldLayouts.Clear();
173+
this.dataPresenter1.DefaultFieldLayout = null;
174+
this.dataPresenter1.FieldLayoutSettings.AutoGenerateFields = true;
175+
176+
// For demo purposes, define a subset of fields to display in the grid.
177+
//
178+
// For the Northwind Orders table let's limit the number of fields by defining a DefaultFieldLayout
179+
// in the XamDataPresenter that includes a subset of all the fields in the Orders table. Not only will this
180+
// limit the number of fields that are displayed by the grid, but it will also improve response time by
181+
// limiting which columns of data are requested from the backend and transmitted over the connection.
182+
if (dataSourceConfigInfo.BaseUri == @"http://services.odata.org/V4/Northwind/Northwind.svc" && dataSourceConfigInfo.EntitySet == "Orders")
183+
{
184+
this.dataPresenter1.FieldLayoutSettings.AutoGenerateFields = false;
185+
FieldLayout fieldLayout = new FieldLayout();
186+
fieldLayout.Fields.Add(new Field("CustomerID", typeof(string)));
187+
fieldLayout.Fields.Add(new Field("EmployeeID", typeof(int)));
188+
fieldLayout.Fields.Add(new Field("ShipName", typeof(string)));
189+
fieldLayout.Fields.Add(new Field("ShipAddress", typeof(string)));
190+
fieldLayout.Fields.Add(new Field("ShipCity", typeof(string)));
191+
fieldLayout.Fields.Add(new Field("ShipRegion", typeof(string)));
192+
fieldLayout.Fields.Add(new Field("ShipPostalCode", typeof(string)));
193+
194+
this.dataPresenter1.FieldLayouts.Add(fieldLayout);
195+
fieldLayout.IsDefault = true;
196+
}
197+
198+
199+
// ======================================================================================================
200+
// Another way to limit the number of fields that are fetched from the backend to improve response time is to set
201+
// the DesiredFields property on the ODataDataSource. Refer to the MainWindow.xaml file to see how we have limited
202+
// the number of fields requested and returned by the data source by setting the DesiredFields property of the
203+
// DataSourceConfigurationInfo combobox item. We will use this configuration info below when we create the
204+
// ODataDataSource instance.
205+
// ======================================================================================================
206+
207+
208+
// Set the grid's DataSource to an instance of an ODataDataSource created using the settings from the
209+
// selected DataSourceConfigurationInfo.
210+
this.dataPresenter1.DataSource =
211+
new ODataDataSource { BaseUri = dataSourceConfigInfo.BaseUri,
212+
EntitySet = dataSourceConfigInfo.EntitySet,
213+
DesiredFields = dataSourceConfigInfo.DesiredFields,
214+
DesiredPageSize = this.DesiredPageSize,
215+
MaximumCachedPages = this.MaximumCachedPages };
171216
}
172217
}
173218
#endregion //cboOdataSources_SelectionChanged
@@ -236,16 +281,17 @@ private void txtPendingMessage_TextChanged(object sender, TextChangedEventArgs e
236281
#endregion //Event Handlers
237282
}
238283

239-
#region ODataSourceListItem Class
240-
public class ODataSourceListItem
284+
#region DataSourceConfigurationInfo Class
285+
public class DataSourceConfigurationInfo
241286
{
242287
public override string ToString() { return Description; }
243288

244289
public string BaseUri { get; set; }
245290
public string Description { get; set; }
246291
public string EntitySet{ get; set; }
292+
public string[] DesiredFields { get; set; }
247293
}
248-
#endregion //ODataSourceListItem Class
294+
#endregion //DataSourceConfigurationInfo Class
249295

250296
#region ColorToBrushConverter Class
251297
public class ColorToBrushConverter : IValueConverter

DataPresenter.DataSources.OData/DataPresenter.DataSources.OData/ODataDataSource.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ protected override VirtualDataSource CreateUnderlyingDataSource()
9595
}
9696
#endregion //CreateUnderlyingDataSource
9797

98-
#region OnEndDeferRefresh
98+
#region OnEndDeferCollectionViewRefresh
9999
/// <summary>
100-
/// Called when <see cref="IsRefreshDeferred"/> changes to false and modifications to the data are allowed.
100+
/// Called when <see cref="IsRefreshDeferred"/> changes to false and modifications to the data exposed via ICollectionView are allowed.
101101
/// </summary>
102-
protected override void OnEndDeferRefresh()
102+
protected override void OnEndDeferCollectionViewRefresh()
103103
{
104104
}
105-
#endregion //OnEndDeferRefresh
105+
#endregion //OnEndDeferCollectionViewRefresh
106106

107107
#endregion //Base Class Overrides
108108

0 commit comments

Comments
 (0)