Monday, April 18, 2011

WCF RIA Services with Silverlight 4

Developing n-tier application with Silverlight in the earlier version was too tedious and error prone. You have to create a WCF Service, tweak web.conf, expose the functionality as WebMethods and web reference to WCF Service from Silverlight project, after adding a new functi0nality in WCF update the service reference in Silverlight project. The list goes on and on.

WCF RIA Service makes it super easy developing n-tier business applications with Silverlight. WCF RIA Services also comes with built in Validation, Authentication and Authorization service.

I have installed Silverlight 4. In the New Project dialog you will see lot more options.  I am starting a Silverlight Application, Silverlight  Business Application I will take up in another blog and click OK.

Following is the script of the AirLines database which I will be using as example.

In the next dialog select Enable WCF RIA Service.

A Silverlight Solution comprises of two project one an Asp .Net Project and another the client Silverlight project.
I am creating an Ado .Net Entity Data Model for connecting to AirLines database in the Web Project.

Run the project. Add a Domain Service Class to the Web Project.

In the Add New Domain Service Class dialog you can see all the tables selected in the Ado .Net Entity Data Model. Check Enable Editing so that it will generate CUD (Create Update Delete) code.

Again run the project so that the Domain Service class will automatically generate the proxy which you can use from Silverlight.
Now I am ready consuming the Domain Service from Silverlight.

My intention is the show the contents of Locations table inside an DataGrid. I am also filling a ComboBox with LocationNames.
using SilverlightRiaShalvin.Web;

DomainService1 context = new DomainService1();
private void UserControl_Loaded(object sender, RoutedEventArgs e)
   dataGrid1.ItemsSource = context.Locations;
   comboBox1.ItemsSource = context.Locations;
   comboBox1.DisplayMemberPath = "LocationName";
   comboBox1.SelectedValuePath = "Locationid";

Here I am extracting the LocationId of the selected Location.
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)

Here the code for inserting a value to Location Table.
private void button1_Click(object sender, RoutedEventArgs e)
  var Loc = new Location{LocationName = "Trivandrum"};
  MessageBox.Show("Record saved");

 Silverlight Business Application Air Lines Project

Now let's take up the AirLines project.
CREATE TABLE dbo.Locations(
    LocationId int IDENTITY(1,1) PRIMARY KEY NOT NULL,
    LocationName varchar(20),
    Latitude numeric(6, 2),
    Longitude numeric(6, 2),
    Description varchar(50))

CREATE TABLE dbo.Flight(
    FlightId int IDENTITY(1,1) NOT NULL,
    FlightNo varchar(10),
    FromLocationId int,
    ToLocationId int,
    ArrivalTime varchar(10),
    DepartureTime varchar(10),
    NoofBusSeats int,
    NoofEcoSeats int)

CREATE TABLE dbo.FlightTrans(
    TransId int IDENTITY(1,1) NOT NULL,
    TransDate datetime ,
    UserName varchar(20) ,
    FlightId int,
    EcoSeats int,
    BusSeats int)

CREATE TABLE dbo.FlightStatus(
    FlightDetailId int IDENTITY(1,1) NOT NULL,
    Flightid int,
    TotalSeats int,
    ExecutiveSeats int,
    BuisnessSeats int,
    FlightDate datetime,
    BookedExecutiveSeats int,
    BookedBusinessSeats int) 

CREATE TABLE dbo.FlightRate(
    FlightRateId int IDENTITY(1,1) NOT NULL,
    FlightId int,
    BusStdRate numeric(8, 2),
    EcoStdRate numeric(8, 2))

Along the way I will also demonstrate a few Visual Studio 2010 features related to Silverlight and WPF.
I am starting a new Business Applicaiton Project. WCF Ria Services will be automatically added when you select Business Application Project.

 I will get a project similar to new Asp .Net Web Site Project in Visual Studion 2010. It has a good navigation structure, nice look and feel and a few HyperlinkMenus to start with.
Let's start by changing the title of the project. It has an inbuilt style set through Silverlight Binding. Do refer my blog Silverlight 4/Silverlight for Windows Phones/WPF 4 DataBinding with Visual Studio 2010

if you want to want to learn more about about Silverlight batabinding.
To switch off the Binding of the TextBlock that holds the Application name go to properties window click on the icon next to the Text property in the dialog select Reset settings. Then type your heading.

