Skip to content

Instantly share code, notes, and snippets.

@adamchester
adamchester / blog_debug_model_binding.aspnetmvc.debug.md
Last active March 28, 2023 15:03
Debugging ASP.NET MVC 4 model binding

The Scenario

Recently we had a tricky defect raised where, under certain circumstances, the user-entered values were not being persisted. The page was a somewhat complex data entry form, with many fields, lots of validation, and a complex hierarchy of view model objects and controls/helpers.

The process

After reproducing the issue (which itself was difficult), the first thing we did was to attach the debugger and set a break point on the first line of the controller action.

@adamchester
adamchester / states.fs
Last active July 4, 2018 00:25
Describing a state machine / approval workflow in F#
type State = Initial | Draft | PendingApproval | Approved | Cancelled | Completed
type Action = Create | Cancel | SendForApproval | Approve | Reject | Complete
type Role = Creator | Approver | Completor
type Transition = { Action:Action; From:State; To:State; Roles:Role list }
module Transitions =
let create = { Action=Create; From=Initial; To=Draft; Roles=[ Creator ] }
let cancel = { Action=Cancel; From=Draft; To=Cancelled; Roles=[ Creator ] }
let sendForAppr = { Action=SendForApproval; From=Draft; To=PendingApproval; Roles=[ Creator ] }
let approve = { Action=Approve; From=PendingApproval; To=Approved; Roles=[ Approver ] }
let reject = { Action=Reject; From=PendingApproval; To=Draft; Roles=[ Approver ] }
@adamchester
adamchester / LinqOfType_vs_ForListToArray.cs
Created June 18, 2016 05:40
Benchmark: Linq OfType<T>.ToArray() vs ForListToArray
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
namespace BenchmarkDotNet.Samples.Framework
{
public class Framework_LinqOfType_vs_ForListToArray
@adamchester
adamchester / csharp-linqpad-samples.cs
Last active May 27, 2016 23:20
MessageTemplates (C#) Samples
void Main() {
var fr = System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR");
var enAu = System.Globalization.CultureInfo.CreateSpecificCulture("en-AU");
var zhCn = System.Globalization.CultureInfo.CreateSpecificCulture("zh-CN");
var invariant = System.Globalization.CultureInfo.InstalledUICulture;
var chairSitTemplate = MessageTemplate.Parse("I sat at {@Chair} on {SitDate,10:dd-MMM-yyyy}");
chairSitTemplate.Format(fr, new Chair(), DateTimeOffset.UtcNow)
.Dump("Format using fr-FR culture uses comma decimal separator and french month names");
@adamchester
adamchester / netstandard.fs
Last active April 24, 2016 06:07
How .NET Standard relates to .NET Platforms (in F#)
// Converted to F# from https://gist.github.com/davidfowl/8939f305567e1755412d6dc0b8baf1b7
module Analogy =
type ASetOfApis = unit -> unit
// Each interface represents a target framework and methods represents groups of APIs available on that target framework.
// The goal is to show the relationship between .NET Standard API surface and other .NET platforms
// .NET Standard
@adamchester
adamchester / netstandard.fs
Created April 24, 2016 01:21
How .NET Standard relates to .NET Platforms (in F# functions)
type ASetOfApis = unit->unit
module ApiSets =
type Primatives = ASetOfApis
type Reflection = ASetOfApis
type Tasks = ASetOfApis
type Collections = ASetOfApis
type Linq = ASetOfApis
type ConcurrentCollections = ASetOfApis
@adamchester
adamchester / generate.fsx
Last active March 13, 2016 09:01
Generate a Visual Studio DGML Graph from F#
[<AutoOpen>]
module Workflow =
type State = Initial | Draft | PendingApproval | Approved | Cancelled | Completed
type Action = Create | Cancel | SendForApproval | Approve | Reject | Complete
type Role = Creator | Approver | Completor
type Transition = { Action:Action; From:State; To:State; Roles:Role list }
module Transitions =
let create = { Action=Create; From=Initial; To=Draft; Roles=[ Creator ] }
let cancel = { Action=Cancel; From=Draft; To=Cancelled; Roles=[ Creator ] }
let sendForAppr = { Action=SendForApproval; From=Draft; To=PendingApproval; Roles=[ Creator ] }
@adamchester
adamchester / NoSpec-report-github.md
Last active February 20, 2016 02:34
AutoFixture NoSpecimen Benchmark: NewInst vs. Singleton
BenchmarkDotNet-Dev=v0.9.1.0+
OS=Microsoft Windows NT 6.2.9200.0
Processor=Intel(R) Core(TM) i7-4980HQ CPU @ 2.80GHz, ProcessorCount=8
Frequency=10000000 ticks, Resolution=100.0000 ns, Timer=HPET
HostCLR=MS.NET 4.0.30319.42000, Arch=64-bit RELEASE [RyuJIT]
JitModules=clrjit-v4.6.1063.1

Type=NoSpec  Mode=Throughput  Platform=AnyCpu  
Framework=V45 TargetCount=10 
@adamchester
adamchester / suaveSerilog.fsx
Created June 8, 2015 11:15
A simple Suave to Serilog adapter
// Step 0. Boilerplate to get the paket.exe tool
open System
open System.IO
Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
if not (File.Exists "paket.exe") then
let url = "https://github.com/fsprojects/Paket/releases/download/0.31.5/paket.exe"
use wc = new Net.WebClient()
let tmp = Path.GetTempFileName()
@adamchester
adamchester / gist:8065483
Created December 21, 2013 04:39
AutoFixture specimen creation performance test
private class SpecimenWithEverything
{
public enum MyEnum
{
MyFirstValue,
MySecondValue,
MyThridValue,
}
public string String1 { get; set; }