Skip to content

Instantly share code, notes, and snippets.

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

James Jackson-South JimBobSquarePants

💭
(•_•) ( •_•)>⌐■-■ (⌐■_■)
View GitHub Profile
@JimBobSquarePants
JimBobSquarePants / ScopedServiceRunner.cs
Created November 10, 2020 21:46
Allows the controlled running of scoped services with automatic cleanup.
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
namespace ThingsWhatImNotProudOf
{
/// <summary>
/// Allows the manufacture and running of items within a smaller lifetime scope than the parent.
/// To be used sparingly when unable to inject a service within the required scope.
/// </summary>
@JimBobSquarePants
JimBobSquarePants / intersection.cs
Last active June 24, 2020 11:36
Calculating intersections between two segments.
void Main()
{
var tl = new Vector2(0, 0);
var br = new Vector2(5, 3);
var tr = new Vector2(5, 0);
var bl = new Vector2(0, 3);
var tlbr = new Segment(tl, br);
var trbl = new Segment(tr, bl);
// Create a query where the result must match or contain the given query
// and the subset of properties and filters.
descriptor.Query(
q => q.Bool(
b => b.Must(
mu =>
mu.MultiMatch(
m => m.Fields(propertyExpressions) // Search within these properties or all.
.Query(searchTerm) // For this query
.Operator(Operator.Or))) // In any of the properties.
@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 / TypeWrapper.cs
Last active February 20, 2020 21:59
You can do really crazy things with C#
public class TypeWrapper
{
private readonly dynamic dyn;
private readonly Dictionary<string, CallSite<Action<CallSite, object, object>>> setters
= new Dictionary<string, CallSite<Action<CallSite, object, object>>>();
private readonly Dictionary<string, CallSite<Func<CallSite, object, object>>> getters
= new Dictionary<string, CallSite<Func<CallSite, object, object>>>();
public TypeWrapper(object d)
@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
{