question

lookforme-8548 avatar image
0 Votes"
lookforme-8548 asked LanHuang-MSFT answered

Connect WPF with WCF + using a Database

So I've written this small programm, where I use WCF and WPF. I also use a Connection with a Database. Does this look alright?

IService:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Runtime.Serialization;
 using System.ServiceModel;
 using System.ServiceModel.Web;
 using System.Text;
    
 namespace WeatherDataWCF
 {
     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
     [ServiceContract]
     public interface IService1
     {
    
         // TODO: Add your service operations here
    
         [OperationContract]
    
         List<WeatherDataDTO> GetWeatherData(RangeFilter rangeFilter);
     }
    
     [DataContract]
     public class RangeFilter
     {
         [DataMember]
         public string From { get; set; }
    
         [DataMember]
         public string To { get; set; }
    
         public RangeFilter() { }
    
         public RangeFilter(string From, string To)
         {
             this.From = From;
             this.To = To;
         }
     }
    
     [DataContract]
     public class WeatherDataDTO
     {
         [DataMember]
         public int Id { get; set; }
    
         // send as string because serialization error with DateTime
         [DataMember]
         public string Date { get; set; }
    
         [DataMember]
         public string Time { get; set; }
    
         [DataMember]
         public double Temperature { get; set; }
    
         [DataMember]
         public int Pressure { get; set; }
    
         [DataMember]
         public int Rain { get; set; }
    
         [DataMember]
         public double Wind { get; set; }
    
         [DataMember]
         public int Direction { get; set; }
    
         [DataMember]
         public int Humidity { get; set; }
     }
 }


Service:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Runtime.Serialization;
 using System.ServiceModel;
 using System.ServiceModel.Web;
 using System.Text;
    
 namespace WeatherDataWCF
 {
     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
     // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
     public class Service1 : IService1
     {
         private WeatherDataEntities context = new WeatherDataEntities();
         public List<WeatherDataDTO> GetWeatherData(RangeFilter rangeFilter)
         {
             List<WeatherDataDTO> weatherDataDTOs = new List<WeatherDataDTO>();
    
             var start = parseDateTime(rangeFilter.From);
             var end = parseDateTime(rangeFilter.To);
    
             var query = from WeatherData in context.WeatherData
                         where WeatherData.Datum >= start && WeatherData.Datum <= end
                         select WeatherData;
    
             var i = 0; 
    
             foreach (var weatherData in query)
             {
                 weatherDataDTOs.Add(new WeatherDataDTO
                 {
                     Id = weatherData.Id,
                     Date = weatherData.Datum.ToString(),
                     Direction = weatherData.Richtung.Value,
                     Humidity = weatherData.Feuchtigkeit.Value,
                     Pressure = weatherData.Luftdruck.Value,
                     Rain = weatherData.Regen.Value,
                     Temperature = weatherData.Temperatur.Value,
                     Time = weatherData.Zeit.Value.ToString(),
                     Wind = weatherData.Wind.Value
                 });
    
                 if(i == 100)
                 {
                     break;
                 }
                 i++;
             }
    
             return weatherDataDTOs;
         }
    
         private DateTime parseDateTime(string dateString)
         {
             return DateTime.Parse(dateString);
         }
     }
 }

MainWindow Code:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Data;
 using System.Windows.Documents;
 using System.Windows.Input;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using System.Windows.Navigation;
 using System.Windows.Shapes;
 using WeatherDataWPF.ServiceReference1;
    
 namespace WeatherDataWPF
 {
     /// <summary>
     /// Interaction logic for MainWindow.xaml
     /// </summary>
     public partial class MainWindow : Window
     {
         private ServiceReference1.IService1 client = new ServiceReference1.Service1Client(); 
            
         public MainWindow()
         {
             InitializeComponent();
             this.fromPicker.SelectedDate = new DateTime(2013, 1, 1);
             this.toPicker.SelectedDate = new DateTime(2013, 1, 5);
         }
    
         private void Button_Click(object sender, RoutedEventArgs e)
         {
    
             var from = this.fromPicker.SelectedDate.ToString().Split(' ')[0];
             var to = this.toPicker.SelectedDate.ToString().Split(' ')[0];
    
    
             var filter = new RangeFilter
             {
                 From = from,
                 To = to
             };
    
             var data = this.client.GetWeatherData(filter);
             this.grid.ItemsSource = data;
         }
     }
 }

This is how the Xaml Code looks like:

 <Window x:Class="WeatherDataWPF.MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:WeatherDataWPF"
         mc:Ignorable="d"
         Title="MainWindow" Height="450" Width="800">
     <Grid>
         <DatePicker x:Name="fromPicker" HorizontalAlignment="Left" Margin="163,68,0,0" VerticalAlignment="Top"/>
         <DatePicker x:Name="toPicker" HorizontalAlignment="Left" Margin="399,68,0,0" VerticalAlignment="Top"/>
         <Label Content="Anfangsdatum:" HorizontalAlignment="Left" Margin="58,66,0,0" VerticalAlignment="Top"/>
         <Label Content="Enddatum:" HorizontalAlignment="Left" Margin="313,66,0,0" VerticalAlignment="Top"/>
         <Button Content="Ausgabe" HorizontalAlignment="Left" Margin="555,69,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
         <DataGrid x:Name="grid" HorizontalAlignment="Left" Height="239" Margin="58,118,0,0" VerticalAlignment="Top" Width="546"/>
    
     </Grid>
 </Window>

Is it smart, that I use the RangeFilter? Should I use Data Binding? Would that be a better solution?

windows-wpfwindows-wcf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered

Hi @lookforme-8548,
I think it is possible to use RangeFilter, the filter can be cached, so it will be faster.
Best regards,
Lan Huang


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.