Skip to content

Instantly share code, notes, and snippets.

View alastairs's full-sized avatar

Alastair Smith alastairs

View GitHub Profile
@alastairs
alastairs / WhitespacePatternMatching1.cs
Last active January 18, 2022 16:21
Using C# pattern matching for string argument checks
// I think it would be nice to be able to use a `bool` Method Group in pattern matching statements, like
// string.IsNullOrWhitespace(). Here's an example.
public class Thing
{
private readonly IOptions<MyOptions> _options;
public Thing(IOptions<MyOptions> options)
{
_options = options switch
@alastairs
alastairs / gist:1142957
Created August 12, 2011 20:45
Leap Year kata
Write a function that returns true or false depending on whether its input integer is a leap year or not.
A leap year is divisible by 4, but is not otherwise divisible by 100 unless it is also divisible by 400.
2001 is a typical common year
1996 is a typical leap year
1900 is an atypical common year
2000 is an atypical leap year
@alastairs
alastairs / EqualityDemo.cs
Created April 1, 2020 08:14
Equality implementation in C#
namespace EqualityDemo
{
public class Person : IEquatable<Person>
{
public string GivenName { get; }
public string FamilyName { get; }
public Address Address { get; }
public override bool Equals(object other)
{
@alastairs
alastairs / keybase.md
Created August 30, 2019 16:06
keybase.md

Keybase proof

I hereby claim:

  • I am alastairs on github.
  • I am alastairs (https://keybase.io/alastairs) on keybase.
  • I have a public key ASA8efXB1aDLQ-vHTUfdKHZOxzIKQk1SK8EyJGJtHKxNpgo

To claim this, I am signing this object:

Letter no. 1 to Brexit Party MEPs for East of England, 2019-

Dear <name>

Congratulations on taking your seat in the eighth European Parliament. I hope you are delighted with your election and are looking forward to representing your constituents. I've been following the rise of the Brexit Party with interest over the last five months but am struggling to find detailed answers to much of your stated position on Brexit. I am sure you will want to set the highest standard of accountability and transparency in your new role -- putting the principles of Trust, Honesty and Integrity at the heart of

@alastairs
alastairs / Cambridge Software Crafters Email.md
Last active April 12, 2018 12:40
Email to the Cambridge Software Crafters re: name change and CoC

Changes to the Cambridge Software Craftsmanship Community

Hi everyone

It's not often that we email our members, so please bear with me and read this to the end.

Recent events in the wider tech community, and reactions within the Software Craftsmanship community, have prompted wider discussions about the inclusiveness of the community, and I've decided to make some small but important changes to our local chapter.

@alastairs
alastairs / NCrunchBugRepro.Test.deps.json
Last active May 27, 2017 01:40
.NET Core dependencies file for an NCrunch bug repro
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v1.1",
"signature": "284bcae32a73d805159b5f9db2a228e815fb23c1"
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v1.1": {
"ncrunchbugrepro.test/1.0.0": {
"dependencies": {
@alastairs
alastairs / JsonPropertyDisplayMetadataProvider.cs
Created May 16, 2017 20:27
Implementation of `IDisplayMetadataProvider` that reads the display name from `JsonProperty` attributes.
namespace Mvc.Examples
{
internal class JsonPropertyDisplayMetadataProvider : IDisplayMetadataProvider
{
public void CreateDisplayMetadata(DisplayMetadataProviderContext context)
{
var attributes = context.Attributes;
var jsonPropertyAttribute = attributes.OfType<JsonPropertyAttribute>().FirstOrDefault();
var displayMetadata = context.DisplayMetadata;
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata
{
/// <summary>
/// Provides <see cref="DisplayMetadata"/> for a <see cref="DefaultModelMetadata"/>.
/// </summary>
public interface IDisplayMetadataProvider : IMetadataDetailsProvider
{
@alastairs
alastairs / CreateRecordRequest.cs
Created May 16, 2017 19:40
Example Data Model/DTO in ASP.NET MVC
namespace Mvc.Examples
{
public class CreateRecordRequest
{
[JsonProperty("employee_id")]
[Required, DisplayName("employee_id")]
public string EmployeeId { get; set; }
[JsonProperty("source_id")]
[Required, DisplayName("source_id")]