4

I'm a bit new to MVVM, but here's my dilemma:

  1. I have a model (or models, but let's keep it simple)
  2. I want to show that model data on multiple different views.
  3. Ok, so ViewModel for each view gets created
  4. UserControls get created (my views)
  5. each Usercontrol gets a ViewModel

Here's my question: What/where/how do I handle propagating this model to each viewmodel?

This is desktop dev, not web dev, so I'm getting stuck on the idea of making sure that the model is in each viewmodel. Normally I handle this by having a controller/parent class propagate down the information to each ViewModel, but I'm starting to get into a more complex application where it's not one model that I need to propagate, but multiple models and viewmodels that might need to call services that would need that info as well, and I'm getting a bit confused as to how exactly the best practice is to proceed.

Just to be thorough, here's a quick example:

View1, View2, View3, and View4 all exist.

View1 and View2 need models M1, M2, and M3 View3 needs models M1, M3, and M4 View4 needs models M2, M3, M4, and M5

Each view now has it's own corresponding ViewModel, VM1-4.

How do I create and propagate M1-5 to the corresponding VMs?

3
  • 1
    Entity Framework does this with a DataContext object, which essentially contains all of the models. This is going to depend heavily on what your data access strategy looks like. Do you have a data access strategy? Oct 24, 2018 at 19:47
  • How your navigation works? What do you mean by model? Do you display this views simultaneously and letting them interact? I've probably would handle this by navigation service which would call Setup method on ViewModel with your objects you want to pass.
    – Szel
    Oct 31, 2018 at 14:45
  • You can think of each of the separate views as UserControls on one main window. A usercontrol on the right side of the screen might mimic what a usercontrol on the left side of the screen is hsowing. Or more likely, a separate window might contain settings information that will effect what's being shown on the usercontrols. Looking for a good way to manage everything. Models are models. They come from a database.
    – oppassum
    Oct 31, 2018 at 16:24

2 Answers 2

1

I would suggest that you consider using an event aggregator pattern to achieve this.

An Event Aggregator acts as a single source of events for many objects. It registers for all the events of the many objects allowing clients to register with just the aggregator.

...

Event Aggregator is a good choice when you have lots of objects that are potential event sources. Rather than have the observer deal with registering with them all, you can centralize the registration logic to the Event Aggregator. As well as simplifying registration, a Event Aggregator also simplifies the memory management issues in using observers.

1
  • If you are using Windows Presentation Foundation (WPF) this can be found in the Caliburn.Micro framwork. Jul 18, 2019 at 7:07
0

"Dependency Injection" and "Inversion of Control" come into my mind. That is, you have some "container" which is responsible for creating the instances and injecting the dependencies.

In your case, you must make sure that only one instance per model is created. That can often be achieved by a "named instance".

In a "poor man's" scenario, you could do:

M1 m1 = new M1();
M2 m2 = new M2();
...
V1 v1 = new V1(m1, m2, m3);
... etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.