通过添加视图状态功能来修改现有的 MVVM 基础架构
我将在现有的 MVVM WPF 应用程序中介绍视图状态功能。目标是能够保存和加载(恢复)控件的特定状态。
问题更多的是从系统灵活性/可维护性角度来看的设计和最佳解决方案。
当前基础设施:
public abstract class ViewModelBase
{
protected ViewModelBase(...)
{
}
}
// and few more very the same ViewModel classes for different control types
public sealed class GridViewModel : ViewModelBase
{
protected GridViewModel(...)
: base(...)
{
}
}
我引入了IViewState接口
,因此每个特定的ViewModel都可以提供自己的实现,例如GridViewState类
,并将其放入ViewModel基础设施中以下方式:(想法是将 ViewState 的类型作为通用参数传递)
public abstract class ViewModelBase<TViewState>
where TViewState : class, IViewState
{
protected ViewModelBase(...)
{
}
public TViewState ViewState { ... }
}
- 将 View State 功能附加到 ViewModel 是个好主意吗?
- 通过像
class GridViewModel
这样的通用类型参数来引入特定ViewModel类型和ViewState之间的绑定关系是一个很好的解决方案吗? - 在哪里以及为什么更好地定义诸如
LoadState() / SaveState()、
IViewState
本身或ViewModelBase
之类的方法? - 还有其他设计解决方案吗?
I'm going to introduce View State feature in the existing MVVM WPF Application. The objective is to be able to Save and Load (restore) particular state of a control.
The question is more about design and best solution from system flexibility/maintability perspectives.
Current infrastructure:
public abstract class ViewModelBase
{
protected ViewModelBase(...)
{
}
}
// and few more very the same ViewModel classes for different control types
public sealed class GridViewModel : ViewModelBase
{
protected GridViewModel(...)
: base(...)
{
}
}
I'm introduced IViewState interface
so each specific ViewModel could provide own implementation like GridViewState class
and going to put it in ViewModel infrastructure in following way: (Idea is to pass type of ViewState as generic parameter)
public abstract class ViewModelBase<TViewState>
where TViewState : class, IViewState
{
protected ViewModelBase(...)
{
}
public TViewState ViewState { ... }
}
- Is it a good idea to attach View State feature to ViewModel?
- Is it a good solution to introduce tied relation between specific ViewModel typa and ViewState through the generic Type parameter like
class GridViewModel<GridViewState>
? - Where and why better to define such methods like
LoadState() / SaveState(),
IViewState
itself orViewModelBase
? - Are there another design solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否有理由不保留域对象,而只是在加载视图模型时重建视图模型?您的视图会持有哪种状态,而您的域类不会?如果它与位置和位置等视觉元素相关,我会将它们推入设置类并保留它。
我假设有一些代表您的领域的底层逻辑和类,如果没有,那么您的 ViewModel 确实是您的领域,并且您的想法是可靠的。
我可能会考虑类似 memento 模式来补充您的虚拟机,因为它们会可能有相当少的事件和其他关系(例如集合项的属性更改事件)无法序列化并且必须重新创建。
那里有很多持久性模式,但从您的命名和解释纪念品来看,它看起来很合适。
Is there a reason you don't persist your domain objects and just rebuild the ViewModels from those when they're loaded? What kind of state would your views hold that your domain classes wouldn't? If it's related to visual elements such as position and location I would push those into a settings class and persist that.
I'm assuming there's some underlying logic and classes which represent your domain, if not then your ViewModels really are your domain and your idea is solid.
I'd probably look at something like the memento pattern to rehydrate your VMs since they'll likely have a fair few events and other relationships (e.g. property changed events of collection items) which can't be serialized and must be recreated.
There are plenty of persistence patterns out there but judging by your naming and explanation memento looks a good fit.