Press "Enter" to skip to content

Lesson 19.4: Replace BaseNotificationClass with Fody PropertyChanged Notification

In this lesson, we’re going to get rid of our base class that handles PropertyChanged notifications. We’ll replace it with a NuGet package that automatically handles these notifications for all properties.

 

This change eliminates something I didn’t really like about the BaseNotificationClass.

Normally, inheritance is used to create child classes that are more-specific versions of the parent class. For example, the Player and Monster classes are more-specific versions of LivingEntity.

The model and viewmodel classes aren’t more-specific versions of BaseNotificationClass. They’re just classes where we want to implement this common property changed notification behavior. So, getting rid of BaseNotificationClass seems cleaner to me.

 

A big problem in programming is that many programmers suffer from Not-Invented-Here Syndrome – a refusal to use well-written and well-supported libraries.

Many of us like to solve the puzzles around programming and think the best decision is to always write our own code. I once worked on a project where a programmer spent a month writing a function that could have literally been done with two lines of code using a library that the project was already using. The worst part was that the hand-written code had several bugs.

So, instead of continuing to use our BaseNotificationClass, we’re going to use the Fody code weaver NuGet package. This package lets us add features that automatically are added when we compile our code. The most popular feature is to automatically raise PropertyChanged notifications.

 


Link to video on YouTube

 

Step 1: Add Fody NuGet packages

In the Engine, SOSCSRPG.Models, and SOSCSRPG.ViewModels projects, add these two NuGet packages:

  1. Fody
  2. PropertyChanged.Fody

 

Step 2: Create FodyWeavers.xml files

In the Engine, SOSCSRPG.Models, and SOSCSRPG.ViewModels projects, create a FodyWeavers.xml file with the contents below:

<?xml version=”1.0″ encoding=”utf-8″?>
<Weavers xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”FodyWeavers.xsd”>
<PropertyChanged />
</Weavers>

 

Step 3: Delete Engine\BaseNotificationClass.cs

 

Step 4: Fix classes that were using BaseNotificationClass

In the classes that were inheriting from BaseNotificationClass, make the following changes:

  1. Add “using System.ComponentModel;” to the using directives
  2. Replace BaseNotificationClass inheritance with INotifyPropertyChanged implementation
  3. Add “public event PropertyChangedEventHandler PropertyChanged;”
  4. Delete all calls to OnPropertyChanged from the setters

 

The files to change are:

Engine\Models\GroupedInventoryItem

Engine\Models\LivingEntity

Engine\Models\PlayerAttribute

Engine\Models\QuestStatus

Engine\ViewModels\CharacterCreationViewModel

Engine\ViewModels\GameSession

 

Engine\Models\Player only needs to have the call to OnPropertyChanged removed, since it’s getting the other behavior by inheriting from the LivingEntity class

 

Step 5: Test the game

Leave a Reply

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