Database operations using Silverlight 2.0 WCF and LINQ to SQL
This is my fifth article in a series focusing on Silverlight 2.0 development using Visual Studio 2008. Technically, we will have Silverlight 2.0 controls accessing a regular WCF Service and doing CRUD operations against a database using the “LINQ to SQL” model.
This article assumes that you have a minimum knowledge of technologies like “LINQ to SQL,” “WCF,” “Silverlight” and so forth. If you don’t, and if you need step-by-step walk-throughs for the above technologies, please go to the following links.
If you are new to Silverlight 2.0 development, please refer to the following articles that I have written before reading the rest of this one (available at http://www.aspfree.com/cp/bio/Jagadish-Chatarji/):
Beginning Silverlight 2.0 Development using Visual Studio 2008
Developing a Silverlight 2.0 Application that Consumes a WCF Service Using Visual Studio 2008
Silverlight 2.0 Application Development with “LINQ to SQL” and WCF Service (HIGHLY RECOMMENDED)
Introducing Two-Way Data Binding using Silverlight 2.0, WCF and “LINQ to SQL”
This article is based on the following configuration:
The entire source code for this article is available in the form of a free downloadable zip file. The solution was developed using Microsoft Visual Studio 2008 Team Edition (with SP1) with Microsoft SQL Server 2008 Developer Edition on Microsoft Windows Server 2003 Standard Edition (with SP2) with Silverlight 2.0 (RTM). I didn’t really test it in any other environment. I request that you post in the discussion area if you have any problems in execution.
Now we have to develop a Silverlight 2.0 page which can consume our previously developed WCF Service. The page would look like the following:
The following are the steps necessary to develop the above Silverlight 2.0 page:
Add a new “Silverlight” project (“DemoSL”) to the solution. This in turn will add a “DemoSL.Web” project (Silverlight hoster application) to the solution. Detailed steps are provided in previous articles.
Add a new “Silverlight user control” (“Page4.xaml”) to the “DemoSL” project and modify “Application_Startup” in “App.xaml” as follows:
Private Sub Application_Startup(ByVal o As Object, ByVal e As StartupEventArgs) Handles Me.Startup
This is a continuation from the previous section. In the previous section, we developed the Siverlight 2.0 UI for CRUD operations against a WCF service. Now, we have to develop the code behind to access the WCF Service. Make sure that you have added a reference to the WCF service in your Silverlight 2.0 project.
The following is the code behind for the UI markup provided in the previous section. The explanation is provided in the next section.
Private Sub AddEmployeeCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
ShowMessage("Added Successfully!")
End Sub
Private Sub UpdateEmployeeCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
ShowMessage("Updated Successfully!")
End Sub
Private Sub DeleteEmployeeCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
ShowMessage("Deleted Successfully!")
btnClear_Click(Nothing, Nothing)
End Sub
Private Sub EmployeeFetched(ByVal sender As Object, ByVal e As EmpService.GetEmployeeCompletedEventArgs)
If e.Result Is Nothing Then
ShowMessage("Employee not found!")
Else
objEmp = e.Result
DoBinding()
ShowMessage("Fetched!")
End If
End Sub
#End Region
#Region "Button Events"
Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClear.Click
objEmp = New EmpService.Emp
DoBinding()
End Sub
Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAdd.Click
ShowMessage("Communicating...")
objService.AddAsync(objEmp)
End Sub
Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnDelete.Click
ShowMessage("Communicating...")
objService.DeleteAsync(objEmp.Empno)
End Sub
Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnUpdate.Click
ShowMessage("Communicating...")
objService.UpdateAsync(objEmp)
End Sub
Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSearch.Click
ShowMessage("Communicating...")
objService.GetEmployeeAsync(objEmp.Empno)
End Sub
#End Region
End Class
Press F5 and you should be able to do all CRUD operations against the “Emp” table in database using Silverlight 2.0 page together with WCF and LINQ to SQL.
If you run into any problems, please refer tp my dedicated sections (especially on troubleshooting) in my previous articles. Please go through all of the troubleshooting steps in my previous articles; do so, and you should have no problems.
This section explains the code given in the previous section.
The constructor in UI class is as follows:
Public Sub New()
InitializeComponent()
InitiateContext()
InitiateHandlers()
End Sub
“InitiateContext” and “InitiateHandlers” are custom methods developed especially to ease our process. The “InitiateContext” method instantiates a WCF Service client object (objService) along with a business object (objEmp). Once the instantiatiations are completed, the business object (objEmp) is assigned to the “DataContext” property of the “LayourRoot” container control to enable “Two-Way” data binding (explained in detail in my previous article).
Every WCF method from Silverlight UI is accessed asynchronously. Once the method at the WCF Service gets processed, the call back mechanism gets back to the method in Silverlight UI and then executes it. For call back mechanisms to work, we should register the handlers. This is handled in “InitiateHandlers.”
Every operation (a WCF method, for example) gets executed at the WCF Service in an asynchronous fashion. The following are the statements which access the WCF methods asynchronously:
objService.AddAsync(objEmp)
objService.DeleteAsync(objEmp.Empno)
objService.UpdateAsync(objEmp)
objService.GetEmployeeAsync(objEmp.Empno)
You can also observe that the same business object (objEmp) is passed while calling the respective WCF method.
In my upcoming articles, we will see more and more examples of Silverlight 2.0 development together with LINQ to SQL and WCF. I hope you enjoyed the article and any suggestions, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com