Skip to content

Instantly share code, notes, and snippets.

View werwolfby's full-sized avatar

Alexander Puzynia werwolfby

  • EPAM Systems
  • Seattle, USA
View GitHub Profile
@werwolfby
werwolfby / CustomJsonSerializerOptions.cs
Last active June 27, 2024 17:24
Custom JsonConverter that could fallback to default one
public static class CustomJsonSerializerOptions
{
public static readonly JsonSerializerOptions Options = new() {
PropertyNamingPolicy = new SnakeCaseJsonNamingPolicy(),
PropertyNameCaseInsensitive = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters = {
new JsonStringEnumConverter (new SnakeCaseJsonNamingPolicy()),
// Other converters
}
@werwolfby
werwolfby / TaskEx.cs
Last active May 30, 2024 08:29
When All with results
public static class TaskEx
{
public static async Task<(T1, T2)> WhenAll<T1, T2>(Task<T1> task1, Task<T2> task2)
{
await Task.WhenAll(task1, task2);
return (task1.GetAwaiter().GetResult(), task2.GetAwaiter().GetResult());
}
public static async Task<(T1, T2, T3)> WhenAll<T1, T2, T3>(Task<T1> task1, Task<T2> task2, Task<T3> task3)
{
@werwolfby
werwolfby / WithTimeoutExtensions.cs
Created June 23, 2020 23:37
WithTimeout extension methods for use in UnitTests
public static class WithTimeoutExtension
{
public static Task WithTimeout(this Task task, int timeout, string actionName = null) => task.WithTimeout(TimeSpan.FromMilliseconds(timeout), actionName);
public static async Task WithTimeout(this Task task, TimeSpan timeout, string actionName = null)
{
var delay = Task.Delay(timeout);
var completedTask = await Task.WhenAny(task, delay);
if (completedTask == task)
{
@werwolfby
werwolfby / TypeExtensions.cs
Created March 20, 2020 06:41
Search type (base or interface) in type with ability to search concrete type by generic type definition
using System;
namespace DataAccess
{
public static class TypeExtensions
{
public static Type? SearchType(this Type type, Type searchType)
{
return searchType.IsInterface
? SearchInterfaceType(type, searchType)
@werwolfby
werwolfby / Tracer.py
Last active November 23, 2019 06:20
Transmission Helper
import logging
__author__ = 'WerWolf'
def trace(logger, level=logging.INFO):
def wrapped(func):
def tmp(*args, **kwargs):
if logger.isEnabledFor(level):
logger.log(level, "Start %s" % func.__name__)
@werwolfby
werwolfby / EnumConverter.cs
Last active August 22, 2018 12:03
Helper method to convert to any Enum from int
public static class EnumConverter<TEnum>
where TEnum : Enum
{
private static readonly Dictionary<int, TEnum> _values;
static EnumConverter()
{
_values = new Dictionary<int, TEnum>();
var enumValues = Enum.GetValues(typeof(TEnum));
@werwolfby
werwolfby / <Custom>DbContext.cs
Last active September 16, 2024 11:32
EF Core DateTime Utc kind
public class CustomDbContext : DbContext
{
// ...
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
// Replace default materializer source to custom, to convert DateTimes
options.ReplaceService<IEntityMaterializerSource, DateTimeKindEntityMaterializerSource>();
base.OnConfiguring(options);
/*******************************************************
*
* Created by: Alexander Puzynia aka WerWolf
* Created: 17.10.2010 23:36
*
* File: MemberAccessorHelper.cs
* Remarks:
*
* History:
* 17.10.2010 23:36 - Create Wireframe
@werwolfby
werwolfby / AccessPrivateWrapper.cs
Created December 24, 2017 09:47
Can be used in UnitTests to access private members over `dynamic` keyword.
public class AccessPrivateWrapper : DynamicObject
{
/// <summary>
/// The object we are going to wrap
/// </summary>
private readonly object _instance;
/// <summary>
/// Specify the flags for accessing members
/// </summary>
public static class StaticReflection
{
public static MemberExpression GetMemberExpression(LambdaExpression expression)
{
return GetMemberExpression() ?? throw new ArgumentException(@"Not a member access", nameof(expression));
MemberExpression GetMemberExpression()
{
switch (expression.Body.NodeType)
{