Like wise I am changing the Content property of Hyperlink buttons to Location and Flights respectively.

 I am using Silverlight Child Window in this project. I am adding a Silverlight Child Window to the project selecting Silverlight Project from the Solution Explorer. As you know two projects will be created when you start new Silverlight project, Silverlight Project and an Asp .Net project for hosting the Silverlight Project. Earlier in the blow we saw adding Entity Data Model and Domain Service Class to the Asp .Net Project.

Insert Location
Location table holds the information about a Location.  This table is referenced by Flights Table.
Here is the Xaml of the frmLocations Silverlight Child Window.

<Grid x:Name="LayoutRoot" Margin="2">
        <RowDefinition />
        <RowDefinition Height="Auto" />
    <sdk:Label Height="28" HorizontalAlignment="Left" Margin="21,34,0,0" Name="label1" VerticalAlignment="Top" Width="120" Content="Location Name" />
    <sdk:Label Height="28" HorizontalAlignment="Left" Margin="21,70,0,0" Name="label2" VerticalAlignment="Top" Width="120" Content="Latitude" />
    <sdk:Label Height="28" HorizontalAlignment="Left" Margin="21,106,0,0" Name="label3" VerticalAlignment="Top" Width="120" Content="Longitude" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="118,26,0,0" Name="txtLocation" VerticalAlignment="Top" Width="120" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="118,62,0,0" Name="txtLatitude" VerticalAlignment="Top" Width="120" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="118,98,0,0" Name="txtLongitude" VerticalAlignment="Top" Width="120" />
    <Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="118,161,0,0" Name="btnSave" VerticalAlignment="Top" Width="75" Click="btnSave_Click" />

Add Entity Data Model and Domain Data Service class as demonstrated in the first part of the blog.

using ShalvinAirlines.Web;

 AirLines context = new AirLines();

private void btnSave_Click(object sender, RoutedEventArgs e)
    decimal intLatitude = decimal.Parse(txtLatitude.Text);
    decimal intLongitude = decimal.Parse(txtLongitude.Text);
    var Loc = new Location { LocationName = txtLocation.Text, Latitude = intLatitude, Longitude = intLongitude };
    MessageBox.Show("Location saved");
Numeric data type in Sql Server will be mapped to decimal data type in .Net.

Insert Flight

<Grid x:Name="LayoutRoot" Margin="2">
        <RowDefinition />
        <RowDefinition Height="Auto" />
    <sdk:Label Height="28" HorizontalAlignment="Left" Margin="72,61,0,0" Name="label1" VerticalAlignment="Top" Width="120" Content="From Location" />
    <ComboBox Height="23" HorizontalAlignment="Left" Margin="181,54,0,0" Name="cboFrom" VerticalAlignment="Top" Width="120" />
    <sdk:Label Content="To Location" Height="28" HorizontalAlignment="Left" Margin="72,103,0,0" Name="label2" VerticalAlignment="Top" Width="120" />
    <ComboBox Height="23" HorizontalAlignment="Left" Margin="181,97,0,0" Name="cboTo" VerticalAlignment="Top" Width="120" />
    <sdk:Label Height="28" HorizontalAlignment="Left" Margin="72,139,0,0" Name="label3" VerticalAlignment="Top" Width="120" Content="Arrival Time" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="181,131,0,0" Name="txtArrivalTime" VerticalAlignment="Top" Width="120" />
    <sdk:Label Height="28" HorizontalAlignment="Left" Margin="72,175,0,0" Name="label4" VerticalAlignment="Top" Width="120" Content="Departure Time" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="181,167,0,0" Name="txtDepartureTime" VerticalAlignment="Top" Width="120" />
    <sdk:Label Height="28" HorizontalAlignment="Left" Margin="72,211,0,0" Name="label5" VerticalAlignment="Top" Width="120" Content="No. Economy Seats" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="181,203,0,0" Name="txtNoEconomySeats" VerticalAlignment="Top" Width="120" />
    <sdk:Label Content="No. Business Seats" Height="28" HorizontalAlignment="Left" Margin="72,241,0,0" Name="label6" VerticalAlignment="Top" Width="120" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="181,233,0,0" Name="txtNoBusinessSeats" VerticalAlignment="Top" Width="120" />
    <Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="181,277,0,0" Name="btnSave" VerticalAlignment="Top" Width="75" Click="btnSave_Click" />
    <sdk:Label Height="28" HorizontalAlignment="Left" Margin="72,26,0,0" Name="label7" VerticalAlignment="Top" Width="120" Content="Flight No." />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="181,18,0,0" Name="txtFlightNo" VerticalAlignment="Top" Width="120" />

