Multiple ViewModels modify the same domain object

2019-08-27 16:41发布

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!

1条回答
甜甜的少女心
2楼-- · 2019-08-27 16:55

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.

查看更多
登录 后发表回答