2016-05-15 42 views
0

如果有这个规则,我是否违反了MVVM的规则? 我是否正确连接我的数据库?我是否正确地遵循MVVM模式?

我的XML

<Window x:Class="WpfApplication7.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:viewBox="clr-namespace:WpfApplication7.ViewModel" 
     xmlns:vm="clr-namespace:WpfApplication7.ViewModel.Command" 
     xmlns:local="clr-namespace:WpfApplication7" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <viewBox:viewModel x:Key="testView"/> 
    </Window.Resources> 

    <Grid DataContext="{Binding Source={StaticResource testView}}"> 
     <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Margin="175,47,0,0" Text="{Binding fname, Source={StaticResource testView}}"/> 
     <Button x:Name="button" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="0,95,0,0" Command="{Binding test, Source={StaticResource testView}}"/> 
     <TextBox x:Name="textBox_Copy" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Margin="175,81,0,0" Text="{Binding mname, Source={StaticResource testView}}"/> 
     <TextBox x:Name="textBox_Copy1" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Margin="175,109,0,0" Text="{Binding lname, Source={StaticResource testView}}"/> 
     <TextBox x:Name="textBox_Copy2" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Margin="175,175,0,0" Text="{Binding IDNum}"/> 

    </Grid> 
</Window> 

这是我的ViewModel类

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using WpfApplication7.ViewModel.Command; 
using WpfApplication7.Model; 
namespace WpfApplication7.ViewModel 
{ 
    public class viewModel : INotifyPropertyChanged 
    { 
     private databaseTest obj = new databaseTest(); 
     public btnTest test { get; set; } 
     public viewModel() 
     { 
      test = new btnTest(this); 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     private void OnPropertyChanged(string property) 
     { 
      if(PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(property)); 

      } 
     } 

     public string fname 
     { 
      get { return obj.firstname; } 
      set { obj.firstname = value; 
       OnPropertyChanged("fname"); 
      } 
     } 
     public string mname 
     { 
      get { return obj.middlename; } 
      set { obj.middlename = value; 
       OnPropertyChanged("mname"); 
      } 
     } 
     public string lname 
     { 
      get { return obj.lastname; } 
      set { obj.lastname = value; 
       OnPropertyChanged("lname"); 
      } 
     } 

     public int IDNum 
     { 
      get { return obj.idNum; } 
      set { obj.idNum = value; 
       OnPropertyChanged("IDNum"); 
      } 
     } 

     public void searchBtn() 
     { 

      obj.sql = @"Provider = Microsoft.ACE.OLEDB.12.0; data source = C:\Users\veekat\Documents\VeeKat.mdb"; 
      obj.conn = new System.Data.OleDb.OleDbConnection(obj.sql); 
      obj.cmd = new System.Data.OleDb.OleDbCommand("select * from info where ID = "+ this.IDNum +"", obj.conn); 

      try 
      { 
       obj.conn.Open(); 
       obj.dr = obj.cmd.ExecuteReader(); 

       if (obj.dr.Read()) 
       { 
        this.fname = obj.dr["Firstname"].ToString(); 
        this.mname = obj.dr["Middlename"].ToString(); 
        this.lname = obj.dr["Lastname"].ToString(); 
       } 
      } 
      catch(Exception ex) 
      { 
       Console.WriteLine(ex.Message); 
      } 
     } 
    } 
} 

这是我的命令类

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Input; 

namespace WpfApplication7.ViewModel.Command 
{ 
    public class btnTest : ICommand 
    {  
     public viewModel viewM { get; set; } 

     public btnTest(viewModel vw) 
     { 
      this.viewM = vw; 
     } 

     public event EventHandler CanExecuteChanged; 

     public bool CanExecute(object parameter) 
     { 
      return true; 
     } 

     public void Execute(object parameter) 
     { 
      viewM.searchBtn(); 
     } 
    } 
} 

在这里,我的模型类

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Data; 
using System.Data.OleDb; 
namespace WpfApplication7.Model 
{ 
    public class databaseTest 
    { 
     private string Firstname; 
     private string Middlename; 
     private string Lastname; 

     public string firstname 
     { 
      get { return Firstname; } 
      set { Firstname = value; } 
     } 

     public string middlename 
     { 
      get { return Middlename; } 
      set { Middlename = value; } 
     } 

     public string lastname 
     { 
      get { return Lastname; } 
      set { Lastname = value; } 
     } 

     private int num; 

     public int idNum 
     { 
      get { return num; } 
      set { num = value; } 
     } 

     public string sql { get; set; } 
     public OleDbConnection conn { get; set; } 
     public OleDbCommand cmd { get; set; } 
     public OleDbDataReader dr { get; set; } 
    } 
} 

到目前为止我没有任何错误,但我想知道您的意见。 as you can see, i search it using the id number

+0

我投票结束这个问题,因为它更适合codereview.stackexchange.com。 –

+0

即时对不起...这是我第一次发布这里对不起... –

回答

0

您所遵循的MVVM模式不正确。但可以改进。

在我看来,最好的办法是将数据库相关的东西分离到一个新的类文件。

添加新的类文件DBManager并在类文件中添加所有与数据库相关的CRUD操作。使这个类成为单例类。随你便。分离数据库操作的用途是,当你的项目变得非常大时,处理数据时很容易处理它,而不是在每个模型中处理它。

0

是你的问题“是否将我的数据库IO放入我的MVVM ViewModel?在这种情况下,可以在此MVVM where to put Data Access Layer?“MVVM放置数据访问层的位置”中找到有关此问题的良好讨论。这应该指向正确的方向。