using ShalvinAirlines.Web;

AirLines context = new AirLines();
private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
    cboFrom.ItemsSource = context.Locations;
    cboFrom.DisplayMemberPath = "LocationName";
    cboFrom.SelectedValuePath = "LocationId";

    cboTo.ItemsSource = context.Locations;
    cboTo.DisplayMemberPath = "LocationName";
    cboTo.SelectedValuePath = "LocationId";

private void btnSave_Click(object sender, RoutedEventArgs e)
    int intFromId = Int32.Parse(cboFrom.SelectedValue.ToString());
    int intToId = Int32.Parse(cboTo.SelectedValue.ToString());
    int intNoBusSeats = Int32.Parse(txtNoBusinessSeats.Text);
    int intNoEcoSeats = Int32.Parse(txtNoEconomySeats.Text);
    var Fl = new Flight { FlightNo = txtFlightNo.Text, 
        FromLocationId = intFromId, 
        ToLocationId = intToId, 
        ArrivalTime =  txtArrivalTime.Text,
    DepartureTime = txtDepartureTime.Text,
    NoofEcoSeats= intNoEcoSeats };

    MessageBox.Show("Flight inserted");
First I filled the Combo Boxes with the LocationName and Set the SelectedValuePath to LocationId so that I an extract the LocationId of the Selected Item.

Booking Module

xmlns:sdk="" Loaded="ChildWindow_Loaded">
<Grid x:Name="LayoutRoot" Margin="2">
        <RowDefinition />
        <RowDefinition Height="Auto" />
    <sdk:Label Content="Flight" Height="28" HorizontalAlignment="Left" Margin="10,10,0,0" Name="label1" VerticalAlignment="Top" Width="120" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="103,81,0,0" Name="txtEconomySeats" VerticalAlignment="Top" Width="120" />
    <ComboBox Height="23" HorizontalAlignment="Left" Margin="103,10,0,0" Name="cboFlight" VerticalAlignment="Top" Width="120" />
    <sdk:Label Content="Date" Height="28" HorizontalAlignment="Left" Margin="10,45,0,0" Name="label2" VerticalAlignment="Top" Width="120" />
    <sdk:DatePicker Height="23" HorizontalAlignment="Left" Margin="103,45,0,0" Name="dtpDate" VerticalAlignment="Top" Width="120" />
    <sdk:Label Content="Economy Seats" Height="28" HorizontalAlignment="Left" Margin="10,82,0,0" Name="label3" VerticalAlignment="Top" Width="120" />
    <sdk:Label Content="Business Seats" Height="28" HorizontalAlignment="Left" Margin="10,131,0,0" Name="label4" VerticalAlignment="Top" Width="120" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="103,123,0,0" Name="txtBusSeats" VerticalAlignment="Top" Width="120" />
    <Button Content="Book" Height="23" HorizontalAlignment="Left" Margin="103,188,0,0" Name="btnBook" VerticalAlignment="Top" Width="75" Click="btnBook_Click" />

AirLines context = new AirLines();
private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
    cboFlight.ItemsSource = context.Flights;
    cboFlight.DisplayMemberPath = "FlightNo";
    cboFlight.SelectedValuePath = "FlightId";


private void btnBook_Click(object sender, RoutedEventArgs e)
    DateTime dt = DateTime.Parse(dtpDate.SelectedDate.ToString());
    int intEcoSeats = Int32.Parse(txtEconomySeats.Text);
    int intBusSeats = Int32.Parse(txtBusSeats.Text);
    int intFlightId = Int32.Parse(cboFlight.SelectedValue.ToString());

    var Book = new FlightTran { TransDate = dt, UserName = GlobalClass.UserName, 
        FlightId = intFlightId, EcoSeats = intEcoSeats, BusSeats = intBusSeats };
    MessageBox.Show("Booking completed");

No comments:

Post a Comment