保存和加载数据“MVVM"大大地? [英] Save and Load data "the MVVM" way?

查看:37
本文介绍了保存和加载数据“MVVM"大大地?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 Telerik 提供的一些控件进行 C# WPF 项目,并且我尊重 MVVM 模式:

I'm currently working on a C# WPF project using some of the Controls provided by Telerik and I'm respecting the MVVM pattern with:

  • 包含数据的模型

  • a Model containing the data

将数据呈现给视图的 ViewModel

a ViewModel presenting the data to the View

显示数据的视图

当然,有些模型可以重复使用并显示在多个视图中(在我的例子中,数据可以是显示在不同图表上的形状的内容).

Of course, some of the Models can be reused and displayed in several Views (in my case, the data can be the content of shapes displayed on different diagrams).

现在我开始设计如何保存数据.我的目标很简单:当用户离开应用程序并返回时,所有视图在内容、颜色、字体、大小、空间位置等方面都必须相同......

Now I'm starting to design how to save the data. My goal is very simple: when the user leaves the application and comes back, all Views must be identical in terms of content, color, fonts, size, position in space...

保存模型仅涵盖内容部分.您将如何保存显示属性,如颜色、字体、空间位置,尤其是当多个 View 依赖于同一个 Model 时?我应该使用 Bindings 并将所有属性从 View 移动到 Model 吗?有模型复杂度会大大增加的风险吗?

Saving a Model covers only the content part. How would you save the display properties like color, fonts, position in space, especially when several Views will rely on the same Model ? Should I use Bindings and move all properties from the View to the Model ? With the risk that the Model complexity will greatly increase ?

您有什么解决方案可以将 UI 属性与模型分开吗?

Do you have any solution where you keep the UI properties separated from the Model ?

还有关于如何以 MVVM 方式"保存和加载数据的最佳实践吗?

Also is there any best practice on how to save and load data "the MVVM way" ?

提前致谢.

推荐答案

我将类似的用户选项保存在应用程序设置中.如果您不熟悉它们,可以在 在 C# 中使用设置 MSDN 页面.简而言之,您可以拥有应用程序和用户设置,听起来您希望每个用户保存用户设置.UI 属性在任何模型中都没有位置,因为将此类信息与模型数据一起存储没有任何好处.你可以对他们做这样的事情:

I save user options like that in the Application Settings. If you're not familiar with them, you can find out the full story on the Using Settings in C# page on MSDN. In short, you can have Application and User Settings and it sounds like you'd want User Settings that are saved per user. UI properties have no place in any model as there is no benefit from storing that kind of information along with model data. You can do this kind of thing with them:

private void LoadSettings(MainWindow window)
{
    Settings.Default.Reload();
    window.WindowStartupLocation = WindowStartupLocation.Manual;
    window.Left = Settings.Default.ApplicationLocation.X;
    window.Top = Settings.Default.ApplicationLocation.Y;
    window.Width = Settings.Default.ApplicationSize.Width;
    window.Height = Settings.Default.ApplicationSize.Height;
    window.WindowState = Settings.Default.IsApplicationMaximised ? WindowState.Maximized : WindowState.Normal;
}

private void SaveSettings(MainWindow window)
{
    Settings.Default.ApplicationLocation = new Point(window.Left, window.Top);
    Settings.Default.ApplicationSize = new Size(window.Width, window.Height);
    Settings.Default.IsApplicationMaximised = window.WindowState == WindowState.Maximized;
    Settings.Default.Save();
}

将一些属性添加到基本或主视图模型可能会更容易,以便您可以将数据绑定到它们:

It might be easier to add some of the properties to a base or main view model, so that you can data bind to them:

public void SaveSettings(string tabName)
{
    Settings.Default.ReleaseTrackSideFormat = StateManager.ReleaseTrackSideFormat;
    Settings.Default.ReleaseLabelCopyFormat = StateManager.ReleaseLabelCopyFormat;
    Settings.Default.ReleaseExportDestination = StateManager.ReleaseExportDestination;
    Settings.Default.ReleaseSearchOptions = new SerializableReleaseSearchOptions(ReleaseSearchOptions);
    ...
    Settings.Default.Save();
}

public void LoadSettings()
{
    Settings.Default.Reload();
    StateManager.ReleaseTrackSideFormat = Settings.Default.ReleaseTrackSideFormat;
    StateManager.ReleaseLabelCopyFormat = Settings.Default.ReleaseLabelCopyFormat;
    StateManager.ReleaseExportDestination = Settings.Default.ReleaseExportDestination;
    ReleaseSearchOptions = new ReleaseSearchOptions(Settings.Default.ReleaseSearchOptions);
    ReleaseExportSearchOptions = new ReleaseExportSearchOptions(Settings.Default.ReleaseExportSearchOptions);
    ...
}

<小时>

更新>>>


UPDATE >>>

您说的很对……您不希望以这种方式存储模型数据.这是针对与 UI 相关的用户首选项.如果您还询问如何保存模型数据,那么快速回答是我会将我的数据存储在数据库中,但这取决于您.您可以轻松地将其存储在计算机上的文件中.这一切都取决于规模、便利性、速度、资源访问等,所以这不是一个真正适合本网站范围的问题.

You're quite right... you wouldn't want to store your model data in this way. This is for UI related user preferences. If you're also asking how to save your model data, then the quick answer is that I'd store mine in a database, but that's up to you. You could just as easily store it in a file on your computer. It all depends on scale, convenience, speed, access to resources, etc., so it's not really a question that is suitable in scope for this website.

但是,网上有大量教程逐步展示了保存数据的不同方法.要回答 那个 问题,我建议您关注其中的一些问题.

However, there are plenty of tutorials online showing different ways of saving data step by step. To answer that question, I recommend that you follow some of them.

我可以告诉您的是,习惯上将您的数据访问代码放入单独项目(或小项目的文件夹)中的类中.此类通常仅在父视图模型属性或基础视图模型属性中引用,并且所有子视图模型都将通过此访问它们的数据......也许是这样的:

What I can tell you is that it is customary to put your data access code into a class in a separate project (or folder for a small project). This class is then often referenced only in a parent, or base view model property and all child view models would access their data via this... perhaps something like this:

protected IModel Model
{
    get { return model; }
}

然后子视图模型会像这样使用它:

Then the child view models would use it like this:

SomeCollectionProperty = Model.GetSomeData();

或者:

Model.SaveSomeData(SomeCollectionProperty);

为了进一步澄清,在这个阶段你对这个 Model 类有什么实现没有区别.只要实现了 GetSomeDataSaveSomeData 方法,视图模型并不关心它是使用数据库还是普通的旧文本文件.因此,这里最好使用接口,尤其是当您想要对视图模型进行任何测试时.

To clarify a little further, it makes no difference at this stage what implementation you have for this Model class. The view models don't care whether it is using a database, or a plain old text file as long as it implements the GetSomeData and SaveSomeData methods. Therefore, it is good to use an interface here, especially if you ever want to do any testing of the view models.

最后,您可能想看看我对 WPF 中 MVVM 的项目结构问题,以便更好地了解这一点.

Finally, you might want to take a look at my answer to the Project structure for MVVM in WPF question to get a better idea about that too.

这篇关于保存和加载数据“MVVM"大大地?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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