Skip to content

Instantly share code, notes, and snippets.

View 0xced's full-sized avatar

Cédric Luthi 0xced

View GitHub Profile
@0xced
0xced / NativeSQLiteWithCostura.csproj
Last active January 19, 2023 20:21
Embed native e_sqlite3.dll or SQLite.Interop.dll with Costura without having to manually copy any dll
<!-- Useful when bundling an app using Microsoft.EntityFrameworkCore.Sqlite, which depends on SQLitePCLRaw.bundle_green, which depends on SQLitePCLRaw.lib.e_sqlite3.v110_xp (having native Windows dlls) -->
<ItemGroup>
<EmbeddedResource Include="$(NugetPackageRoot)\sqlitepclraw.lib.e_sqlite3.v110_xp\1.1.13\runtimes\win-x86\native\e_sqlite3.dll">
<Link>costura32\e_sqlite3.dll</Link>
</EmbeddedResource>
<EmbeddedResource Include="$(NugetPackageRoot)\sqlitepclraw.lib.e_sqlite3.v110_xp\1.1.13\runtimes\win-x64\native\e_sqlite3.dll">
<Link>costura64\e_sqlite3.dll</Link>
</EmbeddedResource>
</ItemGroup>
@0xced
0xced / StringTrimmerInterceptor.cs
Created November 13, 2018 15:49
Entity Framework interceptor to trim strings (remove leading and trailing spaces) before saving to database
using System.Data.Entity;
using System.Data.Entity.Core.Common.CommandTrees;
using System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure.Interception;
using System.Linq;
namespace gist
{
/// Adapted from https://stackoverflow.com/questions/20133696/how-can-i-configure-entity-framework-to-automatically-trim-values-retrieved-for/27504530#27504530
@0xced
0xced / DbContextExtensions.cs
Created October 2, 2018 09:00
Get invariant provider name from DbContext
using System.Data.Entity;
using System.Reflection;
namespace gist
{
public static class DbContextExtensions
{
public static string GetProviderName(this DbContext context)
{
var internalContextProperty = context.GetType().GetProperty("InternalContext", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
@0xced
0xced / WindowsAuthorizationMiddleware.cs
Last active October 3, 2018 09:28
OWIN Middleware for authenticating + authorizing windows accounts (users or groups)
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Threading.Tasks;
using Microsoft.Owin;
// ReSharper disable once CheckNamespace
namespace Owin
{
public static class WindowsAuthorizationAppBuilderExtensions
@0xced
0xced / install_mono_workarounds.sh
Last active June 27, 2019 11:49
Workarounds for using System.Data.SQLite and Microsoft.Azure.Management.ServiceBus on Mono
#!/bin/bash -e -x
# For System.Data.SQLite to work on Mono, see https://stackoverflow.com/questions/21293105/system-dllnotfoundexception-on-mono-sqlite/43173220#43173220
function Install_libSQLiteInterop
{
# Latest version is on https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
SQLITE_VERSION=1.0.111.0
SQLITE_DYLIB=sqlite-netFx-source-${SQLITE_VERSION}/bin/2013/Release/bin/libSQLite.Interop.dylib
pushd ~/Downloads
@0xced
0xced / ForEachAsync.cs
Created May 26, 2018 17:57
Parallel foreach async enumeration with maximum degree of parallelism
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Parallel
{
public static class EnumerableExtensions
{
@0xced
0xced / DbEntityValidationException.cs
Last active June 8, 2018 10:11
Improved DbEntityValidationException message
public partial class MyContext
{
// Adapted from https://blogs.infosupport.com/improving-dbentityvalidationexception/
public override Task<int> SaveChangesAsync()
{
try
{
return base.SaveChangesAsync();
}
catch (DbEntityValidationException exception)
@0xced
0xced / generate_storyboard_constants.py
Created January 25, 2018 09:59
Generate constants from storyboard identifiers
#!/usr/bin/env python
# Adapted from https://joris.kluivers.nl/blog/2014/02/10/storyboard-identifier-constants/ (https://github.com/kluivers/storyboard-constants)
# * Support for accessibility identifiers and accessibility labels
# * Easily extendable with xpath definitions
# * Valid identifiers
# * Namespaced constants, idea from https://www.mikeash.com/pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html
PREFIX = ''
SPACING = '\t'
@0xced
0xced / Emoji.json
Last active November 22, 2023 15:53
Extract emoji by category using the EmojiFoundation framework
{
"People" : [
"😀",
"😃",
"😄",
"😁",
"😆",
"😅",
"😂",
"🤣",
@0xced
0xced / ActuallyLocalizedStringForStatusCode.m
Created November 15, 2017 15:40
Get a *localized* string for a given HTTP status code
#import <Foundation/Foundation.h>
static NSString * _Nonnull ActuallyLocalizedStringForStatusCode(NSInteger statusCode)
{
static NSBundle *cfNetworkBundle;
static dispatch_once_t once;
dispatch_once(&once, ^{
cfNetworkBundle = [NSBundle bundleForClass:NSHTTPURLResponse.class];
});
NSString *httpError = [NSHTTPURLResponse localizedStringForStatusCode:statusCode];