多个ViewModel修改相同的域对象 [英] Multiple ViewModels modify the same domain object

查看:238
本文介绍了多个ViewModel修改相同的域对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我以前发布这个问题为:多个ViewModel请求相同的域对象,我应该给他们相同/不同的实例吗?对于这个我做了一些研究之后,我意识到我之前的问题太混乱了,所以我发表了这个新问题。

$ b 比方说,我想建立一个WPF桌面应用程序来管理10亿人的信息。它只有一个最终用户(我),所有的数据都存储在我的本地机器(SQL Server / MySQL)中。我将使用Visual Studio,Autofac,NHibernate,并尝试实现MVVM,DI,DDD模式。

同一个人可以同时在UI中的任何位置显示:在多个窗口,选项卡,列表,组合框,文本块...每个列表可能包含几千人,整个应用程序可能有10〜100个列表。而且这些列表的内容(人物)会经常更新:每次我改变一个列表的设置时,列表都会被清空,然后被另外几千个新人填满

b
$ b

问题是:如果我在一个列表中修改一个人(例如更改名称),如何将更改应用于所有其他列表?



我认为有两种方法可以解决这个问题:


  1. ,通过整个应用程序使用单个实例。为了实现这一点,我可以通过应用程序使用单个UnitOfWork,但似乎这种方法会导致内存泄漏。

  2. 为每个人使用多个实例每个列表的实例)。为了实现这一点,我想我应该为每个列表使用一个UnitOfWork,并且我必须找到一种方法来同步一个人的所有实例。 Berryl 提到的事件聚合器。但是谁负责发布和注册这些事件?

  3. 我在这里,也看了下面的文章,但是我仍然感到困惑:





    谢谢!

    解决方案

    不要在会话中共享实体实例,也不要为桌面应用程序使用单个会话。

    由于通常 ViewModels是单独业务对话的一部分,因此每个虚拟机可能有一个会话。

    每个ViewModel s应该注册接受的事件,并负责发布它所生成的事件。另一种方法是在服务/业务层级上发布。你需要看看最适合你的体系结构。


    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:

    1. 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.

    2. 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!

    解决方案

    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.

    这篇关于多个ViewModel修改相同的域对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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