When building client applications, there are different design patterns for separating the user interface, operations, and data models. For our game project, we will be using the Model-View-ViewModel (MVVM) pattern.
Model–view–viewmodel (MVVM) is a software design pattern that helps with the separation of the development of the graphical user interface (the view) from the development of the business logic or back-end data classes (the model), so that the view is not dependent on any specific model implementation. The view model is responsible for exposing the data objects from the model to make them easily presented. And the pattern usually uses databinding for connecting properties and event handlers to the view.

A ViewModel is just a C# class designed to manage how the Views and Models communicate with one another.
- We can use this class to manage multiple model classess. For example, when the player is in combat, we will need to manage a
Player
object and aMonster
object. - By having the extra class, we gain the ability to create automated tests. So, we won’t need to manually test our game through the user interface.
- This also gives us the ability to easily create a different UI – to convert the application to a mobile game or text/console game.
Create Initial GameSession class
Let’s follow the same process we did in lesson 2.1 to create the GameSession class:
- Right-click on the Game.Engine project, and add a new folder named “ViewModels”.
- Right-click on the ViewModels folder, and add a new class file named “GameSession.cs”.
The starting code for GameSession
will expose a CurrentPlayer
property so that views or other classes can access it. CurrentPlayer
is an instance of the Player
class we created earlier. Also the GameSession
class defines a constructor which creates a new instance of Player by calling new Player
.
using SimpleRPG.Game.Engine.Models;
namespace SimpleRPG.Game.Engine.ViewModels
{
public class GameSession
{
public Player CurrentPlayer { get; set; }
public GameSession()
{
this.CurrentPlayer = new Player
{
Name = "DarthPedro",
Gold = 1000
};
}
}
}
Notice the format used to initialize the newly created Player with a Name and Gold value. This is using C#’s object initialization syntax to simplify setting properties on a newly created object. This code is equivalent to creating the Player
object and setting properties with sequential statements:
public GameSession()
{
this.CurrentPlayer = new Player();
this.CurrentPlayer.Name = "DarthPedro";
this.CurrentPlayer.Gold = 1000;
}
The first block of code is short hand for this lengthier individual property setting. And it can be used to initialize any object that has public properties and also used for collection initialization.
In the source repository, I also created a simple GameSessionTest
to validate creating a GameSession
with the data we expect. You can review that if you’d like.
We now have 2/3rd’s of our MVVM pattern created. Let’s see how we bring it all together in our next lesson.