我是否正确遵循MVVM模式?

时间:2016-05-15 12:11:50

标签: c# wpf 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

2 个答案:

答案 0 :(得分:0)

您的问题是“将我的数据库IO放入MVVM ViewModel中是一种好习惯吗?”在这种情况下,可以在这个MVVM where to put Data Access Layer?“MVVM中找到对此进行详细讨论的地方放置数据访问层?”这应该指向正确的方向。

答案 1 :(得分:0)

您遵循的MVVM模式不正确。但它可以改善。

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

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