开发者

Modifying an existing MVVM infrastructure by adding View State feature

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 { ... }
}
  1. Is it a good idea to attach View State feature to ViewModel?
  2. Is it a good solution to introduce tied relation between specific ViewModel typa and Vie开发者_运维问答wState through the generic Type parameter like class GridViewModel<GridViewState>?
  3. Where and why better to define such methods like LoadState() / SaveState(), IViewState itself or ViewModelBase?
  4. Are there another design solutions?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