Skip to content

Instantly share code, notes, and snippets.

View JimBobSquarePants's full-sized avatar
💭
(•_•) ( •_•)>⌐■-■ (⌐■_■)

James Jackson-South JimBobSquarePants

💭
(•_•) ( •_•)>⌐■-■ (⌐■_■)
View GitHub Profile
@JimBobSquarePants
JimBobSquarePants / RegisterLocalDb.cs
Created February 28, 2020 04:04
Registering a LocalDb instance via MS DI.
static void RegisterDbContext<TContext>(IServiceCollection services)
where TContext : DbContext
{
services.AddSingleton<SqlInstance<TContext>>(p => new SqlInstance<TContext>(builder => (TContext)ActivatorUtilities.CreateInstance<TContext>(p, builder.Options)));
services.AddScoped<SqlDatabase<TContext>>(p =>
{
SqlInstance<TContext> sqlInstance = p.GetRequiredService<SqlInstance<TContext>>();
// Safe excecution of async via dedicated factory and unwrapping.
@JimBobSquarePants
JimBobSquarePants / git-ignore-clear-cache.txt
Created July 12, 2019 01:34
Git commands for clearing cache
git rm -r --cached .
git add .
@JimBobSquarePants
JimBobSquarePants / build.sh
Created June 20, 2019 06:03
Bash Build Whoas
#!/usr/bin/env bash
set -eu
declare -a build_projects=('./src/My.Extensions.ApiVersioning/')
declare -a test_projects=('./tests/IntegrationTests/' './tests/UnitTests/')
pwsh -NoProfile -ExecutionPolicy Bypass -Command "& '.\build.ps1' -projects " "${build_projects[@]}" "-tests " "${test_projects[@]}"
if [ $# -ne 0 ]; then
echo "Failed to build."
@JimBobSquarePants
JimBobSquarePants / IQueryableSql.sql
Last active June 12, 2019 06:01
Comparison of EF query mapping approaches.
--Microsoft.EntityFrameworkCore.Database.Command:Information: Executed DbCommand (176ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT CASE
WHEN EXISTS (
SELECT 1
FROM [Customers] AS [x])
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END
--Microsoft.EntityFrameworkCore.Database.Command:Information: Executed DbCommand (185ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
@JimBobSquarePants
JimBobSquarePants / build.cmd
Last active May 2, 2019 07:03
cmd to bash???
@echo Off
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '.\build.ps1'"
if not "%errorlevel%"=="0" goto failure
:success
ECHO successfully built project
REM exit 0
goto end
@JimBobSquarePants
JimBobSquarePants / EventsDemo.cs
Created February 7, 2019 23:21
A very simple demo of how to use events in DI environments.
// A very simple service example contract with an event.
// Using an interface ensures that every implementation has same events.
public interface IContentService
{
event EventHandler Saved;
}
// Our component contract
public interface IComponent : IDisposable
{
@JimBobSquarePants
JimBobSquarePants / TransformDemo.cs
Last active November 12, 2018 11:33
Demo of how matrix transforms differ
void Main()
{
Matrix4x4 yawPitchRoll = Matrix4x4.CreateFromYawPitchRoll(25, 32, 67);
Matrix4x4 translateM4 = Matrix4x4.CreateTranslation(new Vector3(3, 6, 0));
Matrix3x2 translate = Matrix3x2.CreateTranslation(new Vector2(3, 6));
Vector2 target = Vector2.Zero;
// All three of the following methods yield the correct transform vector <3,6>
Vector2.Transform(target, translate).Dump();
@JimBobSquarePants
JimBobSquarePants / Xor.cs
Created November 8, 2018 14:10
Xor combination of two Guids.
public static Guid Xor(Guid a, Guid b)
{
unsafe
{
Int64* ap = (Int64*)&a;
Int64* bp = (Int64*)&b;
ap[0] ^= bp[0];
ap[1] ^= bp[1];
return *(Guid*)ap;
@JimBobSquarePants
JimBobSquarePants / JitFail.cs
Created September 27, 2018 14:19
Code that fails in Release only.
private void ProcessScanlineFromPalette<TPixel>(ref byte scanlineRef, ref TPixel rowRef)
where TPixel : struct, IPixel<TPixel>
{
ReadOnlySpan<Rgb24> palettePixels = MemoryMarshal.Cast<byte, Rgb24>(this.palette);
ref Rgb24 palettePixelsRef = ref MemoryMarshal.GetReference(palettePixels);
if (this.paletteAlpha?.Length > 0)
{
// If the alpha palette is not null and has one or more entries, this means, that the image contains an alpha
// channel and we should try to read it.
@JimBobSquarePants
JimBobSquarePants / packing.cs
Created September 26, 2018 09:51
Packing to and from 8bit array
void Main()
{
const int bytesPer8bitScanline = 300;
const int bytesPerScanline = 38;
const int bits = 1;
Random r = new Random();
byte[] source = new byte[bytesPer8bitScanline];
for (int i = 0; i < source.Length; i++)
{