0

I'm familiar with MVVM and differences between models, viewmodels and views. The only thing that I'm not able to find answer to is how to update models at runtime. Simple example to show you what I mean:

Let's say I have application which can display graphs and store them in a database.

I have models

public class Session {
    public Document Doc { get; set; }
}
public class Document {
    public string Name { get; set; }
    public Point[] GraphPoints { get; set; }
}

I can connect those to their viewmodels by passing them as parameters, so:

public class SessionViewModel{
    private readonly Session _session;
    public SessionViewModel(Session session)
    {
        this._session = session;
    }
}
public class DocumentViewModel{
    private readonly Document_document;
    public SessionViewModel(Document document)
    {
        this._document = document;
    }
}

public class ShellViewModel {

    public SessionViewModel SessionVm { get; set; } // <-- Bind in view
    public DocumentViewModel DocumentVm { get; set; } // <-- Bind in view
    private Session _session;

    public ShellViewModel()
    {
        _session = new Session();
        session.Doc = new Document();

        SessionVm = new SessionViewModel(session);
        DocumentVm = new DocumentViewModel(session.Doc);
    }
}

Problem appears when in the middle of my application's life cycle I decide to change value of document. For example:

public void OnNewDocumentLoaded(Document newDoc)
{
     _session.Doc = newDoc;
}

_session.Doc was changed but every DocumentViewModel has its own instance of document which is passed in a constructor, so even though I changed model, my viewmodel stays the same.

Also I don't want to use INotifyPropertyChanged inside my model, because models should not know about framework and from my understanding this is a bad approach. Also I keep my models in PCL project so I'm not even able to implement INotifyPropertyChanged in my models.

FCin
  • 3,804
  • 4
  • 20
  • 49
  • 2
    I have models implement INotifyPropertyChanged, and it seems pretty typical. See http://stackoverflow.com/questions/6922130/in-mvvm-model-should-the-model-implement-inotifypropertychanged-interface. I wrap my POCOs and data objects in a model that manages dirty/save/load and notifies the VM of changes. – Joe Aug 15 '16 at 19:20
  • Like this: http://blog2.rees.biz/_/rsrc/1271023681400/Home/mvvmpattern/wpflobmvvm-thumb1.png – Joe Aug 15 '16 at 19:22
  • I think this is a bad practice, because let's say you want to change wpf to something else. You would have to modify your models. Also as I said I store my models in PCL project that is platform independent. – FCin Aug 15 '16 at 19:25
  • A ton of times, I will pass the model via the constructor like you are doing, but I will implement a few properties from the model in the viewmodel pointing back to the model. This way I know what I should have in the GUI and I know that the model will always be updated. Food for thought.... – Kevin B Burns Aug 15 '16 at 19:26
  • @KevinBBurns I think you are right. I don't know why, but I thought for a second that passing parameter via constructor and assigning it to a new variable makes a new instance of the parameter, but it actually acts as a pointer. Thank you. I think I need more sleep :) – FCin Aug 15 '16 at 19:34

1 Answers1

0

From my understanding of a MVVM approach, models should not have a viewmodel associated with them. Instead, your views should have a viewmodel associated to them. Inside your viewmodel you can have objects from models in your application. Inside your viewmodel is where you should implement INotifyPropertyChanged. Those methods control the objects you have changed and then binding can occur between your view and viewmodel.

Jared
  • 394
  • 4
  • 15
  • What do you mean that "models should not have a viewmodel associated with them"? I thought that every model must have a corresponding viewmodel and view. Also how could I change models and viewmodels? – FCin Aug 15 '16 at 19:24