Skip to content

Instantly share code, notes, and snippets.

View davepcallan's full-sized avatar

Dave Callan davepcallan

View GitHub Profile
@davepcallan
davepcallan / GetRuntimeInformation.cs
Created March 4, 2024 02:47
Get runtime information
using System;
using System.Runtime.InteropServices;
public class C {
static void Main(string[] args)
{
Console.WriteLine(RuntimeInformation.OSDescription);
Console.WriteLine(RuntimeInformation.ProcessArchitecture.ToString());
Console.WriteLine(RuntimeInformation.FrameworkDescription);
}
@davepcallan
davepcallan / GlobalExceptionHandlerProblemDetails.cs
Last active March 3, 2024 04:55
Example of using .NET 8 IExceptionHandler to return 7807 ProblemDetails compliant response to client
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using System.Net;
namespace Samples;
public class GlobalExceptionHandler(ILogger<GlobalExceptionHandler> logger) : IExceptionHandler
{
public async ValueTask<bool> TryHandleAsync(
HttpContext httpContext,
@davepcallan
davepcallan / GlobalExceptionHandler.cs
Last active March 4, 2024 13:42
ASP.NET 8 Global exception handling with IExceptionHandler example
using Microsoft.AspNetCore.Diagnostics;
namespace Samples;
public class GlobalExceptionHandler(ILogger<GlobalExceptionHandler> logger) : IExceptionHandler
{
public ValueTask<bool> TryHandleAsync(
HttpContext httpContext,
Exception exception,
CancellationToken cancellationToken)
@davepcallan
davepcallan / CollectionExpressions.cs
Created February 13, 2024 12:52
Creating list with regular v collection expression approach in .NET 8
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
[Config(typeof(Config))]
[HideColumns(Column.Job, Column.RatioSD, Column.AllocRatio)]
[MemoryDiagnoser]
@davepcallan
davepcallan / StringContains.cs
Last active April 23, 2024 16:21
In .NET 9 string.contains("X") will delegate to string.contains('X')
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
@davepcallan
davepcallan / dotnet9LINQCountByBenchmark.cs
Created January 29, 2024 19:43
Benchmarks for .NET 9 new LINQ CountBy() method
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
namespace Benchmarks
{
[Config(typeof(Config))]
@davepcallan
davepcallan / GetMyOrdersVSA.cs
Created January 29, 2024 15:51
GetMyOrders Vertical slice refactor example from eShopOnWeb
namespace Microsoft.eShopWeb.Features.Orders;
[Route("order/my-orders")]
public class GetMyOrdersController(CatalogContext db) : Controller
{
[HttpGet]
public async Task<IActionResult> MyOrders()
{
var userName = User.Identity.Name;
var orders = await db.Orders
@davepcallan
davepcallan / tableRowAndSize.sql
Created January 23, 2024 17:44
SQL Server query to get row counts and size (MB) for all tables in a DB
;WITH TableSizes AS (
SELECT
s.Name AS SchemaName,
t.NAME AS TableName,
p.rows AS RowCounts,
CAST(SUM(a.total_pages) * 8 / 1024.0 AS DECIMAL(10, 2)) AS TotalSpaceMB
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
@davepcallan
davepcallan / dotnet9LinqBenchmarks.cs
Last active February 8, 2024 19:00
.NET 8 v .NET 9 LINQ benchmarks
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
namespace Benchmarks
{
[Config(typeof(Config))]
@davepcallan
davepcallan / globalQueryFilters.cs
Last active February 28, 2024 05:26
Apply global query filters automatically to all entities which implement an interface or have a particular property
public static void ApplyGlobalFilters<TInterface>(this ModelBuilder modelBuilder,
Expression<Func<TInterface,
bool>> expression)
{
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
if (entityType.ClrType.GetInterface(typeof(TInterface).Name) != null)
{
var newParam = Expression.Parameter(entityType.ClrType);