【发布时间】:2012-05-23 16:13:37
【问题描述】:
我的问题是:如何管理视图模型的继承链?
我的情况:
我有一个标准的 ViewModelBase,它只实现了 INotifyPropertyChanged 接口。
此外,我有一个 BusinessObjectViewModel 有一个 Guid,一个 PersonBaseViewModel 有个人核心数据,一个 CustomerViewModel 有客户相关的东西和EmployeeViewModel 与员工相关的东西。
所有视图模型当然都封装了一个模型对象(Customer、Employee、PersonBase)。
- BusinessObjectViewModel 继承自 ViewModelBase
- PersonBaseViewModel 继承自 BusinessObjectViewModel
- CustomerViewModel 继承自 PersonBaseViewModel
- EmployeeViewModel 继承自 PersonBaseViewModel
模型通过构造函数进入视图模型。
如果我使用构造函数链(每个视图模型都调用基本构造函数),每个视图模型都有它的模型来从模型返回封装的值。
但我必须在每个视图模型中都有一个模型属性。对于 CustomerViewModel,我将在 CustomerViewModel 中有一个引用,一个在 PersonBaseViewModel 中,一个在 BusinessObjectViewModel 中用于同一个对象。这对我来说听起来很愚蠢。
或者我必须在上层视图模型中转换每个属性访问。
附言这只是我模型层次结构的一小部分。
提前致谢。
【问题讨论】:
标签: c# wpf inheritance mvvm viewmodel