Introducing Two-Way Data Binding using Silverlight 2.0, WCF and LINQ to SQL
This is my fourth 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 do an INSERT operation against the 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 you need step-by-step instructions for the above technologies, please go through the following links.
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)
For this article, I upgraded my development machine from "Silverlight 2.0 tools RC 1" to "Silverlight 2.0 tools RTM." My machine now is equipped with the following environment:
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.
Add a new WCF Service project named "DemoEmpService."
Change "Service1" to "EmpService" everywhere in the project (steps are shown in my previous articles).
Add a "LINQ to SQL Classes" item to the project ("DemoEmp.dbml").
Using "Server Explorer," drag and drop "Emp" and "Dept" tables as shown in the following Fig 01.
Modify "IEmpService.vb" as shown below:
' NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
<ServiceContract()> _
Public Interface IEmpService
<OperationContract()> _
Sub Add(ByVal objEmp As Emp)
End Interface
Implement the same in "EmpService" (service) class as follows:
Imports System.Data.Linq
Public Class EmpService
Implements IEmpService
Public Sub New()
End Sub
Public Sub Add(ByVal objEmp As Emp) Implements IEmpService.Add
Using ctxt As New DemoEmpDataContext
'adding a new row
ctxt.GetTable(Of Emp).InsertOnSubmit(objEmp)
'saving rows added
ctxt.SubmitChanges()
End Using
End Sub
End Class
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" ("Page2.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
Private Sub AddEmployeeCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
Me.lblMsg.Text = "Added Successfully!"
End Sub
End Class
Press F5 and you should be able to add new employee information (row) to the database using the Silverlight 2.0 page along with WCF and LINQ to SQL.
If you run into any problems, I dedicated few sections to troubleshooting in my previous article. Please go through all of the troubleshooting steps in my previous article. If you do that, you should have no problems. If you need a complete walk-through, consider reading my previous two articles.
In previous sections, we used controls which are not really bound to any of our business objects (such as the "Emp" class or any of its properties). I managed to create my own "Emp" object in the code behind and assigned all properties with textbox values.
We can also achieve the same result by directly binding the controls to our business objects. The "One-Way" binding simply fills controls with properties information related to the business object. However, the values in the controls will not be assigned back to the properties of the business object. The "Two-way" binding works in both directions (properties to controls and vice versa).
All of this will happen in the presentation layer (Silverlight application). For this, we need not modify any of our existing WCF Service or LINQ to SQL.
Add a new "Silverlight User control" (Page3.xaml) and modify the code as follows:
Private Sub AddEmployeeCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
Me.lblMsg.Text = "Added Successfully!"
End Sub
End Class
You must observe the "DataContext" property which is being set to the "LayoutRoot" control. Make sure to modify "App.xaml" as follows so that it will start Page3 as the root.
Private Sub Application_Startup(ByVal o As Object, ByVal e As StartupEventArgs) Handles Me.Startup
The most important property (say attribute) to examine in all of the above textboxes is the "Text" with "Binding" specification. The binding specification includes the word "Binding" followed by a property name and the "Mode," which is "TwoWay."
Once the "Mode" is provided as "TwoWay" and the user has modified any text in the textboxes, the business object (say "Emp" object in this case) automatically reflects the changes. Similarly, if business object values are modified, it would automatically be reflected in the controls.
The business object will generally be assigned to a container control with the help of the "DataContext" property. Once the container control receives the object, it passes the object to all of its child controls and each of the child controls pull whatever information they require (using the Binding specification).
In our case, the DataContext property is set in code-behind as follows:
Private objEmp As EmpService.Emp
Public Sub New()
InitializeComponent()
objEmp = New EmpService.Emp
Me.LayoutRoot.DataContext = objEmp
End Sub
All of the information in "objEmp" is automatically carried to textboxes and vice versa (which is two-way binding).
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; any suggestions, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com