Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Surviving the pandemic

Phil Haack haacked

🏠
Surviving the pandemic
View GitHub Profile
@haacked
haacked / upcast-example.cs
Last active Apr 21, 2022
Upcast record without more specific type properties
View upcast-example.cs
// 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.
@haacked
haacked / never-not.cs
Last active Mar 23, 2021
Weird roslyn waring
View never-not.cs
public interface IArgument
{
/// <summary>
/// The value of the argument sans quotes.
/// </summary>
string Value { get; }
}
public interface IArguments : IReadOnlyList<IArgument>, IArgument
{
@haacked
haacked / ForbiddenTypeAnalyzer.cs
Last active Nov 1, 2020
Roslyn Analyzer to warn about access to forbidden types
View ForbiddenTypeAnalyzer.cs
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)]
View jsonconvert-bug.cs
[Fact]
public void StackTest()
{
var stack = new Stack<string>();
stack.Push("one");
stack.Push("two");
stack.Push("three");
var serialized = JsonConvert.SerializeObject(stack, new JsonSerializerSettings
{
@haacked
haacked / LayoutForPartialTagHelperExample.md
Last active Apr 1, 2020
I would like a way to do this with the partial tag helper.
View LayoutForPartialTagHelperExample.md

What I'm looking for is a nicer more declarative approach to Templated Razor Delegates

In Any old Razor Page or View - SomePage.cshtml

...
<div>
  <partial name="Shared/_PartialLayout" for="SomeModel">
    <strong>Content</strong> that will be injected into the layout, which 
 will get rendered here.
@haacked
haacked / RoleProfileService.cs
Last active Sep 18, 2019
Providing Role Claims
View RoleProfileService.cs
/*
This needs to be registered with the IdentityServer in the Startup.cs
by calling AddProfileService<RoleProfileService>() on the builder returned
by AddIdentityServer.
ex...
var builder = services.AddIdentityServer(options =>
{
options.Events.RaiseErrorEvents = true;
@haacked
haacked / ModelBuilderExtensions.cs
Last active Nov 19, 2021
Example of applying an EF Core global query filter on all entity types that implement an interface
View ModelBuilderExtensions.cs
/*
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;
@haacked
haacked / index.js
Created Feb 9, 2019
Probot handler for the Why Not Both app
View index.js
module.exports = (app) => {
// Respond to new issue comments
app.on('issue_comment.created', async context => {
const message = context.payload.comment.body
// The following does a rough approximation of
// trying to find a question that proposes two
// alternatives. It's not very smart about it,
// but good enough for our case.
const dichotomy = message.toLowerCase()
View keybase.md

Keybase proof

I hereby claim:

  • I am haacked on github.
  • I am haacked (https://keybase.io/haacked) on keybase.
  • I have a public key whose fingerprint is BF14 9135 A9BB DB1E DF3B 0F27 51AC AFF3 A55E 8DE3

To claim this, I am signing this object:

@haacked
haacked / reading-list.md
Last active Feb 24, 2017
A selection of articles and books that have inspired some of the topics in recent talks I've given.
View reading-list.md