Skip to content

Instantly share code, notes, and snippets.

@mrpmorris
mrpmorris / Program.cs
Last active October 2, 2024 13:45
Internal
using Microsoft.EntityFrameworkCore;
public abstract class EntityBase<TKey> where TKey : IEquatable<TKey>
{
public abstract TKey Id { get; set; }
}
public abstract class RepositoryBase<TDbContext, TEntity, TKey>
where TDbContext : DbContext, new()
where TEntity : EntityBase<TKey>
@mrpmorris
mrpmorris / Morris.Extensions.Aspire.ExternalSolutionSupport.csproj
Last active April 9, 2024 12:15
Prototype. Add references to SLN files without including them in your Aspire solution
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Title>Morris.Extensions.Aspire.ExternalSolutionSupport</Title>
<PackageId>Morris.Extensions.Aspire.ExternalSolutionSupport</PackageId>
<Description>Referenced Solutions support for Aspire</Description>
<Nullable>enable</Nullable>
<ImplicitUsings>true</ImplicitUsings>
<IncludeBuildOutput>false</IncludeBuildOutput>
<IsRoslynComponent>true</IsRoslynComponent>
<TargetFramework>netstandard2.0</TargetFramework>
@mrpmorris
mrpmorris / ConsoleApp153.csproj
Last active April 11, 2024 21:19
Proof that automapper can be faster than your own manually written code
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
@mrpmorris
mrpmorris / eggs.cs
Last active January 25, 2024 19:40
Convert string of floats to floats
static readonly char[] ExtractFloatsTrimChars = new char[] { '[', ']', ' ' };
static float[] ExtractFloats(string input) =>
input
.Trim(ExtractFloatsTrimChars)
.Split(',')
.Select(float.Parse)
.ToArray();
@mrpmorris
mrpmorris / ActivityTrackingMiddleware.cs
Last active January 21, 2024 18:26
Azure function telemetry
public class ActivityTrackingMiddleware : IFunctionsWorkerMiddleware
{
public const string ActivitySourceName = "AzureFunctionsWorker";
private readonly static ConcurrentDictionary<string, TriggerParameterInfo> FunctionIdToTriggerParameterInfoLookup = new();
private readonly static ActivitySource ActivitySource = new(ActivitySourceName);
private readonly HttpTriggerHandler HttpTriggerHandler;
private readonly ActivityTriggerHandler ActivityTriggerHandler;
private readonly FrozenDictionary<string, ICustomTriggerHandler> CustomTriggerHandlers;
@mrpmorris
mrpmorris / IQueryableExtensions.cs
Created October 13, 2022 15:14
OData sorting / filtering from Blazor TelerikGrid through to the server
using MyApp.Contracts;
using Community.OData.Linq;
using Microsoft.EntityFrameworkCore;
namespace CarePlace.AppLayer.Persistence;
internal static class IQueryableExtensions
{
public static IQueryable<T> ApplyODataParameters<T>(
this IQueryable<T> source,
@mrpmorris
mrpmorris / IdentityService.cs
Created September 9, 2022 14:41
IdentityService.cs
internal class IdentityService : IIdentityService
{
// Other properties like the current user etc
public bool IsRunningWithElevatedPrivileges => ElevatedPrivilegeCount > 0;
private volatile int ElevatedPrivilegeCount;
public IDisposable ExecuteWithElevatedPrivileges()
{
Interlocked.Increment(ref ElevatedPrivilegeCount);
return new DisposableCallback(() => Interlocked.Decrement(ref ElevatedPrivilegeCount));
@mrpmorris
mrpmorris / DisposableAction.cs
Created September 9, 2022 14:37
DisposableAction
public sealed class DisposableCallback : IDisposable
{
private readonly Action Action;
private readonly string CallerFilePath;
private readonly int CallerLineNumber;
private readonly bool WasCreated;
private bool IsDisposed;
/// <summary>
/// Creates an instance of the class
@mrpmorris
mrpmorris / IntegrationTestsServer.cs
Created July 21, 2022 15:37
Web integration testing with asp.net and WebSockets
public static class IntegrationTestsServer
{
static IntegrationTestsServer()
{
ConfigureMocks();
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "IntegrationTesting");
WebApplicationFactory<Program> appBuilder = new WebApplicationFactory<Program>()
.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices(services =>
@mrpmorris
mrpmorris / StringHelper.cs
Created June 21, 2022 15:54
ObscureEmailAddress
public static class StringHelper
{
public const byte DefaultUnobscuredLength = 3;
public enum Keep { Start, End };
public static string? ObscureEmailAddress(string? value)
{
if (string.IsNullOrEmpty(value))
return value;