WPF/MVVM:将域模型集合委托给 ViewModel [英] WPF/MVVM: Delegating a domain Model collection to a ViewModel

查看:27
本文介绍了WPF/MVVM:将域模型集合委托给 ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

域模型集合(通常是 List 或 IEnumerable)委托给 ViewModel.

A domain model collection (normally a List or IEnumerable) is delegated to a ViewModel.

这意味着我的 CustomerViewModel 有一个 List 或 IEnumerable 类型的订单集合.

Thats means my CustomerViewModel has a order collection of type List or IEnumerable.

绑定控件无法识别列表中的任何更改.但是有了 ObservableCollection 就可以了.

No change in the list is recognized by the bound control. But with ObservableCollection it is.

这是 MVVM 设计模式中的一个问题.

This is a problem in the MVVM design pattern.

你是如何应对的?

更新:我的做法示例:

 public class SchoolclassViewModel : ViewModelBase
{
    private Schoolclass _schoolclass;
    private ObservableCollection<PupilViewModel> _pupils = new ObservableCollection<PupilViewModel>();        

    public SchoolclassViewModel(Schoolclass schoolclass)
    {
        _schoolclass = schoolclass;
        _schoolclass.Pupils = new List<Pupil>();

        foreach (var p in schoolclass.Pupils)           
            Pupils.Add(new PupilViewModel(p));            
    }

    public Schoolclass GetSchoolclass 
    {
        get { return _schoolclass; } 
    }

    public int ID { get; set; }       

    public string SchoolclassName
    {
        get { return _schoolclass.SchoolclassName;}
        set
        { 
            if(_schoolclass.SchoolclassName != value)
            {                    
                _schoolclass.SchoolclassName = value;
                this.RaisePropertyChanged("SchoolclassName");
            }

        }
    }   

    public ObservableCollection<PupilViewModel> Pupils
    {
        get{ return _pupils;}
        set
        {
            _pupils = value;
            this.RaisePropertyChanged("Pupils");
        } 
    }
}

推荐答案

好的,我会继续添加我的想法作为答案而不是在评论中.:)

Ok, I'll go ahead and add my thoughts as an answer instead of in the comments. :)

我认为最重要的是,这就是 WPF 和数据绑定工作方式的现实.为了实现双向数据绑定,集合需要一种通知绑定到它们的控件的方法,并且大多数域对象中使用的标准列表和集合不/不会/不应该支持这一点.正如我在评论中提到的,需要为非集合属性实现 INotifyPropertyChanged 是标准域对象可能无法满足的另一个要求.

I think the bottom line is that this is just the reality of the way WPF and databinding work. In order for two-way databinding to operate, collections need a means of notifying controls that are bound to them, and the standard lists and collections used in most domain objects don't/won't/shouldn't support this. As I mentioned in a comment, being required to implement INotifyPropertyChanged for non-collection properties is another requirement that may not be met by a standard domain object.

域对象不打算成为视图模型,因此您可能会发现需要在两种类型的对象之间来回映射.这与必须在域对象和数据访问对象之间来回映射没有什么不同.每种类型的对象在系统中都有不同的功能,并且每种类型都应专门设计以支持其在系统中的角色.

Domain objects are not intended to to be viewmodels, and for this reason you may find that you need to map back and forth between the two types of objects. This is not dissimilar to having to map back and forth between domain objects and data access objects. Each type of object has a different function in the system, and each should be specifically designed to support their own role in the system.

综上所述,Agies 使用 AOP 自动生成代理类的想法非常有趣,我打算研究一下.

All that said, Agies's idea of using AOP to automatically generate proxy classes is very interesting, and something I intend to look into.

这篇关于WPF/MVVM:将域模型集合委托给 ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