CorellianAle
CorellianAle

Reputation: 685

How to update model and view model in MVVM pattern?

I am struggling to implement MVVM pattern in my current project.

"ClassA" continuously gets required data from a remote device and stores this data inside it's fields. It is a model, I guess. ClassA updates required information via Update method.

"ClassB" continuously gets the data from "ClassA" and stores it in corresponding properties. Looks like it is a view model.

View is a simple MainWindow.xaml with a DataGrid inside.

I have the following questions:

1) How do I update ViewModel? Should ClassB have an Update method, which accepts an instance of ClassA and updates corresponding fields?

2) Where do I store an instance of ClassA? Should ClassA be a field of ClassB? If it should, then how do I update Model? I thought of something like the following:

public void UpdateB()
{
    ClassA.UpdateA();
    this.FieldOne = ClassA.FieldOne;
    this.FieldTwo = ClassA.FieldTwo;
}

4) Does model have it's update method at all or model just stores the data?

3) What do I do inside MainWindow.cs, aside from windows initialization? Do I update view model (ClassB) there?

Upvotes: 3

Views: 4347

Answers (2)

user3096803
user3096803

Reputation:

I find it best to have a object representing an item in each layer of abstraction. This includes the form of the data as it exists on the disk. Remember that in MVVM, the only real goal is to promote loose coupling between the interface(User Interface) and the implementation(ViewModel functionality).

For example, if I have objects stored in XML files, I will have an object in my data access layer that exists only for the proper management of the XML data. Let's call it ObjectXml. This object only contains data in the form that is native to the data on the disk. In this case, all data has a string representation, as in the XML files.

In the model layer, you will have the data representation of the XML file in the expected data types. Let's call this Object. The property getters and setters may access and set the string representation of the data by performing conversions in both directions. This way, the data is ready to be persisted to the data source(xml file, database etc.).

In ObjectViewModel, properties may access those in Object. The viewmodel contains all the members for representing and modifying the model.

Note that ObjectXml is really only beneficial when you are only allowed to store string information, or when a suitable schema does not exist for your data types.

At the end, you have a hierarchy of containment such as the one below:

public class ObjectXml
{
    [XmlArray("People"), XmlArrayItem("Person")]
    public List<PersonXml> People { get; set; }
    //PersonXml is an xml data model similar to this one

    [XmlElement("Item")]
    public string Items { get; set; }
}

Here is the model for the Xml object:

public class Object
{
    private ObjectXml _xmlContext;

    public Object(ObjectXml xmlContext)
    {
        this._xmlContext = xmlContext;
    }

    public List<Person> People
    {
        get
        {
            //Person requires a constructor that takes a PersonXml object in order for this to work properly
            return this._xmlContext.People.Select(x => new Person(x)).ToList();
        }
        set
        {
            this._xmlContext.People = value.Select(x => new PersonXml(x)).ToList();
        }
    }

    public double Item
    {
        get { return double.Parse(this._xmlContext.Item); }
        set { this._xmlContext.Item = value.ToString(); }
    }
}

Obviously, it's not wise to name your class Object as it's a reserved word in C#. Hopefully I've given you some ideas of how to access and update data in a robust and extensible manner.

In short, you don't need an update method at all. Also, short of constants and property backing fields, there are very few reasons to need direct field access in C# MVVM.

  1. See below. Do not listen to people that say the ViewModel and Model need to be decoupled. The main purpose of the model is an intermediary layer that prepares data to be saved or loaded into the program and to store data in a way that is agnostic to both the data and the program functionality(ViewModel)
  2. You do not need an update method. Use properties that access the data model and persist to the data storage(xml, database etc.) if needed.
  3. You do not need an update method.
  4. You should not have to do anything inside of ViewModel.cs. Only code that modifies the view should be in the codebehind. The only ViewModel you should ever access in a view is one that follows the form of MainWindowViewModel, which is more like an ApplicationViewModel that carries instances of other required viewmodels.

Finally, don't get stuck using an overcomplicated MVVM "framework" as most of the functionality is not useful or necessary.

Upvotes: 1

H W
H W

Reputation: 2586

Like stated in Yuris comment, you should not use any update method, but rather implement the INotifyPropertyChanged interface. Like the name says this notifies all subscribers when the value of a certain Property changed.

This is a nice article which contains code to a minimalistic MVVM implementation. If you have trouble implementing the pattern from scratch, try to start with this example and replace the existing classes with your own one-by-one.

As to the update mechanic inside your MainWindow.cs - you don't need any, if you specify the DataBinding in your xaml code like it is done in the example linked above.

I hope this helps you getting started!

Upvotes: 0

Related Questions