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

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

问题描述

域模型集合(通常为List或IEnumerable)委派到ViewModel。



这表示我的CustomerViewModel有一个订单集合类型为List或IEnumerable。



绑定控件未识别列表中的任何更改。但是用ObservableCollection就是这样。



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



更新:我如何做的示例:

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

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

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

public Sc​​hoolclass GetSchoolclass
{
get {return _schoolclass; }
}

public int ID {get;组; }

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

}
}

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


解决方案>

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



我认为底线是这只是WPF和数据绑定工作方式的现实。为了使双向数据绑定操作,集合需要通知绑定到它们的控制的手段,并且在大多数域对象中使用的标准列表和集合不会/不应/不应该支持这一点。正如我在注释中提到的,需要为非集合属性实现INotifyPropertyChanged是标准域对象不能满足的另一个要求。



域对象不是打算成为视图模型,因此您可能会发现需要在两种类型的对象之间来回映射。这不需要在域对象和数据访问对象之间来回映射。每种类型的对象在系统中都有不同的功能,每个对象都应该专门设计来支持自己在系统中的角色。



总之,Agies使用AOP自动生成代理类的想法非常有趣,我打算研究一下。


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

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

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

This is a problem in the MVVM design pattern.

How do you cope with it?

UPDATE: Sample of how I do it:

 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. :)

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.

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天全站免登陆