MVVM - 从多个表中加载分层数据 [英] MVVM - Loading Hierarchical Data from Multiple Tables

查看:65
本文介绍了MVVM - 从多个表中加载分层数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的基类:

I have a base class that looks something like this:

public class ModelBase : INotifyPropertyChanged { ... public string ID { get { return m_ID; } set { SetProperty(ref m_ID, value); }} public string ParentID { get { return m_ParentID; } set { SetProperty(ref m_ParentID, value); }}
public ObservableCollection<ModelBase> Children { get { return m_Children; }}

}

我有几种模型类型,每种模型类型都存储在自己的模型中表并有自己的数据访问层。这是一个GUI元素的示例,用于更好的"可视化"

I have several Model types and each model type is stored in its own table and has its own data access layer. Here is an example with GUI elements for better "visualization"

TextboxModel - > TextboxDAL - > TextboxTable

PictureModel - > PictureDAL - > PictureTable

GridModel - > GridDAL - > GridTable

DropboxModel - > DropboxDAL - > DropboxTable

等。

TextboxModel --> TextboxDAL --> TextboxTable
PictureModel --> PictureDAL --> PictureTable
GridModel --> GridDAL --> GridTable
DropboxModel --> DropboxDAL --> DropboxTable
etc.

当我启动应用程序时,我的MainViewModel通过适当的DAL在表之后加载表,返回模型。然后MainViewModel尝试(!)查找父模型并将项添加到Children属性(然后触发相应的
VM创建等)。我遇到的问题是父级可能还不存在,因为它还没有从数据库加载。

When I start the application, my MainViewModel loads table after table through the appropriate DAL which returns models. The MainViewModel then tries(!) to find the parent model and add the item to the Children property (which then triggers the appropriate VM to be created etc). The problem I have is that the parent might not exist yet because it hasn't been loaded from the database yet.

一个解决方案是每个元素查询每个表并搜索记录ParentID == this.ID.但这样效率非常低(我不使用SQL,而是使用一些专有的存储库,我不能改变添加表和读/写
)。

One solution would be that each element queries each table and searches for records where the ParentID == this.ID. But that would be very inefficient (I am not using SQL but some proprietary repository that I cannot change short of adding tables and read/write to).

另一个解决方案是首先从所有表中读取所有记录但不尝试将任何模型添加到另一个模型子属性。在加载完所有内容后,我会弄清楚层次结构是什么,然后才开始向Children
属性添加模型。

Another solution would be to first read all records form all tables but not trying to add any model to another models Children property. After everything is loaded I then figure out what the hierarchy is and only then start adding models to the Children property.

第二种方法是我喜欢的但仍然感觉笨重。有没有更好的方法呢?

The second approach is what I am favoring but it still feels clunky. Is there a better way to do this?




推荐答案

<你喜欢hulamula,

Hi hulamula,

>>第二种方法是我喜欢的,但它仍然感觉很笨重。有没有更好的方法呢?

>>The second approach is what I am favoring but it still feels clunky. Is there a better way to do this?

如果记录不是很大,比如五万或更多,我建议您可以使用第二种方法,将所有记录加载到内存中。然后我们可以通过内存过滤记录,它会提高性能。 

If the records is not huge, such as fifty thousand or more, I would suggest that you could use the second approach, which load all the records into memory. then we could filter the records via memory, it will improve performance. 

祝你好运,

张龙


这篇关于MVVM - 从多个表中加载分层数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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