MVVM最佳实践与保持简单

时间:2012-06-30 00:07:15

标签: wpf mvvm

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

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

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

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

我的代码:

//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}" />

其他人的代码:

//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 个答案:

答案 0 :(得分:0)

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