Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
665 views
in Technique[技术] by (71.8m points)

nhibernate - Multiple ViewModels modify the same domain object

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...