MVVM最佳实践与保持简单

时间:2023-01-22 20:17:18

The first code section below is the code I am tempted to write. The second code section below is what a previous code worker wrote when trying to achieve the same task.

下面的第一个代码部分是我想写的代码。下面的第二个代码部分是前一个代码工作者在尝试实现相同任务时所写的内容。

The previous co-worker's code seems to follow standard MVVM practice on having a seperate ViewModel for each type of item, of keeping track of SelectedItems in the ViewModel rather than the view, and of avoiding ObservableCollection in the model.

以前的同事的代码似乎遵循标准MVVM实践,为每种类型的项目分别设置ViewModel,跟踪ViewModel中的SelectedItems而不是视图,并避免模型中的ObservableCollection。

The code I am tempted to write is about half the size and complexity, with less potential for the model and ViewModel getting out of sync, and far less lines of code.

我试图编写的代码大小和复杂程度只有一半,模型和ViewModel失去同步的可能性更小,代码行也少得多。

Is MVVM best practice really the right answer here? Is there some sort of middle ground combining the best of both versions?

MVVM最佳实践真的是正确答案吗?是否有某种中间结合两种版本的最佳结合?

My code:

//Model

public class Cheese
{
    public string Name { get; set; }
    public int Tastiness { get; set; }
    public Color Color { get; set; }
}

public class CheeseEditorModel
{
    public ObservableCollection<Cheese> Cheeses { get; private set; }

    public CheeseEditorModel()
    {
        //read cheeses in from file/database/whatever
    }

    public DeleteCheeses(SelectedObjectCollection selected)
    {
        //delete cheeses
    }
}

//ViewModel

public class CheeseEditorViewModel
{
    private CheeseEditorModel _model;
    public ObservableCollection<Cheese> Cheeses { get {return _model.Cheeses} }

    public CheeseEditorViewModel()
    {
        _model = new CheeseEditorModel();
    }

    public DeleteSelected(SelectedObjectCollection selected)
    {
        _model.Delete(selected);
    }
}

//XAML

<ListBox Name="CheeseListBox" ItemsSource={Binding Path="Cheeses"} />
<Button Command={Binding DeleteSelected} CommandParameter="{Binding ElementName=CheeseListBox, Path=SelectedItems}" />

Other person's code:

其他人的代码:

//Model

public class Cheese
{
    public string Name { get; set; }
    public int Tastiness { get; set; }
    public Color Color { get; set; }
}

public class CheeseEditorModel
{
    public List<Cheese> Cheeses { get; private set; }

    public CheeseDataModel()
    {
        //read cheeses in from file/database/whatever
    }

    public DeleteCheeses(IEnumerable<Cheese> toDelete)
    {
        //delete cheeses
    }
}

//ViewModel

public class CheeseViewModel
{
    private Cheese _cheese { get; set; }
    public bool IsSelected { get; set; }

    public CheeseViewModel(Cheese cheese)
    {
        _cheese = cheese;
        IsSelected = false;
    }

    public string Name {get {return _cheese.Name} set { _cheese.Name = value } }
    public int Tastiness {get {return _cheese.Tastiness} set { _cheese.Tastiness= value } }
    public Color Color {get {return _cheese.Color} set { _cheese.Color = value } }
}

public class CheeseEditorViewModel
{
    private CheeseEditorModel _model;
    public ObservableCollection<CheeseViewModel> Cheeses { get; private set; }

    public CheeseEditorViewModel()
    {
        _model = new CheeseEditorModel();
        foreach (cheese in _model.Cheeses)
            Cheeses.Add(cheese);
    }

    public DeleteSelected()
    {
        var selected = from cheese in Cheeses select cheese.CheeseModel where cheese.IsSelected();
        _model.Delete(selected);
        var selectedVM = from cheese in Cheeses select cheese where cheese.IsSelected();
        foreach (cheese in selectedVM)
            Cheeses.Remove(selected);
    }
}

//XAML

<ListBox ItemsSource={Binding Path="Cheeses"}>
<ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
    </Style>
</ListBox.ItemContainerStyle>
</ListBox>
<Button Command={Binding DeleteSelected} />

1 个解决方案

#1


0  

does Cheese implement INotifyPropertyChanged? if yes why? but nevertheless i go with the other's person code. you can easily add new non model related stuff

奶酪实施INotifyPropertyChanged?如果是,为什么?但我仍然使用对方的人员代码。您可以轻松添加新的非模型相关的东西

#1


0  

does Cheese implement INotifyPropertyChanged? if yes why? but nevertheless i go with the other's person code. you can easily add new non model related stuff

奶酪实施INotifyPropertyChanged?如果是,为什么?但我仍然使用对方的人员代码。您可以轻松添加新的非模型相关的东西