Skip to content

Instantly share code, notes, and snippets.

View davepcallan's full-sized avatar

Dave Callan davepcallan

View GitHub Profile
@davepcallan
davepcallan / HotelBookingTemplate.cs
Created January 3, 2025 09:03
Template Method Pattern C# simple example
public abstract class HotelBookingTemplate
{
// Template Method
public void BookRoom()
{
SelectRoom();
ProcessPayment();
ApplyDiscount();
SendBookingConfirmation();
}
@davepcallan
davepcallan / Program.cs
Created January 3, 2025 08:42
.NET Rate limiting simple examples
using Microsoft.AspNetCore.RateLimiting;
using System.Threading.RateLimiting;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRateLimiter(options =>
{
// 1. Fixed Window Limiter (20 requests per 2 minutes)
// -----------------------------------------------
// |----------------- 2 min -----------------|
@davepcallan
davepcallan / SlowQueryInterceptor.cs
Last active November 25, 2025 15:54
Finding slow queries with Entity Framework Interceptors
using Microsoft.EntityFrameworkCore.Diagnostics;
using System.Data.Common;
namespace EntityFrameworkExamples;
public class SlowQueryInterceptor(ILogger<SlowQueryInterceptor> logger) : DbCommandInterceptor
{
private const int _slowQueryThresholdInMilliseconds = 5; //from config
public override DbDataReader ReaderExecuted(
@davepcallan
davepcallan / CreateOrder.cs
Last active December 18, 2024 00:15
Vertical Slice Architecture example - UseCases -> CreateOrder.cs
// 1. Order Entity
namespace VerticalSliceExample.Domain;
public class Order
{
public int Id { get; set; }
public int CustomerId { get; set; }
public Dictionary<int, int> Products { get; set; } = new(); // ProductId -> Quantity
public decimal TotalAmount { get; private set; }
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
@davepcallan
davepcallan / CountByDotnet9.cs
Created November 30, 2024 15:50
.NET 9 new LINQ CountBy compared to existing approaches benchmarks
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
using System;
using System.Collections.Generic;
using System.Linq;
@davepcallan
davepcallan / FindSparseColumnCandidates.sql
Created May 11, 2024 10:24
SQL Server SQL script to find sparse column candidates
USE TestDB
GO
DECLARE @DatabaseName VARCHAR(100)
DECLARE @SchemaName VARCHAR(100)
DECLARE @TableName VARCHAR(100)
DECLARE @ColumnName VARCHAR(100)
DECLARE @FullyQualifiedTableName VARCHAR(500)
--Create Temp Table to Save Results
@davepcallan
davepcallan / AddColumnIfNotExists.sql
Created May 7, 2024 16:20
Add created_date column to all tables which don't have it already in SQL Server
SELECT 'ALTER TABLE ' + QUOTENAME(ss.name) + '.' + QUOTENAME(st.name) + ' ADD created_date DATETIME NULL;'
FROM sys.tables st
INNER JOIN sys.schemas ss on st.[schema_id] = ss.[schema_id]
WHERE st.is_ms_shipped = 0
AND NOT EXISTS (
SELECT 1
FROM sys.columns sc
WHERE sc.[object_id] = st.[object_id]
AND sc.name = 'created_date'
)
@davepcallan
davepcallan / userInfo.graphql
Created April 25, 2024 15:30
Example query of how to get user information from the GitHub graphql API
{
rateLimit {
limit
remaining
used
resetAt
}
user(login: "davepcallan") {
name
bio
@davepcallan
davepcallan / ExceptionHandlingDotnet9.cs
Created April 11, 2024 18:56
Exception Handling .NET 9 v .NET 8
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Environments;
namespace ExceptionBenchmark
{
[Config(typeof(Config))]
@davepcallan
davepcallan / SQLServerTruncateRollbackExample.sql
Created April 5, 2024 16:43
Sample SQL which shows rollback IS possible in SQL Server
-- Create Test Table
CREATE TABLE TruncateTest (ID INT)
INSERT INTO TruncateTest (ID)
SELECT 1
UNION ALL
SELECT 2
UNION ALL
SELECT 3
GO
-- Check the data before truncate