.NET MVC routing on legacy ASP pages

When working on legacy ASP websites often you have to work with a directory full of .aspx files: about.aspx login.aspx logout.aspx shopping.aspx … With some architectures, the code behind calls into a front controller as follows: Protected Overrides Sub OnLoad(ByVal eventArgs As System.EventArgs) MyBase.OnLoad(eventArgs) Dim frontController As New LegacyFrontController frontController.LoadPage("shopping.aspx") End Sub Thanks to MVC route controllers, we can refactor such websites and reduce the amount of .aspx files. [Read More]

Single return versus multiple return

Imagine the maitre d’ at a restaurant. When a diner enters the restaurant the maitre d’ has to perform a number of checks to determine whether the diner is allowed a table. When we take this real world scenario and convert it into code, we end up writing a validation function. If the rules are: Check the diner is old enough Check the booking exists Check the booking has not expired Check there is a table available Then we can write the code as follows: [Read More]

Feature picks from C# 6

These are my feature picks from the upcoming C# 6: New nameof operator The nameof operator provides the name of the target variable. We can use it to prevent debugging messages becoming inconsistent after refactoring variable names. if (!_Validate(name)) { throw new ArgumentException(nameof name + " is not valid"); // throws "name is not valid" } Primary constructors Primary constructors allow us to initialise fields and properties without a constructor. [Read More]

Immutable C# Structs

This is a basic immutable struct in C#. public struct PuppyDogPair { public PuppyDogPair(Guid puppyId, Guid dogId) : this() { Puppy = puppyId; Dog = dogId; } public Guid Puppy { get; private set; } public Guid Dog { get; private set; } } If your struct is not immutable, then you will have to pass it by reference if you want to mutate it inside helper methods. private static void PerformAction(ref Config config) { // do something with config config. [Read More]