Click here to Skip to main content
Click here to Skip to main content

Performance tips for RIA Service with Silverlight 4

, 12 Apr 2011
Rate this:
Please Sign up or sign in to vote.
Performance is a vital part for any application possibly more important for web based app.With wizard based approach of RIA service generally we tends to comprise all entities, exposing to the client and also allowing the DomainService to go for everything from the database. Not only this approach t

Performance is a vital part for any application possibly more important for web based app.With wizard based approach of RIA service generally we tends to comprise all entities, exposing to the client and also allowing the DomainService to go for everything from the database. Not only this approach takes a toll on security, but also it loads the middle tier unreasonably.
So this post is about few tips using which we can improve the performance. Well mostly we will cover the Pagination, Limiting Query results and Output cache of the results.

The Example

As we need a database with records that really exist on real worlds scenario.I am going to stick with Adventure Database example database from Microsoft.As mentioned on my earlier post ,More details regarding the database can be found from here.Also you can download database samples from codeplex  with following links AdventureWorksLT 2005 Version,AdventureWorksLT 2008 Version.For Presentation model scenario i am going to use a part of the Data Model which involves Customer and Address .

And we will use the same sample as mentioned in my Presentation Model article.

RIA Performance Tips

Before beginning with the performance measures allow me to put some light on basic configurations and coding of the project.In this example the DataBinding is achieved  declaratively through DomainDataSource control .The DomainDataSourceControl code is as follows

        <riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my:CustomerPresentationModel, CreateList=true}" Height="0"
                                      LoadedData="customerPresentationModelDomainDataSource_LoadedData"
                                      Name="customerPresentationModelDomainDataSource"
                                      QueryName="GetCustomersWithAddressQuery" Width="0">
            <riaControls:DomainDataSource.DomainContext>
                <my:ADVWORKDomainContext />
            </riaControls:DomainDataSource.DomainContext>
        </riaControls:DomainDataSource>

The next step is bind the the DomainDataSource to the Item Source of DataGrid ,

        <sdk:DataGrid AutoGenerateColumns="False" Height="468"  
           ItemsSource="{Binding ElementName=customerPresentationModelDomainDataSource, Path=Data}" 
           Margin="0,66,0,0" Name="customerPresentationModelDataGrid" 
           RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn x:Name="addressIDColumn" Binding="{Binding Path=AddressID}" Header="Address ID" Width="SizeToHeader" />
                <sdk:DataGridTextColumn x:Name="addressLine1Column" Binding="{Binding Path=AddressLine1}" Header="Address Line 1" Width="SizeToHeader" />
                <sdk:DataGridTextColumn x:Name="addressLine2Column" Binding="{Binding Path=AddressLine2}" Header="Address Line 2" Width="SizeToHeader" />
                <sdk:DataGridTemplateColumn x:Name="addressModifiedDateColumn" Header="Address Modified Date" Width="SizeToHeader">
                    <sdk:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
<sdk:DatePicker SelectedDate="{Binding Path=AddressModifiedDate, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" />
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellEditingTemplate>
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=AddressModifiedDate, StringFormat=\{0:d\}}" />
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                </sdk:DataGridTemplateColumn>
                <sdk:DataGridTextColumn x:Name="cityColumn" Binding="{Binding Path=City}" Header="City" Width="SizeToHeader" />
                <sdk:DataGridTextColumn x:Name="customerIDColumn" 
                    Binding="{Binding Path=CustomerID, Mode=OneWay}" Header="Customer ID" 
                    IsReadOnly="True" Width="SizeToHeader" />
                <sdk:DataGridTemplateColumn x:Name="customerModifiedDateColumn" Header="Customer Modified Date" Width="SizeToHeader">
                    <sdk:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <sdk:DatePicker SelectedDate="{Binding Path=CustomerModifiedDate, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" />
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellEditingTemplate>
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=CustomerModifiedDate, StringFormat=\{0:d\}}" />
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                </sdk:DataGridTemplateColumn>
                <sdk:DataGridTextColumn x:Name="emailAddressColumn" Binding="{Binding Path=EmailAddress}" Header="Email Address" Width="SizeToHeader" />
                <sdk:DataGridTextColumn x:Name="firstNameColumn" Binding="{Binding Path=FirstName}" Header="First Name" Width="SizeToHeader" />
                <sdk:DataGridTextColumn x:Name="lastNameColumn" Binding="{Binding Path=LastName}" Header="Last Name" Width="SizeToHeader" />
                <sdk:DataGridTextColumn x:Name="phoneColumn" Binding="{Binding Path=Phone}" Header="Phone" Width="SizeToHeader" />
                <sdk:DataGridTextColumn x:Name="postalCodeColumn" Binding="{Binding Path=PostalCode}" Header="Postal Code" Width="SizeToHeader" />
                <sdk:DataGridTextColumn x:Name="stateProvinceColumn" Binding="{Binding Path=StateProvince}" Header="State Province" Width="SizeToHeader" />
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>

