MVVM与webservice

use*_*618 1 c# wpf web-services mvvm

我打算用MVVM做一个wpf应用程序(它基于http://www.codeproject.com/KB/WPF/MVVMQuickTutorial.aspx ).

此应用程序将每月与webservice连接一个.

在webservice我有合同

public class Student
{
        public string Name {get; set;}
        public int Score {get; set;}
        public DateTime TimeAdded {get; set;}
        public string Comment {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

在WPF应用程序中添加和删除学生将保存到xml文件.

所以在wpf应用程序学生将是这样的:

public class Student
{
    public string Name {get; set;}
    public int Score {get; set;}
    public DateTime TimeAdded {get; set;}
    public string Comment {get; set;}

    public Student(string Name, int Score,
        DateTime TimeAdded, string Comment) {
        this.Name = Name;
        this.Score = Score;
        this.TimeAdded = TimeAdded;
        this.Comment = Comment;
    }
}

public class StudentsModel: ObservableCollection<Student>
{
    private static object _threadLock = new Object();
    private static StudentsModel current = null;

    public static StudentsModel Current {
        get {
            lock (_threadLock)
            if (current == null)
                current = new StudentsModel();

            return current;
        }
    }

    private StudentsModel() 
    {

        // Getting student s from xml
        }
    }

    public void AddAStudent(String Name,
        int Score, DateTime TimeAdded, string Comment) {
        Student aNewStudent = new Student(Name, Score,
            TimeAdded, Comment);
        Add(aNewStudent);
    }
}
Run Code Online (Sandbox Code Playgroud)

如何连接这两个类?

最糟糕的想法我想是来自webservice的合同学生将在这个wpf应用程序中使用来从xml获取学生,在其他应用程序集合中的学生将从数据库中获取.

我是设计模式的新手,所以对我来说很难:/

示例:我单击AddUser,在应用程序A中调用webservice方法将用户添加到数据库,在应用程序B中将用户添加到XML文件,并在应用程序中.基类是webservice的合同.

下一个解释:

第一个应用程序使用webservice在数据库中保存数据.第二个应用程序永远不会在xmls中保存数据,每个perm月将此xmls发送到webservice并将其转换为学生的内容并将其保存在数据库中

dec*_*one 7

从你的问题中不清楚实际问题是什么.但是,我想我可以解决一些问题并向您展示解决问题的方法.

1)我在你的项目中看到的主要问题是你有两个Student类的定义.您可以轻松地将它们合并为单个定义.(我会告诉你如何...)

2)目前还不清楚您是否希望WPF客户将数据保存到Data Source(XML?)或者您Web Service应该这样做.如果WPF客户端应该保存Students,那么它的Web Service用途是什么?

3)在这种情况下,你没有ViewModelStudent类定义任何地方Model.

我创建了一个包含3个项目的示例.

1)WebService- WCF服务项目

2)StudentLib- 类库项目(Student定义类)

3)DesktopClient- WPF应用项目

这是源代码:

WebService.IStudentService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using StudentLib;

namespace WebService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IStudentService" in both code and config file together.
    [ServiceContract]
    public interface IStudentService
    {
        [OperationContract]
        StudentLib.Student GetStudentById(Int32 id);

        [OperationContract]
        void AddStudent(StudentLib.Student student);
    }
}
Run Code Online (Sandbox Code Playgroud)

WebService.StudentService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using StudentLib;

namespace WebService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "StudentService" in code, svc and config file together.
    public class StudentService : IStudentService
    {
        public StudentLib.Student GetStudentById(int id)
        {
            return new StudentLib.Student() { Name = "John Doe", Score = 80, TimeAdded = DateTime.Now, Comment = "Average" };
        }

        public void AddStudent(StudentLib.Student student)
        {
            // Code to add student
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

WebService's Web.Config

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <bindings />
    <client />
    <services>
      <service name="WebService.StudentService" behaviorConfiguration="metaDataBehavior">
        <endpoint address="basic" binding="basicHttpBinding" contract="WebService.IStudentService" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="metaDataBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

StudentLib.Student.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;

namespace StudentLib
{
    [DataContract]
    public class Student
    {
        [DataMember]
        public String Name { get; set; }

        [DataMember]
        public Int32 Score { get; set; }

        [DataMember]
        public DateTime TimeAdded { get; set; }

        [DataMember]
        public String Comment { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

DesktopClient.StudentViewModel.cs

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

namespace DesktopClient
{
    class StudentViewModel
    {
        protected StudentLib.Student Student { get; set; }

        public StudentViewModel(StudentLib.Student student)
        {
            this.Student = student;
        }

        public String Name { get { return Student.Name; } }
        public Int32 Score { get { return Student.Score; } }
        public DateTime TimeAdded { get { return Student.TimeAdded; } }
        public String Comment { get { return Student.Comment; } }
    }
}
Run Code Online (Sandbox Code Playgroud)

DesktopClient.MainWindow.xaml

<Window x:Class="DesktopClient.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Width="400"
        Height="300"
        Loaded="Window_Loaded">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBlock Grid.Column="0"
                   Grid.Row="0">Name :</TextBlock>
        <TextBlock Grid.Column="1"
                   Grid.Row="0"
                   Text="{Binding Name}"></TextBlock>
        <TextBlock Grid.Column="0"
                   Grid.Row="1">Score :</TextBlock>
        <TextBlock Grid.Column="1"
                   Grid.Row="1"
                   Text="{Binding Score}"></TextBlock>
        <TextBlock Grid.Column="0"
                   Grid.Row="2">Time Added :</TextBlock>
        <TextBlock Grid.Column="1"
                   Grid.Row="2"
                   Text="{Binding TimeAdded}"></TextBlock>
        <TextBlock Grid.Column="0"
                   Grid.Row="3">Comment :</TextBlock>
        <TextBlock Grid.Column="1"
                   Grid.Row="3"
                   Text="{Binding Comment}"></TextBlock>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

DesktopClient.MainWindow.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 DesktopClient.StudentService;
using StudentLib;

namespace DesktopClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            IStudentService client = new StudentServiceClient();

            Student student = client.GetStudentById(1);
            DataContext = new StudentViewModel(student);

            client.AddStudent(new StudentLib.Student() { Name = "Jane Doe", Score = 70, TimeAdded = DateTime.Now, Comment = "Average" });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这里解决了上述所有问题:

1)Student该类在项目和项目StudentLib引用的公共程序集()中定义.因此,在添加a时,代码生成器会重用该类.WebServiceDesktopClientService Reference

2)我建议所有与存储相关的操作都在Web Service,Client应用程序应该只用于Web Service存储数据.

3)使用StudentViewModel类而不是Student类来显示数据MainWindow.