Silverlight 2.0 – SQL İşlemleri için WCF ve Linq To SQL Kullanım

Silverlight uygulamları RIA yapısında oldukları için direk sql işlemleri ile çalışmak yerine bunları bir servis olarak oluşturup kullanmamız önerilmektedir. Bu sebepten ötürü veri tabanı işlemlerimizi yapmak için ister Linq Data Model oluşturduktan sonra WCF servisine querynin yapılacağı metota bağlyoruz. Sonrasında Silverlight projesine Web service referance olarak ekledikten sonra gerekli işlemleri yaparak istediğimiz data kontrollerine ekleyebilinmesi mümkündür.

Sırası ile yapılması aşağıdaki adımları uygularsanız sizin projenizde sorunsuz bir biçimde çalışacaktır.

  • Silverlight projesi oluşturuyoruz.
  • Oluşturulan web projesine Linq To SQL Data Model ekliyoruz.
  • Oluşturulan web projesine WCF Service ekliyoruz ve aşağıdaki kodları interface ve sınıfa ekliyoruz.

IService1.cs

using System.Collections.Generic;
using System.ServiceModel;

namespace SQLData.Web
{
    // NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        List<Customer> GetCustomerGetByLastName(string lastName);
    }
}

Service1.cs

using System.Collections.Generic;
using System.Linq;

namespace SQLData.Web
{
    // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config.
    public class Service1 : IService1
    {

        #region IService1 Members

        public List<Customer> GetCustomerGetByLastName(string lastName)
        {
            var ctx = new DataClasses1DataContext();
            var matchingCustomer = from cust in ctx.Customers
                                   where cust.LastName.StartsWith(lastName)
                                   select cust;
            return matchingCustomer.ToList();
        }

        #endregion
    }
}

  • Yukarıdaki işlemleri gerçekledikten sonra webconfigte ws service content özelliğini basic olarak değiştiriyoruz.
  • Uygulamayı derliyoruz.
  • Sİlverlight uygulaması üzerine ilk olarak Silverlight.Data.Control assembly ‘ını projeye ekliyoruz. Bu işlem silverlight uygulamasında DataGrid kontrolünü kullanabilmenize olanak tanıyacaktır.
  • Silverlight uygulamasına WCF service ini referans olarak ekliyoruz.
  • Sonrasında Page.xaml ve page.xaml.cs ‘e aşağıdaki kod bloklarını ekliyoruz

Page.xaml

<UserControl x:Class="SQLData.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    Width="700" Height="300">
    <Grid x:Name="LayoutRoot" Background="White" ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition />
            <RowDefinition Height="30" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>       
        <TextBlock Text="Lütfen Arayacağınız kişinin soyadını giriniz.." Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left"/>
        <TextBox x:Name="txtLastName" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" Width="150" Text="Lütfen arayın..."/>
        <my:DataGrid x:Name="grd" AlternatingRowBackground="Beige" AutoGenerateColumns="True" Width="700" Height="200" CanUserResizeColumns="True" Grid.Row="1"/>
        <Button x:Name="btnClick" Grid.Row="2" HorizontalAlignment="Center" Content="Ara"/>
    </Grid>
</UserControl>

Page.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;

namespace SQLData
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(Page_Loaded);
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            btnClick.Click += new RoutedEventHandler(btnClick_Click);
        }

        void btnClick_Click(object sender, RoutedEventArgs e)
        {
            ServiceReference1.Service1Client webService = new SQLData.ServiceReference1.Service1Client();
            webService.GetCustomerGetByLastNameCompleted += new EventHandler<SQLData.ServiceReference1.GetCustomerGetByLastNameCompletedEventArgs>(webService_GetCustomerGetByLastNameCompleted);
            webService.GetCustomerGetByLastNameAsync(txtLastName.Text);
        }

        void webService_GetCustomerGetByLastNameCompleted(object sender, SQLData.ServiceReference1.GetCustomerGetByLastNameCompletedEventArgs e)
        {
            grd.ItemsSource = e.Result;
        }
    }
}

Yukarıdaki işlemleri adım adım yaptıktan sonra projemizi çalıştırdığımız da sorunsuzca uygulamamız çalışacaktır. Yaptığımız işlem ise soyadını girdiğimiz kullanıcıları DataGrid kontrolünde listelemektir.

Not: Linq To Sql Data Model de SQL Server 2008 veri tabanı örneklerinden AdventureWorks_LT veri tabanından Customer tablosunu kullandık.

Herkese mutlu günler diliyorum.

Yorum Gönder

0 Yorumlar

Ad Code

Responsive Advertisement