Skip to content

Instantly share code, notes, and snippets.

View Calabonga's full-sized avatar
💭
Пишите правильный код

Calabonga Calabonga

💭
Пишите правильный код
View GitHub Profile
@Calabonga
Calabonga / DebugConfigurationDefinition
Created June 1, 2024 01:21
AppDefinitions for ASP.NET Core
/// <summary>
/// Show loaded configurations for ASP.NET Core or Web API application
/// </summary>
public class DebugConfigurationDefinition : AppDefinition
{
#if DEBUG
public override bool Enabled => true;
#else
public override bool Enabled => false;
#endif
@Calabonga
Calabonga / Money.cs
Created March 11, 2024 03:04
Implicit operators demo
public struct Money
{
private readonly double _amount;
public Money(double amount)
{
_amount = amount;
}
public double Amount => _amount;
@Calabonga
Calabonga / LastName.cs
Created October 29, 2023 04:04
ValueObject implementeation
public sealed record LastName
{
public const int MaxLength = 32;
private LastName(string value)
{
Value = value;
}
public string Value { get; private set; }
@Calabonga
Calabonga / BenchmarkTemplate.cs
Created December 22, 2022 01:01
Benchmarking with BenchmarkDotNet
[RankColumn]
[HideColumns(new string[] { "Job" })]
[MemoryDiagnoser(false)]
[SimpleJob(RuntimeMoniker.Net60)]
[SimpleJob(RuntimeMoniker.Net70)]
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByMethod)]
public class Benchmark
{
// ... start from here
}
@Calabonga
Calabonga / JsonSerializerOptions Demo
Created September 3, 2022 03:44
JsonSerializerOptions
var options = new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
ReferenceHandler = ReferenceHandler.IgnoreCycles,
WriteIndented = true
}
var json = JsonSerializer.Serialize(state, options);
@Calabonga
Calabonga / Program.cs
Last active November 24, 2022 02:37
Logger and configuration for Console Application
/* required nugets
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="7.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="6.0.1" />
@Calabonga
Calabonga / Directory.Build.props
Created May 4, 2022 01:39
Visual Studio properties for all projects
<Project>
<PropertyGroup>
<!-- <LangVersion>latest</LangVersion> -->
<!--<LangVersion>preview</LangVersion>-->
<LangVersion>8</LangVersion>
</PropertyGroup>
</Project>
@Calabonga
Calabonga / xunit.runner.json
Last active April 29, 2022 03:21
xunit.runner.json
{
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
"methodDisplay": "method",
"methodDisplayOptions": "all"
}
@Calabonga
Calabonga / TaskHelper.cs
Created April 20, 2022 08:24
Run and forget task C#
public static class TaskHelper
{
/// <summary>
/// Runs a TPL Task fire-and-forget style, the right way - in the
/// background, separate from the current thread, with no risk
/// of it trying to rejoin the current thread.
/// </summary>
public static void RunBg(Func<Task> fn) => Task.Run(fn).ConfigureAwait(false);
/// <summary>
@Calabonga
Calabonga / AsyncHelper.cs
Last active April 20, 2022 04:17
AsyncHelper helps to start asynchronous method as synchronously
public static class AsyncHelpers
{
/// <summary>
/// Executes an async Task<T> method which has a void return value synchronously
/// </summary>
/// <param name="task">Task<T> method to execute</param>
public static void RunSync(Func<Task> task)
{
var oldContext = SynchronizationContext.Current;
var synch = new ExclusiveSynchronizationContext();