Building a simple line of business Application using RIA Services
Last day we had enough of theory on RIA Services. Today let us get our hand dirty and launch visual studio. We will keep it simple and easy in flow since it is our first exercise.
Essentially we will fetch data from Person table and bind it to grid view on Silverlight page. Flow diagram of the application we are going to make would be as below diagram.
Building Silverlight Application
Start with creating a Silverlight application.
Next you will get prompted to choose
- Web site to host Silverlight application
- Select Silverlight target version
And if you have WCF RIA services installed then you will get a check box to instruct Silverlight whether you want to enable WCF RIA Service for this particular Silverlight application or not ? You want to enable WCF RIA service so check the checkbox Enable WCF RIA Services.
We have instructed Silverlight application while creating that we want RIA Service enabled and next we need to create Data Model to be exposed and share data through this model in between presentation layer and middle layer.
Building Data Model
To create data model right click on web project [project with extension .web] and choose option add new item from context menu.
Next from Data tab you need to choose ADO.Net Entity Model template. We are going to use ADO.Net Entity Model ORM to create data model. ADO.Net Entity Model creates model with extension edmx. Change name of created edmx file to SchoolModel.edmx.
After clicking on add button, you will be prompted to choose the source of data model. Either you can create empty data model or choose a database as a source to create data model. We are going to choose School database residing on my local server as a source of data model. So select Generate from database option and click on next button
Now you will be prompted to choose database object. If school database is not listed in drop down then you can create a New Connection else you can choose from the dropdown. Let us proceed with creating a new connection
On clicking of New Connection button, you will be prompted with dialog box to specify database connection properties. Specify different values as of below image and click on Test Connection. After getting successful message click on Ok button.
In Data connection dialog box if you want you can change the name of connection setting in web.confg. However I am leaving the default name.
Click on next button to get last dialog box to conclude data model creation. Here you need to choose database objects you want to put as mart of data model. You can have tables, views, and stored procedures from database as part of data model. I am selecting all tables and stored procedures as part of data model. If you want you can change namespace name but for sake of simplicity at this point leave everything else as default.
On click on Finish button you will get data model created and you will get data model file open in designer surface. Here you can notice mapped between the entities.
In solution explorer you will find SchoolModel.edmx file has been added under the web project.
Building Domain Service
To create data model right click on web project [project with extension .web] and choose option add new item from context menu.
Next from Web tab you need to choose Domain Service class template. Change name to SchoolRIAService.cs and click on Add button.
In next dialog box go ahead and
- Select DataContext or ObjectContext class from the dropdown. You will get SchoolEntities context class in drop down value to choose.
- You can edit Domain Service class name. For simplicity leave it as it is.
- You have check box to specify Enable Client Access. Check this check box.
- You have check box to specify whether you want to expose ODATA Endpoint. For this time uncheck it.
- You will get all the entities from the context class listed. You can choose from them to be exposed as RIA Service. I am selecting all the entities and enabling all entities for edit.
- Check the box generates associated class for metadata.
Click on OK button and give a second to examine web project in solution explorer. You will find three classes added there. We will examine and talk more on these classes in coming days. Today we are just going to see there uses at the presentation layer in Silverlight project.
Using Domain Service in Presentation layer or Silverlight client
You have,
- Created data model
- Create domain service
Now build the web project and add below namespace on MainPage.xaml.cs file.
After adding namespaces open MainPage.xaml and drag and drop a data grid on that. Give name of the data grid as grdRIADataGrid. Xaml would be as below,
MainPage.Xaml
<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="DemoDay2.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White"> <sdk:DataGrid x:Name="grdRIADataGrid" /> </Grid> </UserControl>
At this point designing of page has been completed. You need to bind data from person table to data grid. For that
1. Create instance of domain service class
2. Load all people to the context of client side proxy
3. Fetch the entities from loaded list of entities and bind as item source of data grid
Code behind would look like as below,
MainPage.xaml.cs
using System.Windows.Controls; using DemoDay2.Web; using System.ServiceModel.DomainServices.Client; namespace DemoDay2 { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); SchoolRIAContext proxy = new SchoolRIAContext(); LoadOperation<Person> loadPerson = proxy.Load(proxy.GetPeopleQuery()); grdRIADataGrid.ItemsSource = loadPerson.Entities ; } } }
Now you run Silverlight project and in data grid you will get the entire person from school database using RIA Service.
Today we followed a simple path and fetched data from database and bind to data grid on at Silverlight client using RIA Service. We have not gone deeper into different aspect of context class. We will discuss then in further days. I will conclude day two with assumption that by now you know,
- Why WCF RIA?
- Creating simple line of business application using RIA Services
See you on day 3
Follow @debug_mode
Leave a comment