%META:TOPICPARENT{name="VirtAdoNet35Provider"}%
---+ Simple .NET RIA Services Application using Virtuoso as the Data Server
%TOC%
---++ Introduction
.NET RIA Services is a new framework from Microsoft that simplifies the development of N-Tier web applications. A typical application will consist of a presentation layer, application logic and a data access layer. In the examples that follow, we will be using a combination of Silverlight 3.0 and .NET RIA Services for the presentation and application layers, while using Virtuoso as the data server via its native ADO.NET provider. The first example demonstrates how to display data from the Employee table in the Demo database using web page hosted grid control. The second example shows the navigational power behind hyperlinked data (de-referenceable HTTP scheme IRIs) exposed by Linked Data Views built stop Virtuoso's demonstration relational database schema (which is very similar to SQL Server's Northwind Database).
---++ Prerequisites
1. The example assumes that you have a local Virtuoso server with the Northwind demo database installed. If the demo database is not already installed then download the [[http://virtuoso.openlinksw.com/download/][demo database VAD package]] (demo_dav.vad) and install it. The VAD package will create a new database in Virtuoso called demo containing the familiar Northwind tables. It will also creates [[http://docs.openlinksw.com/virtuoso/rdfsparqlintegrationmiddleware.html#rdfviews][Linked Data Views]] of the Northwind tables. In the example we assume the database is accessible on a hostname of "demo.openlinksw.com" on the default port 80, where an actually live instance of the Virtuoso Demo database is hosted. Users would use the appropriate hostname and port number of their Virtuoso installation to create the sample application, and would be would be localhost:8890 for a default installation or whatever the [[http://docs.openlinksw.com/virtuoso/databaseadmsrv.html#ini_URIQA][URIQA DefaultHost]] Virtuoso configuration parameter is set to when the demo database VAD package is installed.
1. The Virtuoso ADO.Net provider for .Net 3.5 and the Entity Framework
1. Microsoft Visual Studio 2008
1. The Virtuoso [[http://virtuoso.openlinksw.com/download/][Cartridges]] VAD package
1. [[http://www.microsoft.com/downloads/details.aspx?familyid=9442b0f2-7465-417a-88f3-5e7b5409e9dd&displaylang=en][Silverlight 3 Tools for Visual Studio 2008 SP1]]
1 [[http://www.microsoft.com/downloads/details.aspx?FamilyID=76bb3a07-3846-4564-b0c3-27972bcaabce&displaylang=en][Microsoft .NET RIA Services July 2009 Preview]]
---++ Creating the Application
---+++ Step 1 - Create the Visual Studio Projects
1. Open Visual Studio and create a new Silverlight Application project. Call the project DemoApplication.
1. In the New Silverlight Application dialog ensure that Enable .NET RIA Services is checked. Click the OK button.
%BR%%BR%%BR%%BR%
1. At this point a skeleton solution is created that consists of a client project called DemoApplication and a server project called DemoApplication.Web. This application will use data from the Virtuoso database. We add the data and its schema to the application by adding an ADO.NET entity data model to the server project.
---+++Step 2 - Add the Data Model
1. Right click the server project in the Solution Explorer and Add New Item. In the dialog box select ADO.NET Entity Data Model and call it demo.edmx. Click the Add button. This will open the Entity Data Model Wizard.
1. Choose Generate From Database and click Next.
1. Set up a connection to the Demo database on your local Virtuoso Server, select Yes, include the sensitive data in the connection string and set the name of the entities to DemoEntities. Click Next.
1. On the Choose Your Database Objects page expand Tables and select Employees. Check that the Model Namespace is DemoModel and click Finish.
%BR%%BR% %BR%%BR%
1. We want to make the entities in the model available to both the client and server parts of the solution. To do this we need to add a DomainService to the solution. However, to make the entities from the data model available to the domain service we must first build the solution.
---+++Step 3 - Add a Domain Service
1. First build the solution.
1. Right click the server project in the Solution Explorer and Add New Item. In the dialog box choose Domain Service Class from the Templates pane and call it EmployeeService.cs. Click Add. This will open the Add New Domain Service Class dialog.
1. The entities from the model we have just added to the project are listed under Entities. Tick the box next to Employees. and click OK.
%BR%%BR%%BR%%BR%
1. This will create the DomainService class and generated code in both the client and server parts of the application. The Silverlight client can now interact with the data through the DomainContext class in the client project. At this point you need to build the solution again.
---+++Step 4 - Display The Data
1. From the Silverlight XAML Controls in the Toolbox drag a DataGrid between the <Grid> </Grid> tags on MainPage.xaml in the client. Call the grid EmployeeGrid.
1. Instantiate the DomainContext to get the list of employees and add them to the grid by adding code to MainPage.xaml.cs so it looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DemoApplication.Web;
using System.Windows.Ria.Data;
namespace DemoApplication
{
public partial class MainPage : UserControl
{
private EmployeeContext _employeeContext = new EmployeeContext();
public MainPage()
{
InitializeComponent();
LoadOperation LoadOp =
this._employeeContext.Load(this._employeeContext.GetEmployeesQuery());
this.EmployeeGrid.ItemsSource = LoadOp.Entities;
}
}
}
1. Build and run the application. Internet Explorer will be launched and you will see the data displayed on the page as a grid.
%BR%%BR%%BR%%BR%
---++Displaying RDF Data
One advantage of using Virtuoso as the data server is the seamless way in which we can use this use this application to expose dereferenceable HTTP IRIs which make the record level relationships explorable across many dimensions.
Virtuoso's hybrid SPASQL (SPARQL inside SQL) query language enables RDF model data -- hosted within the Virtuoso Quad store -- to be queried using SPARQL from any of its SQL interfaces (ODBC, JDBC, ADO.NET, OLE-DB, XMLA); all you do is prefix your queries with the keyword SPARQL. In the example that follows we will use SPASQL to create a SQL view that exposes IRIs for records in the Employee table.
---+++Step 1 - Create the View in Virtuoso
1. Open the Virtuoso Conductor.
1. In iSQL, execute the following statement. Remember to use the appropriate hostname and port number of your Virtuoso installation, typically localhost:8890 for a default installation or whatever the [[http://docs.openlinksw.com/virtuoso/databaseadmsrv.html#ini_URIQA][URIQA DefaultHost]] Virtuoso configuration parameter was set to when the demo database VAD package was installed.
CREATE VIEW Demo.demo.sparqlview as
SPARQL
PREFIX nwind:
SELECT DISTINCT ?s
FROM
WHERE {?s a nwind:Employee}
---+++Step 2 - Modify the Solution To use the View
1. Delete the existing Employee model and add a new one that comprises this new view.
%BR%%BR%%BR%%BR%
1. Delete the DomainService. Build the solution and add a new DomainService called EmployeeService. Select the sparqlview entity. Build the solution.
%BR%%BR%%BR%%BR%
1. Modify the code in mainpage.xaml.cs so it uses the sparqview entity. It should look like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DemoApplication.Web;
using System.Windows.Ria.Data;
namespace DemoApplication
{
public partial class MainPage : UserControl
{
private EmployeeContext _employeeContext = new EmployeeContext();
public MainPage()
{
InitializeComponent();
LoadOperation LoadOp =
this._employeeContext.Load(this._employeeContext.GetSparqlviewQuery());
this.EmployeeGrid.ItemsSource = LoadOp.Entities;
}
}
}
1. Build and run the application. You will see a list of IRIs that identify the Northwind employees.
%BR%%BR%%BR%%BR%
1. To realize the power of linked data we would now liked to begin exploring this data by clicking on these IRIs.
---+++ Step 3 - Make Hyperlinks From IRIs
1. Modify the DataGrid in MainPage.xaml to bind the IRI in each cell of the grid to a Hyperlink button.
1. We set the AutoGenerateColumns property of the DataGrid to False then add our own Template for the column. MainPage.xaml should look like this:
1. If we build and run the project now each IRI in the list appears as a hyperlink.
%BR%%BR%%BR%%BR%
1. Clicking on each of the links exposes new Employee relations where each Attribute and Value (optionally) pair also exist in link form. Each of these links creates a powerful linked data graph exposing data level relationships, all devoid of traditional join overhead associated with relational queries.
%BR%%BR%%BR%%BR%
%BR%%BR%%BR%%BR%
---++ Next Steps
The examples in this document show you how to simply display data in a browser using Silverlight 3 and .NET RIA Services. See an [[RIAServicesHowTo2][example]] of a more complicated application.