Skip to content

Instantly share code, notes, and snippets.

View haacked's full-sized avatar
🏠
Code code code

Phil Haack haacked

🏠
Code code code
View GitHub Profile
@haacked
haacked / ModelBuilderExtensions.cs
Last active March 29, 2024 14:16
Example of applying an EF Core global query filter on all entity types that implement an interface
/*
Copyright Phil Haack
Licensed under the MIT license - https://github.com/haacked/CodeHaacks/blob/main/LICENSE.
*/
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Text.RegularExpressions;
using System.Reflection;
namespace RegexLibraryBuilder
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class RegexBuilderMain
{
@haacked
haacked / TestHelper.cs
Created January 14, 2012 07:25
String Comparison Unit Test Helper
public static class TestHelpers
{
public static void ShouldEqualWithDiff(this string actualValue, string expectedValue)
{
ShouldEqualWithDiff(actualValue, expectedValue, DiffStyle.Full, Console.Out);
}
public static void ShouldEqualWithDiff(this string actualValue, string expectedValue, DiffStyle diffStyle)
{
ShouldEqualWithDiff(actualValue, expectedValue, diffStyle, Console.Out);
@haacked
haacked / ForbiddenTypeAnalyzer.cs
Last active May 14, 2023 18:38
Roslyn Analyzer to warn about access to forbidden types
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;
// CREDIT: https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/Core/SymbolIsBannedAnalyzer.cs
[DiagnosticAnalyzer(LanguageNames.CSharp)]
@haacked
haacked / ServiceResolverAdapter.cs
Created March 11, 2012 19:34
ServiceResolverAdapter: Allows you to use the ASP.NET MVC DependencyResolver for ASP.NET Web API
public class ServiceResolverAdapter : IDependencyResolver
{
private readonly System.Web.Mvc.IDependencyResolver dependencyResolver;
public ServiceResolverAdapter(System.Web.Mvc.IDependencyResolver dependencyResolver)
{
if (dependencyResolver == null) throw new ArgumentNullException("dependencyResolver");
this.dependencyResolver = dependencyResolver;
}
@haacked
haacked / gist:912630
Created April 10, 2011 19:16
jquery validation with radio buttons.
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
<style type="text/css">
input.error
{
border: solid 1px red;
color: Red;
}
@haacked
haacked / stripe-mrr.cs
Last active November 8, 2022 12:54
Calculating MRR with C# and the Stripe API
// This is the source for the blog post here: https://haacked.com/archive/2022/10/12/calculating-mrr-with-stripe-and-csharp/
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace Stripe;
public static class StripeExtensions
@haacked
haacked / serious-startup-resources.md
Last active October 28, 2022 00:17
Resources from Serious Lessons Building a Startup with Azure and ASP.NET Core
  1. Abbot - The product my company is building and subject of the talk.
  2. A Serious Business, Inc. - My company.
  3. Age and High-Growth Entrepreneurship - Contains the graph of the probability of successful exit by age.
  4. Hubot - our old friend and inspiration for Abbot.
  5. Chat-Ops - the way of working together in Chat with a bot.
  6. YCombinator - The Startup Incubator we joined.
  7. ASP.NET Core - A big part of our tech stack
  8. PROJECT MANAGEMENT - A TREE SWING STORY - source of the tree-swing cartoon which originally started in the 70s
  9. [Making sense of MVP](https://blog.crisp.se/2016/01/25/henrikkn
@haacked
haacked / download-nuget-licenses.ps1
Last active September 5, 2022 16:35
A PowerShell script to download your NuGet package licenses as first seen in http://haacked.com/archive/2015/03/28/download-nuget-package-licenses/
Split-Path -parent $dte.Solution.FileName | cd
New-Item -ItemType Directory -Force -Path ".\licenses"
@( Get-Project -All | ? { $_.ProjectName } | % { Get-Package -ProjectName $_.ProjectName } ) | Sort -Unique Id | % { $pkg = $_ ; Try { (New-Object System.Net.WebClient).DownloadFile($pkg.LicenseUrl, (Join-Path (pwd) 'licenses\') + $pkg.Id + ".html") } Catch [system.exception] { Write-Host "Could not download license for $($pkg.Id)" } }
@haacked
haacked / upcast-example.cs
Last active April 21, 2022 20:05
Upcast record without more specific type properties
// Suppose we have the two record types
public record Person(int Id) : PersonUpdate()
{
};
public record PersonUpdate()
{
public string Name {get; init;}
//... bunch of other properties.