Skip to content

Instantly share code, notes, and snippets.

@mrpmorris
mrpmorris / ConditionNotBeingHonoured.cs
Created May 23, 2018 10:42
ResolveUsing is still executed when Condition fails
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AutoMapperCondition
{
class Program
@mrpmorris
mrpmorris / gist:963a081fe9ab31b00c3ccfa01f951f49
Created January 22, 2022 13:16
Task<T>.WithCancellationToken extension for async methods that do not accept a CancellationToken parameter
public static class TaskExtensions
{
public static async Task<T?> WithCancellationToken<T>(this Task<T> source, CancellationToken cancellationToken)
{
var cancellationTask = new TaskCompletionSource<bool>();
cancellationToken.Register(() => cancellationTask.SetCanceled());
_ = await Task.WhenAny(source, cancellationTask.Task);
if (cancellationToken.IsCancellationRequested)
@mrpmorris
mrpmorris / Program.cs
Last active January 22, 2022 16:30
An HTTP server accepting WebSockets
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace ConsoleApp15;
public static class Program
{
public static async Task Main(string[] args)
{
IHostBuilder hostBuilder = Host.CreateDefaultBuilder(args)
@mrpmorris
mrpmorris / MyDbContext.cs
Created February 17, 2022 09:39
Example of how to avoid multiple-thread access to DbContext in Blazor-Server
Instead of this
return Db.Users.FirstOrDefaultAsync(x => x.EmailAddress == emailAddress);
do this
return Db.InvokeAsync(() => ...the query above...);
// Add the following methods to your DbContext
@mrpmorris
mrpmorris / FluxorStatePattern-InterfaceExample.cs
Last active March 31, 2022 14:48
State patterns in Fluxor
using Fluxor;
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection();
services.AddFluxor(x =>
{
x.ScanAssemblies(typeof(Program).Assembly);
// CON: Need to explicitly register a reducer type for each feature state
x.ScanTypes(
typeof(ChangeCommonStateReducers<State1>), // Register this generic class
using Fluxor;
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection();
services.AddFluxor(x =>
{
x.ScanAssemblies(typeof(Program).Assembly);
// PRO: No need to register lots of generic classes here
});
@mrpmorris
mrpmorris / Benchmarks.cs
Created April 23, 2022 21:45
An immutable array class that uses multi-levels of arrays to speed up copying
// Benchmarks
using BenchmarkDotNet.Attributes;
using LanguageExt;
using System.Collections.Immutable;
namespace MyImmutableArray;
[MemoryDiagnoser]
public class Benchmarks
{
// BenchmarkDotNet benchmarks to test the speed of .net framework immutable classes
// See this image for results => https://user-images.githubusercontent.com/3111981/165134368-5875560d-47ef-4627-b51f-854e0cf24d36.png
using BenchmarkDotNet.Attributes;
using System.Collections.Immutable;
namespace ConsoleApp20;
[MemoryDiagnoser]
public class BM
using Gambit.GameServer.Contracts;
using Gambit.GameServerIntegrationTests.Drivers;
namespace Gambit.GameServerIntegrationTests.Features.Users;
[Binding]
public class SignInStepDefinitions
{
private readonly UserDriver UserDriver;
@mrpmorris
mrpmorris / ApiEffects.cs
Created May 3, 2022 14:41
Fluxor auto-dispatch API calls
using System.Collections.Concurrent;
using System.Reflection;
using CarePlace.Client.Services;
using Fluxor;
using MediatR;
namespace XXXXXXX.Client.ViewState;
public class ApiEffects
{