简体   繁体   English

MVVM中的分层数据问题

[英]Hierarchical data problems in MVVM

Do you think this Model-ViewModel implementation is correct? 您认为此Model-ViewModel实现是否正确? Note that Stars (in the VM) is an ObservableCollection of VMs inside another one, and i feel like it decouples the VM from the M because when removing an element from Stars, i still have to delete it's model manually. 请注意,Stars(在VM中)是另一个VM中的一个ObservableCollection VM,我感觉它使VM与M分离,因为从Stars中删除元素时,我仍然必须手动删除其模型。

Any ideas on how to improve this without using OnCollectionChanged? 关于如何在不使用OnCollectionChanged的情况下改善此问题的任何想法? Thanks in advance. 提前致谢。

Models: 楷模:

public class Galaxy
{
    public Galaxy(string name, IList<Star> stars)
    {
        Name = name;
        Stars = stars;
    }
    public string Name { get; set; }
    public IList<Star> Stars { get; set; }
}

public class Star
{
    public Star(string name)
    {
        Name = name;
    }
    public string Name { get; set; }
}

ViewModels : ViewModels:

public class GalaxyVM : ViewModelBase
{
    private Galaxy _galaxy;
    private ObservableCollection<StarVM> _stars;

    public GalaxyVM(Galaxy galaxy)
    {
        _galaxy = galaxy;
        _stars = new ObservableCollection<StarVM>(from sys in _galaxy.Stars
                                                  select new StarVM(sys));
    }

    public string Name
    {
        get { return _galaxy.Name; }
    }
    public ObservableCollection<StarVM> Stars
    {
        get { return _stars; }
    }
}

public class StarVM : ViewModelBase
{
    private Star _star;
    public StarVM(Star star)
    {
        _star = star;
    }
    public string Name
    {
        get { return _star.Name; }
    }
}

Well, there's decoupling and there's decoupling. 好吧,这里有去耦,也有去耦。

It's one thing to have a model that doesn't know anything about the implementation details of its views. 拥有对视图的实现细节一无所知的模型是一回事。 That's good. 那很好。

It's another thing to have a model that can't be used in dynamic views. 拥有无法在动态视图中使用的模型是另一回事。 If a model class doesn't implement change notification, it's going to be difficult to reliably keep the UI in sync with the state of the model. 如果模型类没有实现更改通知,那么将很难可靠地使UI与模型状态保持同步。 You can try and phony up change notification by putting it all into your view classes, but at the end of the day there's going to be code somewhere that changes the state of the model directly, and if it doesn't implement change notification the views aren't going to know about it and the UI will get out of sync. 您可以尝试通过将变更通知全部放入视图类中来伪造变更通知,但是到最后,将有代码直接在某个地方更改模型的状态,并且如果未实现变更通知,则视图不会了解它,UI将会不同步。

I probably wouldn't implement an IList<Star> property with a public setter in Galaxy . 我可能不会在Galaxy使用公共设置器实现IList<Star>属性。 I'd make the Stars property of type StarList , and create a StarList class that implemented IList<Star> , unless I had a very good reason not to. 我会做的Stars型物业StarList ,并创建一个StarList类实现IList<Star> ,除非我有一个很好的理由不这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM