2016-05-15 4 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); 
      } 
     } 
    } 
} 

これは私のCommandクラス

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操作をクラスファイル内に追加します。このクラスをシングルトンクラスにします。それはあなた次第です。 db操作を分離することは、プロジェクトが非常に大きくなった場合、すべてのモデルで処理するのではなく、単一のファイルにあるときにデータを操作することが容易になるということです。

0

あなたの質問は「私のデータベースIOをMVVM ViewModelの中に置くのは良い習慣ですか?この場合の良い議論はこの中にありますMVVM where to put Data Access Layer? "MVVMはどこにデータアクセス層を置くのですか?"正しい方向に向けるべきです。

関連する問題