Nov 082009
 

Microsoft Silverlight 3 includes support for creating Rich Internet Applications with virtually no code at all. In this article, I will create a simple RIA application which displays data from the SQL Server 2005 AdventureWorks database. For those who are wondering what RIA services are? Its a pattern which integrates ASP.NET with Silverlight and provides a simple way to write n-tier applications. What I admired about RIA services applications are it brings your database logic to your business layer. What that means is that your database and Data access layer virtually contains no application specific code at all. In future, upgrading, migrating to any other database is amazingly simple. There is the element of a performance implication while using Linq though, which needs to be taken into consideration. Thanks to Sachin Rathi from Microsoft for his session in TechDays where he taught about this new topic.

So lets jump into it. First the tools, which can be downloaded from here. The fifth point in the list is the RIA kit. Once thats done, you would have a new template – Silverlight Business Application.

newProject

So like we saw in the previous blog, there are two applications created – one for our Silverlight application and one for the ASP.NET application. The basic application is already created with a decent theme.

businessApp

Lets create a new xaml page for displaying our data created by the RIA applications. Right click on the Views folder and add a new Silverlight page.

addnewPage

addnewItem

Now that the new Silverlight page is created, lets add a link to it from the main page. Add this markup after the link to the about page.

<Rectangle x:Name=”Divider2″ Style=”{StaticResource DividerStyle}”/>
<HyperlinkButton x:Name=”Link3″ Style=”{StaticResource LinkStyle}”
NavigateUri=”/EmployeesPage” TargetName=”ContentFrame” Content=”Employees”/>
Once thats done, we need to add any database from which to retrieve data from. I will use the sample AdventureWorks database. Copy the database file to the App_Data folder. Now what is needed is a Entity Data model. The Entity data model generates a ER data model by analyzing the database. Right click on the web project and add a new item – AdventureWorks.edmx. In the next screen click on Generate from database. The ED model automatically detects the database files and generates the connection string. Click next and select the employee table alone in the next screen.
edmxModel
edmodel
This step generates the entity relationship model for the database, and make the code “aware” of the underlying database. In later steps we could write Linq queries against the database. The next step would be to add a new domain service class, the item template for which has been installed already.
domainService
domainServiceOpts
To the domain service class that was automatically generated, add a new method GetSalariedEmployees.
public IQueryable<Employee> GetEmployees()
{
return this.Context.Employee;
}
public IQueryable<Employee> GetSalariedEmployees()
{
return this.Context.Employee
.Where(emp => emp.SalariedFlag == true);
}
Then add a new datagrid to the EmployeesPage.xaml. A reference needs to be added to the Windows.Controls.Data assembly. Also a namespace is added to the datagrid. The markup is as below.
employeeMarkup
Now that the datagrid is loaded, lets add the namespace for the Application in the xaml page. Here is the finished xaml file.
finishedXaml
It can also be done through code like this.
employees _eObj = new employees();
public EmployeesPage()
{
InitializeComponent();
this.dataGrid.ItemsSource = _eObj.Employees;
_eObj.Load(_eObj.GetEmployeeQuery());
}

This application is by no means over, we can add sorting, filtering, updating rows to this with as little code as possible. More about it in future posts.