Skip to content

Instantly share code, notes, and snippets.

View mikehadlow's full-sized avatar

Mike Hadlow mikehadlow

View GitHub Profile
@mikehadlow
mikehadlow / result.ts
Last active May 1, 2023 22:52
Simple monadic result type in Typescript
// Result
type Success<S> = { success: true, value: S }
type Failure<F> = { success: false, message: F }
type Bindable<S, F> = {
bind: <B,>(func: (input: S) => Result<B, F>) => Result<B, F>
}
type Result<S, F> = (Success<S> | Failure<F>) & Bindable<S, F>
@mikehadlow
mikehadlow / Program.cs
Created April 28, 2022 16:10
Attach Event Handlers By Reflection
namespace EventHandlersByReflection;
using System.Reflection;
using static System.Console;
public class Program
{
public static void Main()
{
var thing = new ThingWithEvents();
@mikehadlow
mikehadlow / Topological.cs
Last active February 5, 2021 10:53
Topological Sort
using System;
using System.Collections.Generic;
using System.Linq;
namespace Trading.Backend.Events.Contracts.Tests
{
public static class TopologicalTest
{
public static void ShouldCorrectlyOrderNodes()
{
@mikehadlow
mikehadlow / SimpleConsoleLoop.cs
Created January 25, 2021 16:38
A simple framework for a console application periodic loop.
using System;
using System.Threading;
using System.Threading.Tasks;
using static System.Console;
namespace SimpleConsoleLoop
{
class Program
{
static Task Main()
@mikehadlow
mikehadlow / EasyNetQMicrosoftExtensionsLogProvider.cs
Created January 4, 2021 15:25
Register a Microsoft.Extensions.Logging.ILoggerProvider with EasyNetQ
using System;
using EasyNetQ.Logging;
using Microsoft.Extensions.Logging;
// use like this...
// LogProvider.SetCurrentLogProvider(new EasyNetQMicrosoftExtensionsLogProvider(loggerProvider));
// RabbitHutch.CreateBus(..);
namespace Logging
{
@mikehadlow
mikehadlow / OptionsMonitor.cs
Last active January 4, 2021 15:17
How to create a ConsoleLoggerProvider without the full hosting framework.
using Microsoft.Extensions.Logging.Console;
using Microsoft.Extensions.Options;
using System;
namespace Sample
{
class Program
{
static void Main()
{
@mikehadlow
mikehadlow / ServiceCollectionWriter.cs
Created December 1, 2020 10:30
Microsoft.Extensions.DependencyInjection object graph writer.
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace MyNamespace
{
public class ServiceCollectionWriter
{
private XmlElement FaultFromException(Exception exception)
=> (exception is TargetInvocationException targetInvocationException)
? FaultFromException(targetInvocationException.InnerException)
: (exception is SonosException sonosException)
? XmlElementFromFault(new SoapFault
{
FaultCode = sonosException.FaultCode,
FaultString = sonosException.FaultString,
Detail = sonosException.Detail
})
{
"albums": [
{
"id": "1234",
"price": {
"amount": 12,
"currency": "EUR",
"vatAmount": 2.4
}
}
using System;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace MyProject.Tests
{
public class AsyncNamingConventionTests
{
[Fact]