Performance Snapshot – Before

Using Visual Studio Profiler Wizard with CPU sampling ,the snapshot shows the elapsed time for calling the query and fetching the detail from the database.The straight forward approach to RIA service with Domain DataSource control it retrieves all 800 records at one shot.

RIA Performance Tips

Well a detailed tutorial about performance wizard in Visual Studio 2010 can be found here.

Performance Tips and Tricks

As the number of records returned by the query always have a performance impact over web so the following tips and trick in combination proper coding standard always makes a difference lets discuss few of them

Pagination with LoadSize and PageSize

The first step of improvements lies with applying paging to the data .Well paging works with out DomainDataSource control but it works better with DomainDataSource control.So lets add a DataPager control to the page and bind the source to Domaindatasource control

        <sdk:DataPager Height="26" Margin="0,0,0,44" Name="dpCustomers" PageSize="10" VerticalAlignment="Bottom"
                       DisplayMode="FirstLastPreviousNextNumeric" AutoEllipsis="False"
                       Source="{Binding Path=ItemsSource,ElementName=customerPresentationModelDataGrid}"/>

Although the paging option will start working but still we are loading everything from the database so we need to some tweaks to the DomainDataSource control

RIA Performance Tips

As mentioned in above image ,LoadSize load exactly specified amount of records with each hit to the Tier interaction.For next 30 records it hits the database again.

RIA Performance Tips

so here with above snap shot from Tier Interaction you can find out for each record after 30th the DomainDataSource control is sending fresh request to fetch next set.

While working with LoadSize and page Size make sure that the PageSize of Datapager and DomainDataSource control are same , here in this example its defined as 10.Also make sure the page size is less than the LoadSize.

Limit Results of Query Operation

Instead of fetching all the detail , try using QueryAttribute to limit the results from the query operation .Domain Service at server side supports attribute to limit the result set.For example here in this example each call to domain service returns maximum 30 results.

RIA Performance Tips

Well make sure that your loadsize and pagesize as mentioned in above tips for Datapager must be with in this limit .Well it worth mentioning that one of my reader was facing issues ,the famous LoadOperation failed and on digging a bit we found real problem was timeOut .once we set the ResultLimit , the project was on run.

Cashing of Output

Well this option really does not meant for real time data where changes are very frequent but you can always cache the output of a method call using attributes.

RIA Performance Tips

As shown above OutoutCacheLocation specifies the location where the resource need to be cached.

Performance Snapshot – After

Well we talked much and now its time for result , as mentioned above the TierInteraction gives you a complete picture of time taken by each method call .

RIA Performance Tips

Comparison of Performance

The last call where you can compare both performance results let you show a complete picture over the improvement.

RIA Performance Tips

The Delta here shows a sharp increment in performance of Tier Interaction  for GetCustomerWithAddress query method.

Conclusion

May be there are lots of scope available for performance improvement such as exposing entities that need to be exposed to client by using Presentation Model and some few tricks over native WCF service but for the time being hope above will make a difference .Keep me suggesting your ideas and comments.

Source Code and Performance Reports

Download Link – : RIAPerformanceTips.zip

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Manas_Patnaik
Software Developer (Senior) Infosys
India India
Nothing special .. I like challenges and love my critics.
 
Associate Consultant | Microsoft | Bangalore | India

Blog : http://manaspatnaik.com/blog

Twitter@manas_patnaik
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140718.1 | Last Updated 12 Apr 2011
Article Copyright 2011 by Manas_Patnaik
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid