Press "Enter" to skip to content

Author: Scott

Automate your boilerplate classes in Visual Studio

I’m working on a program where I’ll need to create 25-50 classes that do the same type of thing and have the same basic structure. Instead of copy-pasting-editing for each class, I’m going to save time and reduce errors by creating a class template for Visual Studio to use for these classes. Example source code description The project is a game that connects to Twitch IRC chat. People in chat type a command, the program recognizes it, and then does the required action. This is the Command Design Pattern, which you can read more about here. Without a custom class…

Leave a Comment

Replace “switch” statements with dictionaries

Long functions can make code difficult to read. We have to use mental energy to remember what was done in the code above the line we’re looking at. Replacing long “switch” statements with a dictionary may make your code shorter and easier to work with. In the sample code, we’re going to handle keyboard input (the WASD keys) changing the location of something on the screen. We’ll use this simple Location class with XPosition and YPosition properties. With the switch statement version of the code, in the window’s constructor, we initialize a Location object and store it to a class-level…

Leave a Comment

Make your code easier to read with “early exits”

One technique to make your code easier to read, and work with, is to use “early exits” in your functions. Here is how I used early exits to clean up a function I wrote for a game. Let’s say we have an online game and want the players to be able to change their character’s name. Here’s the code for our simple Player class, with an Id and Name property. The function to rename the character needs to do several checks to prevent the user from renaming to a bad name, for example: an empty string, a name that another…

Leave a Comment

How to create classes from JSON data files in C#

If you have a JSON data file and would like to quickly build classes you can deserialize the data into, Visual Studio has a simple way for you to do this – without manually creating the properties. Let’s say we want to build a class to hold the data in this config.json file: Create a new class. In this example, I’ll name the class ConfigValues.cs. Delete everything from the file, except for the namespace declaration. Highlight all the data in the JSON file and copy it to the clipboard (Ctrl-C). Go back into the empty class file (ConfigValues.cs) and put…

Leave a Comment

How to implement the Decorator design pattern in C#

What is the Decorator Design Pattern? The Decorator design pattern is a structural pattern that lets you add new behavior to a class by “wrapping” it inside another class. This pattern is sometimes called the Wrapper Design Pattern. You may use this pattern in Domain-Driven Design, where something like an Order class may have a different meaning to the different departments of a company – and require different properties. Sample code description This code will be for a pizza restaurant. People can order a pizza to eat in the dining room, for picking up, or for delivery. For each of…

Leave a Comment

How to implement the Adapter design pattern in C#

What is the Adapter Design Pattern? The Adapter design pattern is a structural pattern, used to allow objects of one type to be used in an application or function that expects objects of a different type. You may use this pattern when your program needs to submit data to a web service or API, which is expecting the data as a different datatype. In this example, we’ll simulate a program that creates an Order object (with Customer and OrderItem objects in its properties) and needs to submit a shipping request to a different program, that expects objects of a different…

Leave a Comment

How to implement the Publish/Subscribe design pattern in C#

What is the Publish/Subscribe Design Pattern? The Publish/Subscribe Design Pattern is a technique for one or more objects to know when something happens to another object – without them needing to continually inspect the object. In this example, I’ll show a non-pattern way to monitor an object, a way using the publish/subscribe pattern, and an enhanced way to pass additional information when “publishing”. The sample code will be for a game. If the player moves to a location that is poisonous, they take damage. If the player’s hit points drop to zero or less, the game wants to know, so…

Leave a Comment

How to implement the Data Mapper design pattern in C#

What is the Data Mapper Design Pattern? The Data Mapper design pattern is a technique to save data from in-memory objects to the database, and load data from the database into in-memory objects. This pattern has the code for saving and loading inside a separate class from the model class. The Active Record Design Pattern is a similar pattern but moves the code for database saving and loading inside the Model class. Example C# source code for the Data Mapper design pattern For this example, we’ll use this simple Customer class as the Model. It only has properties. It could…

Leave a Comment

How to implement the Active Record design pattern in C#

What is the Active Record Design Pattern? The Active Record design pattern is a technique to save data from in-memory objects to the database, and load data from the database into in-memory objects. This pattern includes the code for saving and loading inside the model class. The Data Mapper Design Pattern is a similar pattern but moves the saving and loading code out of the model, into a separate class. Example C# source code for the Active Record design pattern Here, we have a typical Customer class. On lines 8 and 9, we have the connection string for the database.…

Leave a Comment

How to implement the Memento Design Pattern in C#

What is the Memento Design Pattern? The Memento Design Pattern is a behavioral pattern, used to save an object’s state (its property values) to allow for restoring the object’s properties to their previous values (rollback or undo changes). There are three components for the Memento Design Pattern: the Originator (class to maintain state history for), the Memento (holds a “snapshot” of the state), and the Caretaker (manages saving and restoring mementos). Code Sample for Memento Design Pattern in C# Here’s how to use a memento to track multiple snapshots of a Customer object’s property values. In this example, I’ll have the…

Leave a Comment