Lesson 03.4: Creating the GameSession (ViewModel) Class

In this lesson, we will create a ViewModel. This is a class that will manage communication between the View (MainWindow.xaml) and the Models (currently, only Player.cs).

Using a ViewModel makes it easier to create automated tests (which we will do later), or create a different type of user interface (such as a web browser-based version of the game).




  • A ViewModel is designed to manage how the Views and Models communicate with each other.
    • We can use this class to manage different models. For example, when the player is in combat, we will need to manage a Player object and a Monster object.
    • By having the extra class, we gain the ability to create automated tests. So, we won’t need to manually (and slowly) test our program.
    • This also gives us the ability to easily create a different UI – to convert the program to a web game, or text game.
  • In the Player class, the property datatypes are “string” and “int”.
    • In this model class, we will use Player as the datatype for the CurrentPlayer property – because that property will hold a Player object.
    • If a class uses a class in a different namespace, you need to include a “using” statement at the top of the class, so it can “use” the classes in the other namespace.
  • When you need an object, you call the constructor on a class.
    • This is like “filling in” the “blank form” of the class.
    • If we don’t define a constructor, the class still automatically has a default constructor.
    • We can use the constructor to perform additional steps, when the class is created. For example, setting property values.
  • “Scope” is the visibility level of a class, property, or variable.
    • For now, we will use “public” – the most-visible scope. In the future, we will reduce the scope of some properties.
  • A single equal sign “=” is used when you want to assign a value to a variable, property, object, etc.
    • The value on the right of the equal sign is evaluated, and assigned to the variable on the left of the equal sign.


Source Code

In the Engine project, right-click on “Engine”, and add a new folder named “ViewModels”.

Right-click on the ViewModels folder, and add a new class named “GameSession.cs”.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Engine.Models;

namespace Engine.ViewModels
    class GameSession
        Player CurrentPlayer { get; set; }

        public GameSession()
            CurrentPlayer = new Player();
            CurrentPlayer.Name = "Scott";
            CurrentPlayer.Gold = 100000;


Modify Player.cs, to look like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Engine.Models
    public class Player
        public string Name { get; set; }
        public string CharacterClass { get; set; }
        public int HitPoints { get; set; }
        public int ExperiencePoints { get; set; }
        public int Level { get; set; }
        public int Gold { get; set; }


Return to main page

3 thoughts on “Lesson 03.4: Creating the GameSession (ViewModel) Class

  1. Hi Scott, thank you so much for your videos. I’ve been watching other WPF MVVM videos and they implement ViewModels in the WPF project whereas you implement the folder in Engine class library. Is there any advantage to either method?

    1. You’re welcome, Paul!

      I prefer to implement the ViewModel in the Engine class so I can use it like a “public API” that exposes the logic in the rest of the code. This way, if I create a unit test project, the unit tests can instantiate a ViewModel object, call the functions (like calling an API), and check that the expected changes happened. You can also write a web or console UI that uses the existing ViewModel (although you’ll have to do some changes for a web UI, to maintain state). If the ViewModel is in the View project, and you want to create a different type of front-end, then you’d need to recreate/repeat the ViewModel logic for the new UI project.

      However, it isn’t very often that someone suddenly decides to create a second type of UI for an existing program. So, it usually won’t matter which way you choose.

Leave a Reply

Your email address will not be published. Required fields are marked *