Designing WCF DataContract Classes Using the LINQ to SQL Designer - Inheriting WCF DataContract classes visually using the LINQ to SQL Designer: accessing
(Page 5 of 5 )
In the previous section, we saw how we can inherit existing "DataContract" classes easily using the LINQ to SQL Designer. Now, we need to have this show up in a Silverlight application.
Modify IEmpService.vb so that it looks like the following:
<ServiceContract()> _
Public Interface IEmpService
<OperationContract()> _
Function GetEnameDname() As List(Of EnameDname)
<OperationContract()> _
Function GetEnameDnameSal() As List(Of EnameDnameSal)
End Interface
Add a new method to EmpService.vb as follows:
Public Function GetEnameDnameSal() As System.Collections.Generic.List(Of EnameDnameSal) Implements IEmpService.GetEnameDnameSal
Using ctxt As New DemoEmpDataContext
ctxt.ObjectTrackingEnabled = False
Return (From p In ctxt.Emps _
Select New EnameDnameSal With {.Ename = p.Ename, .Dname = p.Dept.Dname, .Sal = p.Sal}).ToList
End Using
End Function
Modify the existing markup so that it looks like the following:
<UserControl x:Class="DemoSL.Page5"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data">
<StackPanel Height="Auto" HorizontalAlignment="Stretch" x:Name="LayoutRoot" VerticalAlignment="Stretch" Background="White" Orientation="Vertical">
<Button Height="27" HorizontalAlignment="Left" x:Name="btnShow" Width="80" Content="Show"/>
<Button Height="27" HorizontalAlignment="Left" x:Name="btnShowSalAlso" Width="90" Content="Show Sal Also"/>
<TextBlock Height="26" HorizontalAlignment="Left" x:Name="lblMsg" Width="350" Text="" TextWrapping="Wrap"/>
<ScrollViewer Height="243" HorizontalAlignment="Stretch" Width="Auto">
<data:DataGrid x:Name="dgEmpDept"/>
</ScrollViewer>
</StackPanel>
</UserControl>
And finally, modify the code-behind so that it looks like the following:
Partial Public Class Page5
Inherits UserControl
Public Sub New()
InitializeComponent()
End Sub
Private Sub btnShow_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnShow.Click
Me.lblMsg.Text = "Fetching..."
Dim objService As New EmpService.EmpServiceClient
AddHandler objService.GetEnameDnameCompleted, AddressOf EmployeeListFetched
objService.GetEnameDnameAsync()
End Sub
Private Sub EmployeeListFetched(ByVal sender As Object, ByVal e As EmpService.GetEnameDnameCompletedEventArgs)
Me.dgEmpDept.ItemsSource = e.Result
Me.lblMsg.Text = "Fetched!"
End Sub
Private Sub btnShowSalAlso_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnShowSalAlso.Click
Me.lblMsg.Text = "Fetching..."
Dim objService As New EmpService.EmpServiceClient
AddHandler objService.GetEnameDnameSalCompleted, AddressOf EmployeeListFetched
objService.GetEnameDnameSalAsync()
End Sub
Private Sub EmployeeListFetched(ByVal sender As Object, ByVal e As EmpService.GetEnameDnameSalCompletedEventArgs)
Me.dgEmpDept.ItemsSource = e.Result
Me.lblMsg.Text = "Fetched!"
End Sub
End Class
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
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |