多个 ViewModel 修改同一个域对象
对不起我的英语。
我之前发布过这个问题: 多个 ViewModel 请求相同的域对象,我应该给它们相同/不同的实例吗?
在我对此进行了一些研究之后,我意识到我之前的问题太混乱了,所以我发布了这个新问题。
假设我想构建一个 WPF 桌面应用程序来管理 10 亿人的信息。它只有一个最终用户(我),所有数据都存储在我的本地计算机(SQL Server / MySQL)中。我将使用 Visual Studio、Autofac、NHibernate,并尝试实现 MVVM、DI、DDD 模式。
同一个人可以同时显示在 UI 中的任何位置:多个窗口、选项卡、列表、组合框、文本块...每个列表可以包含几千人,整个应用程序可以有 10~100 个列表。而且列表的内容(人员)经常更新:每次更改列表的设置时,列表都会被清空,然后又填充数千个新人员。
问题是:如果我修改一个列表中的某个人(例如更改名称),如何将更改应用到所有其他列表?
我认为有两种方法可以解决此问题:
对于每个人,在整个应用程序中使用单个实例。为了实现这一点,我可以在应用程序中使用单个 UnitOfWork,但这种方法似乎会导致内存泄漏。
为每个人使用多个实例(每个列表一个实例)。为了实现这一点,我认为我应该为每个列表使用一个 UnitOfWork,并且我必须找到一种方法来同步一个人的所有实例。 Berryl< /a> 提到的事件聚合器。但是谁负责发布和注册事件?
我在此处发现了一个非常类似的问题,并阅读了以下内容文章,但我仍然感到困惑:
谢谢!
sorry for my English.
I previously posted this question as: Multiple ViewModels request same domain object, should I give them the same/different instance(s)?
After I did some research on this, I realized my previous question was too confusing, so I post this new question.
Let's say I want to build a WPF desktop application to manage the information of 1 billion persons. It has only one end user (me), and all data is stored in my local machine (SQL Server / MySQL). I'm going to use Visual Studio, Autofac, NHibernate, and try to implement MVVM, DI, DDD patterns.
The same person could simultaneously displayed anywhere in the UI: in multiple windows, tabs, lists, comboboxes, textblocks... Each list could contain a few thousands persons, and the whole application could have 10~100 lists. And the contents (persons) of the lists get updated frequently: Each time I change the settings of a list, the list gets emptied, then filled with another several thousands of new persons.
The problem is: If I modify a person (eg. change name) in one list, how to apply the changes to all other lists?
I think there are 2 ways to solve this problem:
For each person, use a single instance through the entire application. To implement this, I can use a single UnitOfWork through the application, but it seems this approach would cause memory leak.
Use multiple instances for each person (one instance per list). To implement this, I think I should use one UnitOfWork for each list, and I must find a way to synchronize all instances of a person. Berryl mentioned event aggregator. But who is responsible to publish and register the events?
I found a very similar question at here, and also read the following articles, but I still feel confused:
Thanks!
不要跨会话共享实体实例,并且永远不要将单个会话用于桌面应用程序。
由于 ViewModel 通常是单独业务对话的一部分,因此每个虚拟机可能有一个会话。
每个 ViewModel 应该注册它接受的事件,并负责发布它生成的事件。另一种方法是在服务/业务层级别进行发布。您需要了解什么最适合您的架构。
Do not share entity instances across sessions, and NEVER use a single session for a desktop app.
Since usually ViewModels are part of separate business conversations, you are likely to have one session per VM.
Each ViewModel should register the events it accepts, and it's responsible for publishing the ones it generates. An alternative is publishing at the service/business layer level. You'll need to see what fits best your architecture.