Skip to content

Instantly share code, notes, and snippets.

@eocron
eocron / validation.cs
Last active March 19, 2024 16:57
How validation can be done Fully-Coolly
public class Program
{
public static async Task Main()
{
var results = Validate.If(() => "a" == "a")
//optional then branch
.Then(()=>
{
Console.WriteLine("a equals a, so Then was executed");
//some code
@eocron
eocron / gist:94b5f3f66bbc0a16f64ffb6ce8f2c8ef
Created February 28, 2024 07:43
OxyPlot drag&drop + create on double click
public class SelectedDataPointViewModel
{
private readonly LineSeries _series;
public DataPoint StartDataPoint { get; private set; }
public DataPoint CurrentDataPoint => _series.Points[SelectedDataPointIndex];
public int SelectedDataPointIndex { get; set; }
public bool IsDragging { get; set; }
public float InitiationDistance { get; set; }
@eocron
eocron / cleanup_csproj_deps.ps1
Created May 19, 2023 08:25
Remove csproj duplicate dependencies
param ($BasePath = './')
$CsProjects = Get-ChildItem -Path $BasePath -Filter '*.csproj' -File -recurse
foreach ($csProjFile in $CsProjects)
{
$path = $csProjFile.FullName
Write-Host $path
$xml = [XML](Get-Content $path)
@eocron
eocron / SuffixAutomata.cs
Created August 5, 2020 23:06
Suffix automata implementation and few algorithms from https://cp-algorithms.com/string/suffix-automaton.html
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1
{
public interface ISuffixAutomataState<T>
{
bool IsTerminal { get; }
@eocron
eocron / StringGenerator.cs
Created November 5, 2019 12:50
Random domain string generator
using System;
using System.Linq;
public sealed class StringGenerator
{
private const int DefaultRandomStringLength = 10;
private static readonly Random Rnd = new Random();
private volatile char[] _charDomain;
public char[] CharacterDomain
@eocron
eocron / PathHelper.cs
Created July 2, 2019 09:10
Retrieves full path, regardless where is executing directory. It is essential method for testing and when running services under system32 path.
public static class PathHelper
{
private static readonly string DataPath;
static PathHelper()
{
var assembly = Assembly.GetAssembly(typeof(PathHelper));
var codebase = assembly.CodeBase.Replace("file:///", "");
var baseDir = Path.GetDirectoryName(codebase);
DataPath = baseDir;
@eocron
eocron / ReferenceAssembly.cs
Last active April 24, 2019 08:17
References assembly through type reference, so assembly will be copied on build even, if it is not used explicitly (for example, plugins not used until some point in application, but need to be copied). Works in Release mode too.
public static class AssemblyReference
{
/// <summary>
/// Installs reference to assembly in which specified type resides.
/// It will be copied on build even in release mode.
/// </summary>
/// <typeparam name="T"></typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void Install<T>()
{
@eocron
eocron / PagedMapSync
Created February 5, 2018 20:46
Used for synchronization of two storages
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
namespace ConsoleApplication1
{
/// <summary>
/// Performs synchronization between continuous collection and unknown collection.
/// For example, between continuous MSSQL table ids and unknown mapped id in other system, like ElasticSearch.
@eocron
eocron / LockScope.cs
Last active October 28, 2017 14:06
Simple synchronization primitive in concept of 'using' directive. Just aquire read/write scope, execute critical section and don't forget to dispose it after you are done. Also includes dictionary for multiple scopes retrieval at once..
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
namespace Helpers
{
public sealed class LockScope
@eocron
eocron / SerializationHelpers.cs
Last active October 27, 2017 22:21
Just another JSON/XML serializer/deserializer on DataContract without external dependencies. Also have human-readable xml serializer.
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Xml;
namespace Helpers
{
public static class JsonHelper